|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Consolidation\Cgr; |
|
4
|
|
|
|
|
5
|
|
|
class Application |
|
6
|
|
|
{ |
|
7
|
|
|
protected $outputFile = ''; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Run the cgr tool, a safer alternative to `composer global require`. |
|
11
|
|
|
* |
|
12
|
|
|
* @param array $argv The global $argv array passed in by PHP |
|
13
|
|
|
* @param string $home The path to the user's home directory |
|
14
|
|
|
* @return integer |
|
15
|
|
|
*/ |
|
16
|
|
|
public function run($argv, $home) |
|
17
|
|
|
{ |
|
18
|
|
|
$optionDefaultValues = $this->getDefaultOptionValues($home); |
|
19
|
|
|
$optionDefaultValues = $this->overlayEnvironmentValues($optionDefaultValues); |
|
20
|
|
|
|
|
21
|
|
|
list($argv, $options) = $this->parseOutOurOptions($argv, $optionDefaultValues); |
|
22
|
|
|
$commandList = $this->separateProjectAndGetCommandList($argv, $home, $options); |
|
23
|
|
|
if (empty($commandList)) { |
|
24
|
|
|
return 1; |
|
25
|
|
|
} |
|
26
|
|
|
return $this->runCommandList($commandList, $options); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Set up output redirection. Used by tests. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function setOutputFile($outputFile) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->outputFile = $outputFile; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Figure out everything we're going to do, but don't do any of it |
|
39
|
|
|
* yet, just return the command objects to run. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function parseArgvAndGetCommandList($argv, $home) |
|
42
|
|
|
{ |
|
43
|
|
|
$optionDefaultValues = $this->getDefaultOptionValues($home); |
|
44
|
|
|
$optionDefaultValues = $this->overlayEnvironmentValues($optionDefaultValues); |
|
45
|
|
|
|
|
46
|
|
|
list($argv, $options) = $this->parseOutOurOptions($argv, $optionDefaultValues); |
|
47
|
|
|
return $this->separateProjectAndGetCommandList($argv, $home, $options); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Figure out everything we're going to do, but don't do any of it |
|
52
|
|
|
* yet, just return the command objects to run. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function separateProjectAndGetCommandList($argv, $home, $options) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
list($command, $projects, $composerArgs) = $this->separateProjectsFromArgs($argv, $options); |
|
57
|
|
|
$commandList = $this->getCommandsToExec($command, $composerArgs, $projects, $options); |
|
58
|
|
|
return $commandList; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Run all of the commands in a list. Abort early if any fail. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $commandList An array of CommandToExec |
|
65
|
|
|
* @return integer |
|
66
|
|
|
*/ |
|
67
|
|
|
public function runCommandList($commandList, $options) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
foreach ($commandList as $command) { |
|
70
|
|
|
$exitCode = $command->run($this->outputFile); |
|
71
|
|
|
if ($exitCode) { |
|
72
|
|
|
return $exitCode; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
return 0; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Return an array containing a list of commands to execute. Depending on |
|
80
|
|
|
* the composition of the aguments and projects parameters, this list will |
|
81
|
|
|
* contain either a single command string to call through to composer (if |
|
82
|
|
|
* cgr is being used as a composer alias), or it will contain a list of |
|
83
|
|
|
* appropriate replacement 'composer global require' commands that install |
|
84
|
|
|
* each project in its own installation directory, while installing each |
|
85
|
|
|
* projects' binaries in the global Composer bin directory, |
|
86
|
|
|
* ~/.composer/vendor/bin. |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $composerArgs |
|
89
|
|
|
* @param array $projects |
|
90
|
|
|
* @param array $options |
|
91
|
|
|
* @return CommandToExec |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getCommandsToExec($command, $composerArgs, $projects, $options) |
|
94
|
|
|
{ |
|
95
|
|
|
$execPath = $options['composer-path']; |
|
96
|
|
|
// If command was not 'global require', 'global update' or |
|
97
|
|
|
// 'global remove', then call through to the standard composer |
|
98
|
|
|
// with all of the original args. |
|
99
|
|
|
if (empty($command)) { |
|
100
|
|
|
return array(new CommandToExec($execPath, $composerArgs)); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
// Call requireCommand, updateCommand, or removeCommand, as appropriate. |
|
103
|
|
|
$methodName = "{$command}Command"; |
|
104
|
|
|
if (method_exists($this, $methodName)) { |
|
105
|
|
|
return $this->$methodName($execPath, $composerArgs, $projects, $options); |
|
106
|
|
|
} else { |
|
107
|
|
|
// If there is no specific implementation for the requested command, then call 'generalCommand'. |
|
108
|
|
|
return $this->generalCommand($command, $execPath, $composerArgs, $projects, $options); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Return our list of default option values, with paths relative to |
|
114
|
|
|
* the provided home directory. |
|
115
|
|
|
* @param string $home The user's home directory |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getDefaultOptionValues($home) |
|
119
|
|
|
{ |
|
120
|
|
|
return array( |
|
121
|
|
|
'composer' => false, |
|
122
|
|
|
'composer-path' => 'composer', |
|
123
|
|
|
'base-dir' => "$home/.composer/global", |
|
124
|
|
|
'bin-dir' => "$home/.composer/vendor/bin", |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Replace option default values with the corresponding |
|
130
|
|
|
* environment variable value, if it is set. |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function overlayEnvironmentValues($defaults) |
|
133
|
|
|
{ |
|
134
|
|
|
foreach ($defaults as $key => $value) { |
|
135
|
|
|
$envKey = 'CGR_' . strtoupper(strtr($key, '-', '_')); |
|
136
|
|
|
$envValue = getenv($envKey); |
|
137
|
|
|
if ($envValue) { |
|
138
|
|
|
$defaults[$key] = $envValue; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $defaults; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* We use our own special-purpose argv parser. The options that apply |
|
147
|
|
|
* to this tool are identified by a simple associative array, where |
|
148
|
|
|
* the key is the option name, and the value is its default value. |
|
149
|
|
|
* The result of this function is an array of two items containing: |
|
150
|
|
|
* - An array of the items in $argv not used to set an option value |
|
151
|
|
|
* - An array of options containing the user-specified or default values |
|
152
|
|
|
* |
|
153
|
|
|
* @param array $argv The global $argv passed in by php |
|
154
|
|
|
* @param array $optionDefaultValues An associative array |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
public function parseOutOurOptions($argv, $optionDefaultValues) |
|
158
|
|
|
{ |
|
159
|
|
|
$argv0 = array_shift($argv); |
|
160
|
|
|
$options['composer'] = (strpos($argv0, 'composer') !== false); |
|
|
|
|
|
|
161
|
|
|
$passAlongArgvItems = array(); |
|
162
|
|
|
$options = array(); |
|
163
|
|
|
while (!empty($argv)) { |
|
164
|
|
|
$arg = array_shift($argv); |
|
165
|
|
|
if ((substr($arg, 0, 2) == '--') && array_key_exists(substr($arg, 2), $optionDefaultValues)) { |
|
166
|
|
|
$options[substr($arg, 2)] = array_shift($argv); |
|
167
|
|
|
} else { |
|
168
|
|
|
$passAlongArgvItems[] = $arg; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
return array($passAlongArgvItems, $options + $optionDefaultValues); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* After our options are removed by parseOutOurOptions, those items remaining |
|
176
|
|
|
* in $argv will be separated into a list of projects and versions, and |
|
177
|
|
|
* anything else that is not a project:version. Returns an array of two |
|
178
|
|
|
* items containing: |
|
179
|
|
|
* - An associative array, where the key is the project name and the value |
|
180
|
|
|
* is the version (or an empty string, if no version was specified) |
|
181
|
|
|
* - The remaining $argv items not used to build the projects array. |
|
182
|
|
|
* |
|
183
|
|
|
* @param array $argv The $argv array from parseOutOurOptions() |
|
184
|
|
|
* @return array |
|
185
|
|
|
*/ |
|
186
|
|
|
public function separateProjectsFromArgs($argv, $options) |
|
187
|
|
|
{ |
|
188
|
|
|
$cgrCommands = array('require', 'update', 'remove'); |
|
189
|
|
|
$command = 'require'; |
|
190
|
|
|
$composerArgs = array(); |
|
191
|
|
|
$projects = array(); |
|
192
|
|
|
$globalMode = !$options['composer']; |
|
193
|
|
|
foreach ($argv as $arg) { |
|
194
|
|
|
if ($arg[0] == '-') { |
|
195
|
|
|
// Any flags (first character is '-') will just be passed |
|
196
|
|
|
// through to to composer. Flags interpreted by cgr have |
|
197
|
|
|
// already been removed from $argv. |
|
198
|
|
|
$composerArgs[] = $arg; |
|
199
|
|
|
} elseif (strpos($arg, '/') !== false) { |
|
200
|
|
|
// Arguments containing a '/' name projects. We will split |
|
201
|
|
|
// the project from its version, allowing the separator |
|
202
|
|
|
// character to be either a '=' or a ':', and then store the |
|
203
|
|
|
// result in the $projects array. |
|
204
|
|
|
$projectAndVersion = explode(':', strtr($arg, '=', ':'), 2) + array('', ''); |
|
205
|
|
|
list($project, $version) = $projectAndVersion; |
|
206
|
|
|
$projects[$project] = $version; |
|
207
|
|
|
} elseif ($this->isComposerVersion($arg)) { |
|
208
|
|
|
// If an argument is a composer version, then we will alter |
|
209
|
|
|
// the last project we saw, attaching this version to it. |
|
210
|
|
|
// This allows us to handle 'a/b:1.0' and 'a/b 1.0' equivalently. |
|
211
|
|
|
$keys = array_keys($projects); |
|
212
|
|
|
$lastProject = array_pop($keys); |
|
213
|
|
|
unset($projects[$lastProject]); |
|
214
|
|
|
$projects[$lastProject] = $arg; |
|
215
|
|
|
} elseif ($arg == 'global') { |
|
216
|
|
|
// Make note if we see the 'global' command. |
|
217
|
|
|
$globalMode = true; |
|
218
|
|
|
} else { |
|
219
|
|
|
// If we see any command other than 'global [require|update|remove]', |
|
220
|
|
|
// then we will pass *all* of the arguments through to |
|
221
|
|
|
// composer unchanged. We return an empty projects array |
|
222
|
|
|
// to indicate that this should be a pass-through call |
|
223
|
|
|
// to composer, rather than one or more calls to |
|
224
|
|
|
// 'composer require' to install global projects. |
|
225
|
|
|
if ((!$globalMode) || (!in_array($arg, $cgrCommands))) { |
|
226
|
|
|
return array('', array(), $argv); |
|
227
|
|
|
} |
|
228
|
|
|
// Remember which command we saw |
|
229
|
|
|
$command = $arg; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
return array($command, $projects, $composerArgs); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Provide a safer version of `composer global require`. Each project |
|
237
|
|
|
* listed in $projects will be installed into its own project directory. |
|
238
|
|
|
* The binaries from each project will still be placed in the global |
|
239
|
|
|
* composer bin directory. |
|
240
|
|
|
* |
|
241
|
|
|
* @param string $composerCommand The composer command to run e.g. require |
|
242
|
|
|
* @param string $execPath The path to composer |
|
243
|
|
|
* @param array $composerArgs Anything from the global $argv to be passed |
|
244
|
|
|
* on to Composer |
|
245
|
|
|
* @param array $projects A list of projects to install, with the key |
|
246
|
|
|
* specifying the project name, and the value specifying its version. |
|
247
|
|
|
* @param array $options User options from the command line; see |
|
248
|
|
|
* $optionDefaultValues in the main() function. |
|
249
|
|
|
* @return array |
|
250
|
|
|
*/ |
|
251
|
|
|
public function generalCommand($composerCommand, $execPath, $composerArgs, $projects, $options) |
|
252
|
|
|
{ |
|
253
|
|
|
$globalBaseDir = $options['base-dir']; |
|
254
|
|
|
$binDir = $options['bin-dir']; |
|
255
|
|
|
$env = array("COMPOSER_BIN_DIR" => $binDir); |
|
256
|
|
|
$result = array(); |
|
257
|
|
|
foreach ($projects as $project => $version) { |
|
258
|
|
|
$installLocation = "$globalBaseDir/$project"; |
|
259
|
|
|
$projectWithVersion = $this->projectWithVersion($project, $version); |
|
260
|
|
|
$commandToExec = $this->buildGlobalCommand($composerCommand, $execPath, $composerArgs, $projectWithVersion, $env, $installLocation); |
|
261
|
|
|
$result[] = $commandToExec; |
|
262
|
|
|
} |
|
263
|
|
|
return $result; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Run `composer global update`. Not only do we want to update the |
|
268
|
|
|
* "global" Composer project, we also want to update all of the |
|
269
|
|
|
* "isolated" projects installed via cgr in ~/.composer/global. |
|
270
|
|
|
* |
|
271
|
|
|
* @param string $command The path to composer |
|
|
|
|
|
|
272
|
|
|
* @param array $composerArgs Anything from the global $argv to be passed |
|
273
|
|
|
* on to Composer |
|
274
|
|
|
* @param array $projects A list of projects to update. |
|
275
|
|
|
* @param array $options User options from the command line; see |
|
276
|
|
|
* $optionDefaultValues in the main() function. |
|
277
|
|
|
* @return array |
|
278
|
|
|
*/ |
|
279
|
|
|
public function updateCommand($execPath, $composerArgs, $projects, $options) |
|
280
|
|
|
{ |
|
281
|
|
|
// If 'projects' list is empty, make a list of everything currently installed |
|
282
|
|
|
if (empty($projects)) { |
|
283
|
|
|
$projects = FileSystemUtils::allInstalledProjectsInBaseDir($options['base-dir']); |
|
284
|
|
|
$projects = $this->flipProjectsArray($projects); |
|
285
|
|
|
} |
|
286
|
|
|
return $this->generalCommand('update', $execPath, $composerArgs, $projects, $options); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Convert from an array of projects to an array where the key is the |
|
291
|
|
|
* project name, and the value (version) is an empty string. |
|
292
|
|
|
* |
|
293
|
|
|
* @param string[] $projects |
|
294
|
|
|
* @return array |
|
295
|
|
|
*/ |
|
296
|
|
|
public function flipProjectsArray($projects) |
|
297
|
|
|
{ |
|
298
|
|
|
return array_map(function () { |
|
299
|
|
|
return ''; |
|
300
|
|
|
}, array_flip($projects)); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Return $project:$version, or just $project if there is no $version. |
|
305
|
|
|
* |
|
306
|
|
|
* @param string $project The project to install |
|
307
|
|
|
* @param string $version The version desired |
|
308
|
|
|
* @return string |
|
309
|
|
|
*/ |
|
310
|
|
|
public function projectWithVersion($project, $version) |
|
311
|
|
|
{ |
|
312
|
|
|
if (empty($version)) { |
|
313
|
|
|
return $project; |
|
314
|
|
|
} |
|
315
|
|
|
return "$project:$version"; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Generate command string to call `composer require` to install one project. |
|
320
|
|
|
* |
|
321
|
|
|
* @param string $command The path to composer |
|
|
|
|
|
|
322
|
|
|
* @param array $composerArgs The arguments to pass to composer |
|
323
|
|
|
* @param string $projectWithVersion The project:version to install |
|
324
|
|
|
* @param array $env Environment to set prior to exec |
|
325
|
|
|
* @param string $installLocation Location to install the project |
|
326
|
|
|
* @return CommandToExec |
|
327
|
|
|
*/ |
|
328
|
|
|
public function buildGlobalCommand($composerCommand, $execPath, $composerArgs, $projectWithVersion, $env, $installLocation) |
|
329
|
|
|
{ |
|
330
|
|
|
$projectSpecificArgs = array("--working-dir=$installLocation", $composerCommand, $projectWithVersion); |
|
331
|
|
|
$arguments = array_merge($composerArgs, $projectSpecificArgs); |
|
332
|
|
|
return new CommandToExec($execPath, $arguments, $env, $installLocation); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Identify an argument that could be a Composer version string. |
|
337
|
|
|
* |
|
338
|
|
|
* @param string $arg The argument to test |
|
339
|
|
|
* @return boolean |
|
340
|
|
|
*/ |
|
341
|
|
|
public function isComposerVersion($arg) |
|
342
|
|
|
{ |
|
343
|
|
|
$specialVersionChars = array('^', '~', '<', '>'); |
|
344
|
|
|
return is_numeric($arg[0]) || in_array($arg[0], $specialVersionChars); |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: