Skip to content

Commit c1ab90c

Browse files
Damian MooymanAaron Carlino
Damian Mooyman
authored and
Aaron Carlino
committed
ENHANCEMENT New projects will set config.platform.php to that specified by root requirements (#66)
Fixes silverstripe/silverstripe-framework#7709
1 parent 126366d commit c1ab90c

File tree

1 file changed

+106
-76
lines changed

1 file changed

+106
-76
lines changed

src/Utility/Composer.php

+106-76
Original file line numberDiff line numberDiff line change
@@ -45,96 +45,42 @@ public static function createProject(
4545
$repository = null,
4646
$preferDist = false
4747
) {
48-
// Create-options
49-
$createOptions = [
50-
"--no-secure-http",
51-
"--no-interaction",
52-
"--ignore-platform-reqs",
53-
];
54-
55-
// Set dev / stable options
56-
if ($preferDist) {
57-
$createOptions[] = "--prefer-dist";
58-
$createOptions[] = "--no-dev";
59-
} else {
60-
$createOptions[] = "--prefer-source";
61-
$createOptions[] = "--keep-vcs"; // create only
62-
}
63-
64-
// If using a repository, we must delay for a later update
65-
if ($repository) {
66-
$createOptions[] = '--repository';
67-
$createOptions[] = $repository;
68-
$createOptions[] = '--no-install';
69-
}
70-
7148
// Create comand
49+
$createOptions = self::getCreateOptions($repository, $preferDist);
7250
$runner->runCommand(array_merge([
7351
"composer",
7452
"create-project",
7553
$recipe,
7654
$directory,
77-
$version
55+
$version,
7856
], $createOptions), "Could not create project with version {$version}");
7957

80-
// Update un-installed project with custom repository
81-
if ($repository) {
82-
// Add repository temporarily
83-
$runner->runCommand([
84-
'composer',
85-
'config',
86-
'repositories.temp',
87-
'composer',
88-
$repository,
89-
'--working-dir',
90-
$directory,
91-
]);
92-
// Enable http:// local repositories
93-
$runner->runCommand([
94-
'composer',
95-
'config',
96-
'secure-http',
97-
'false',
98-
'--working-dir',
99-
$directory,
100-
]);
58+
// Set composer config
59+
$customConfig = self::getUpdateConfig($directory, $repository);
60+
foreach ($customConfig as $option => $arguments) {
61+
$runner->runCommand(array_merge(
62+
['composer', 'config', $option],
63+
$arguments,
64+
['--working-dir', $directory]
65+
));
66+
}
10167

102-
// update options
103-
$updateOptions = [
104-
"--no-interaction",
105-
"--ignore-platform-reqs",
106-
];
107-
108-
// Set dev / stable options
109-
if ($preferDist) {
110-
$updateOptions[] = "--prefer-dist";
111-
$updateOptions[] = "--no-dev";
112-
} else {
113-
$updateOptions[] = "--prefer-source";
114-
}
115-
116-
// Update with the given repository
117-
$runner->runCommand(array_merge([
118-
'composer',
119-
'update',
120-
'--working-dir',
121-
$directory,
122-
], $updateOptions), "Could not update project");
68+
// Update with the given repository
69+
$updateOptions = self::getUpdateOptions($preferDist);
70+
$runner->runCommand(array_merge([
71+
'composer',
72+
'update',
73+
'--working-dir',
74+
$directory,
75+
], $updateOptions), "Could not update project");
12376

124-
// Revert changes made above
77+
// Revert all custom config
78+
foreach ($customConfig as $option => $arguments) {
12579
$runner->runCommand([
12680
'composer',
12781
'config',
12882
'--unset',
129-
'repositories.temp',
130-
'--working-dir',
131-
$directory,
132-
]);
133-
$runner->runCommand([
134-
'composer',
135-
'config',
136-
'--unset',
137-
'secure-http',
83+
$option,
13884
'--working-dir',
13985
$directory,
14086
]);
@@ -155,4 +101,88 @@ public static function getOAUTHToken(CommandRunner $runner)
155101
$result = $runner->runCommand($command, $error);
156102
return trim($result);
157103
}
104+
105+
/**
106+
* Get list of custom config to use for `composer update` when creating a project
107+
*
108+
* @param string $directory
109+
* @param string $repository
110+
* @return array
111+
*/
112+
protected static function getUpdateConfig($directory, $repository)
113+
{
114+
// Register all custom options to temporarily set
115+
$customConfig = [];
116+
117+
// If `requirements.php` is specified, set platform to lowest platform version
118+
$composerData = Config::loadFromFile($directory . '/composer.json');
119+
if (isset($composerData['require']['php'])
120+
&& preg_match('/^[\\D]*(?<version>[\\d.]+)/', $composerData['require']['php'], $matches)
121+
) {
122+
$customConfig['platform.php'] = [$matches['version']];
123+
}
124+
125+
// Update un-installed project with custom repository
126+
if ($repository) {
127+
$customConfig['repositories.temp'] = ['composer', $repository];
128+
$customConfig['secure-http'] = ['false'];
129+
}
130+
131+
return $customConfig;
132+
}
133+
134+
/**
135+
* Get all extra options to use with `composer create-project` when creating a project
136+
*
137+
* @param string $repository
138+
* @param string $preferDist
139+
* @return array
140+
*/
141+
protected static function getCreateOptions($repository, $preferDist)
142+
{
143+
// Create-options
144+
$createOptions = [
145+
'--no-secure-http',
146+
'--no-interaction',
147+
'--ignore-platform-reqs',
148+
'--no-install',
149+
];
150+
151+
// Set dev / stable options
152+
if ($preferDist) {
153+
$createOptions[] = "--prefer-dist";
154+
$createOptions[] = "--no-dev";
155+
} else {
156+
$createOptions[] = "--prefer-source";
157+
$createOptions[] = "--keep-vcs"; // create only
158+
}
159+
160+
// Add repository
161+
if ($repository) {
162+
$createOptions[] = '--repository';
163+
$createOptions[] = $repository;
164+
}
165+
return $createOptions;
166+
}
167+
168+
/**
169+
* Get all extra composer cli options to use with `composer update` when creating a project
170+
*
171+
* @param $preferDist
172+
* @return array
173+
*/
174+
protected static function getUpdateOptions($preferDist)
175+
{
176+
// update options
177+
$updateOptions = ["--no-interaction"];
178+
179+
// Set dev / stable options
180+
if ($preferDist) {
181+
$updateOptions[] = "--prefer-dist";
182+
$updateOptions[] = "--no-dev";
183+
} else {
184+
$updateOptions[] = "--prefer-source";
185+
}
186+
return $updateOptions;
187+
}
158188
}

0 commit comments

Comments
 (0)