-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathComposer.php
More file actions
402 lines (342 loc) · 9.87 KB
/
Composer.php
File metadata and controls
402 lines (342 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
namespace Statamic\Console\Processes;
use Illuminate\Support\Facades\Cache;
use Statamic\Console\Composer\Lock;
use Statamic\Jobs\RunComposer;
use Statamic\Support\Str;
use Statamic\View\Antlers\Language\Utilities\StringUtilities;
class Composer extends Process
{
protected $memoryLimit;
protected $withoutQueue = false;
/**
* Instantiate composer process.
*
* @param mixed $basePath
*/
public function __construct($basePath = null)
{
parent::__construct($basePath);
// Set this process to eleven.
$this->toEleven();
// Set memory limit for child process to eleven.
$this->memoryLimit = config('statamic.system.php_memory_limit');
}
/**
* Run without queue.
*
* @return $this
*/
public function withoutQueue()
{
$this->withoutQueue = true;
return $this;
}
/**
* Check if specific package is installed.
*
* @return bool
*/
public function isInstalled(string $package)
{
return Lock::file($this->basePath.'composer.lock')->isPackageInstalled($package);
}
/**
* List installed packages (including dev dependencies).
*
* @return \Illuminate\Support\Collection
*/
public function installed()
{
$lock = Lock::file($this->basePath.'composer.lock');
if (! $lock->exists()) {
return collect();
}
return collect($this->runJsonComposerCommand('show', '--direct', '--no-plugins')?->installed ?? [])
->keyBy('name')
->map(function ($package) use ($lock) {
$package->version = $this->normalizeVersion($package->version);
$package->dev = $lock->isDevPackageInstalled($package->name);
return $package;
});
}
/**
* Get installed version of a specific package.
*
* @return string
*/
public function installedVersion(string $package)
{
$lock = Lock::file($this->basePath.'composer.lock');
if (! $lock->exists()) {
return null;
}
$version = $lock->getInstalledVersion($package);
return $this->normalizeVersion($version);
}
/**
* Get installed path of a specific package.
*
* @return string
*/
public function installedPath(string $package)
{
return collect($this->runJsonComposerCommand('show', '--direct', '--path', '--no-plugins')?->installed ?? [])
->keyBy('name')
->get($package)
->path;
}
/**
* Require a package.
*
* @param mixed $extraParams
*/
public function require(string $package, ?string $version = null, ...$extraParams)
{
if ($version) {
$parts[] = $version;
}
$parts[] = '--update-with-dependencies';
$parts = array_merge($parts, $extraParams);
$this->queueComposerCommand('require', $package, ...$parts);
}
/**
* Require a dev package.
*/
public function requireDev(string $package, ?string $version = null, ...$extraParams)
{
$this->require($package, $version, '--dev', ...$extraParams);
}
/**
* Require multiple packages at once.
*
* @param mixed $extraParams
*/
public function requireMultiple(array $packages, ...$extraParams)
{
$parts = array_merge($this->normalizePackagesArrayToRequireArgs($packages), $extraParams);
$parts[] = '--update-with-dependencies';
$this->queueComposerCommand('require', ...$parts);
}
/**
* Require multiple dev packages at once.
*
* @param mixed $extraParams
*/
public function requireMultipleDev(array $packages, ...$extraParams)
{
$this->requireMultiple($packages, '--dev', ...$extraParams);
}
/**
* Remove a package.
*
* @param mixed $extraParams
*/
public function remove(string $package, ...$extraParams)
{
$this->queueComposerCommand('remove', $package, ...$extraParams);
}
/**
* Remove a dev package.
*
* @param mixed $extraParams
*/
public function removeDev(string $package, ...$extraParams)
{
$this->remove($package, '--dev', ...$extraParams);
}
/**
* Remove multiple packages at once.
*
* @param mixed $extraParams
*/
public function removeMultiple(array $packages, ...$extraParams)
{
$parts = array_merge($packages, $extraParams);
$this->queueComposerCommand('remove', ...$parts);
}
/**
* Remove multiple dev packages at once.
*
* @param mixed $extraParams
*/
public function removeMultipleDev(array $packages, ...$extraParams)
{
$this->removeMultiple($packages, '--dev', ...$extraParams);
}
/**
* Update a package.
*/
public function update(string $package)
{
$this->queueComposerCommand('update', $package, '--with-dependencies');
}
/**
* Install Composer dependencies.
*
* @return void
*/
public function install(string $workingDirectory)
{
$this->runComposerCommand('install', '--working-dir='.$workingDirectory);
}
/**
* Get cached output for package process.
*
* @return mixed
*/
public function cachedOutput(string $package)
{
return parent::cachedOutput($this->getCacheKey($package));
}
/**
* Get cached output for last completed package process.
*
* @return mixed
*/
public function lastCompletedCachedOutput(string $package)
{
return parent::lastCompletedCachedOutput($this->getCacheKey($package));
}
/**
* Clear cached output.
*/
public function clearCachedOutput(string $package)
{
Cache::forget($this->getCacheKey($package));
}
/**
* Run composer and externally operate on ouput.
*
* @param mixed $command
* @param mixed $operateOnOutput
* @return string
*/
public function runAndOperateOnOutput($command, $operateOnOutput)
{
$command = $this->prepareProcessArguments($command);
return parent::runAndOperateOnOutput($command, $operateOnOutput);
}
/**
* Run composer command.
*
* @param mixed $parts
* @return mixed
*/
public function runComposerCommand(...$parts)
{
return $this->run($this->prepareProcessArguments($parts));
}
/**
* Run json composer command.
*
* @param mixed $parts
* @return string
*/
private function runJsonComposerCommand(...$parts)
{
$output = $this->runComposerCommand(...array_merge($parts, ['--format=json']));
// Strip out php8 deprecation warnings
$json = substr($output, strpos($output, "\n{"));
return json_decode($json);
}
/**
* Queue composer command.
*
* @param string $command
* @param string $package
* @param mixed $extraParams
*/
private function queueComposerCommand($command, $package, ...$extraParams)
{
if ($this->withoutQueue) {
return $this->runComposerCommand($command, $package, ...$extraParams);
}
$parts = array_merge([$command, $package], $extraParams);
dispatch(new RunComposer($this->prepareProcessArguments($parts), $this->getCacheKey($package)));
}
/**
* Prepare process arguments.
*
* @param array $parts
* @return array
*/
private function prepareProcessArguments($parts)
{
return array_merge([
$this->phpBinary(),
"-d memory_limit={$this->memoryLimit}",
$this->composerBinary(),
$this->colorized ? '--ansi' : '--no-ansi',
], $parts);
}
/**
* Absolute path to the Composer binary.
*/
private function composerBinary(): string
{
$isWindows = $this->isWindows();
$output = $this->run($isWindows ? 'where composer' : 'which composer');
if ($isWindows) {
return $this->locateComposerPharOnWindows($output);
}
return $output;
}
private function locateComposerPharOnWindows($output): string
{
$output = StringUtilities::normalizeLineEndings($output);
if (! Str::contains($output, "\n")) {
$candidates = [trim($output)];
} else {
$candidates = explode("\n", $output);
}
foreach ($candidates as $candidate) {
// Do we have a bat file? The phar is likely beside it.
if (Str::endsWith($candidate, '.bat')) {
// Remove that 🦇 extension.
$candidate = mb_substr($candidate, 0, mb_strlen($candidate) - 4);
}
$pharPath = $candidate.'.phar';
if (file_exists($pharPath)) {
// Use "composer.phar" if we have it.
return $pharPath;
}
}
return $output;
}
/**
* Sometimes composer returns versions with a 'v', sometimes it doesn't.
*
* @return string
*/
private function normalizeVersion(string $version)
{
return ltrim($version, 'v');
}
/**
* Get cache key for composer output storage.
*
* @param string $package
* @return string
*/
private function getCacheKey($package)
{
return "composer.{$package}";
}
/**
* Normalize packages array to require args, with version handling if `package => version` array structure is passed.
*
* @return array
*/
private function normalizePackagesArrayToRequireArgs(array $packages)
{
return collect($packages)
->map(function ($value, $key) {
return Str::contains($key, '/')
? "{$key}:{$value}"
: "{$value}";
})
->values()
->all();
}
}