Skip to content

Commit

Permalink
ENHANCEMENT New projects will set config.platform.php to that specifi…
Browse files Browse the repository at this point in the history
…ed by root requirements (#66)

Fixes silverstripe/silverstripe-framework#7709
  • Loading branch information
Damian Mooyman authored and Aaron Carlino committed Jan 17, 2018
1 parent 126366d commit c1ab90c
Showing 1 changed file with 106 additions and 76 deletions.
182 changes: 106 additions & 76 deletions src/Utility/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,96 +45,42 @@ public static function createProject(
$repository = null,
$preferDist = false
) {
// Create-options
$createOptions = [
"--no-secure-http",
"--no-interaction",
"--ignore-platform-reqs",
];

// Set dev / stable options
if ($preferDist) {
$createOptions[] = "--prefer-dist";
$createOptions[] = "--no-dev";
} else {
$createOptions[] = "--prefer-source";
$createOptions[] = "--keep-vcs"; // create only
}

// If using a repository, we must delay for a later update
if ($repository) {
$createOptions[] = '--repository';
$createOptions[] = $repository;
$createOptions[] = '--no-install';
}

// Create comand
$createOptions = self::getCreateOptions($repository, $preferDist);
$runner->runCommand(array_merge([
"composer",
"create-project",
$recipe,
$directory,
$version
$version,
], $createOptions), "Could not create project with version {$version}");

// Update un-installed project with custom repository
if ($repository) {
// Add repository temporarily
$runner->runCommand([
'composer',
'config',
'repositories.temp',
'composer',
$repository,
'--working-dir',
$directory,
]);
// Enable http:// local repositories
$runner->runCommand([
'composer',
'config',
'secure-http',
'false',
'--working-dir',
$directory,
]);
// Set composer config
$customConfig = self::getUpdateConfig($directory, $repository);
foreach ($customConfig as $option => $arguments) {
$runner->runCommand(array_merge(
['composer', 'config', $option],
$arguments,
['--working-dir', $directory]
));
}

// update options
$updateOptions = [
"--no-interaction",
"--ignore-platform-reqs",
];

// Set dev / stable options
if ($preferDist) {
$updateOptions[] = "--prefer-dist";
$updateOptions[] = "--no-dev";
} else {
$updateOptions[] = "--prefer-source";
}

// Update with the given repository
$runner->runCommand(array_merge([
'composer',
'update',
'--working-dir',
$directory,
], $updateOptions), "Could not update project");
// Update with the given repository
$updateOptions = self::getUpdateOptions($preferDist);
$runner->runCommand(array_merge([
'composer',
'update',
'--working-dir',
$directory,
], $updateOptions), "Could not update project");

// Revert changes made above
// Revert all custom config
foreach ($customConfig as $option => $arguments) {
$runner->runCommand([
'composer',
'config',
'--unset',
'repositories.temp',
'--working-dir',
$directory,
]);
$runner->runCommand([
'composer',
'config',
'--unset',
'secure-http',
$option,
'--working-dir',
$directory,
]);
Expand All @@ -155,4 +101,88 @@ public static function getOAUTHToken(CommandRunner $runner)
$result = $runner->runCommand($command, $error);
return trim($result);
}

/**
* Get list of custom config to use for `composer update` when creating a project
*
* @param string $directory
* @param string $repository
* @return array
*/
protected static function getUpdateConfig($directory, $repository)
{
// Register all custom options to temporarily set
$customConfig = [];

// If `requirements.php` is specified, set platform to lowest platform version
$composerData = Config::loadFromFile($directory . '/composer.json');
if (isset($composerData['require']['php'])
&& preg_match('/^[\\D]*(?<version>[\\d.]+)/', $composerData['require']['php'], $matches)
) {
$customConfig['platform.php'] = [$matches['version']];
}

// Update un-installed project with custom repository
if ($repository) {
$customConfig['repositories.temp'] = ['composer', $repository];
$customConfig['secure-http'] = ['false'];
}

return $customConfig;
}

/**
* Get all extra options to use with `composer create-project` when creating a project
*
* @param string $repository
* @param string $preferDist
* @return array
*/
protected static function getCreateOptions($repository, $preferDist)
{
// Create-options
$createOptions = [
'--no-secure-http',
'--no-interaction',
'--ignore-platform-reqs',
'--no-install',
];

// Set dev / stable options
if ($preferDist) {
$createOptions[] = "--prefer-dist";
$createOptions[] = "--no-dev";
} else {
$createOptions[] = "--prefer-source";
$createOptions[] = "--keep-vcs"; // create only
}

// Add repository
if ($repository) {
$createOptions[] = '--repository';
$createOptions[] = $repository;
}
return $createOptions;
}

/**
* Get all extra composer cli options to use with `composer update` when creating a project
*
* @param $preferDist
* @return array
*/
protected static function getUpdateOptions($preferDist)
{
// update options
$updateOptions = ["--no-interaction"];

// Set dev / stable options
if ($preferDist) {
$updateOptions[] = "--prefer-dist";
$updateOptions[] = "--no-dev";
} else {
$updateOptions[] = "--prefer-source";
}
return $updateOptions;
}
}

0 comments on commit c1ab90c

Please sign in to comment.