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