/
Locker.php
511 lines (437 loc) · 17.1 KB
/
Locker.php
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Package;
use Composer\Json\JsonFile;
use Composer\Installer\InstallationManager;
use Composer\Pcre\Preg;
use Composer\Repository\LockArrayRepository;
use Composer\Util\ProcessExecutor;
use Composer\Package\Dumper\ArrayDumper;
use Composer\Package\Loader\ArrayLoader;
use Composer\Package\Version\VersionParser;
use Composer\Plugin\PluginInterface;
use Composer\Util\Git as GitUtil;
use Composer\IO\IOInterface;
use Seld\JsonLint\ParsingException;
/**
* Reads/writes project lockfile (composer.lock).
*
* @author Konstantin Kudryashiv <ever.zet@gmail.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class Locker
{
/** @var JsonFile */
private $lockFile;
/** @var InstallationManager */
private $installationManager;
/** @var string */
private $hash;
/** @var string */
private $contentHash;
/** @var ArrayLoader */
private $loader;
/** @var ArrayDumper */
private $dumper;
/** @var ProcessExecutor */
private $process;
/** @var mixed[]|null */
private $lockDataCache = null;
/** @var bool */
private $virtualFileWritten = false;
/**
* Initializes packages locker.
*
* @param IOInterface $io
* @param JsonFile $lockFile lockfile loader
* @param InstallationManager $installationManager installation manager instance
* @param string $composerFileContents The contents of the composer file
*/
public function __construct(IOInterface $io, JsonFile $lockFile, InstallationManager $installationManager, $composerFileContents, ProcessExecutor $process = null)
{
$this->lockFile = $lockFile;
$this->installationManager = $installationManager;
$this->hash = md5($composerFileContents);
$this->contentHash = self::getContentHash($composerFileContents);
$this->loader = new ArrayLoader(null, true);
$this->dumper = new ArrayDumper();
$this->process = $process ?: new ProcessExecutor($io);
}
/**
* Returns the md5 hash of the sorted content of the composer file.
*
* @param string $composerFileContents The contents of the composer file.
*
* @return string
*/
public static function getContentHash($composerFileContents)
{
$content = JsonFile::parseJson($composerFileContents, 'composer.json');
$relevantKeys = array(
'name',
'version',
'require',
'require-dev',
'conflict',
'replace',
'provide',
'minimum-stability',
'prefer-stable',
'repositories',
'extra',
);
$relevantContent = array();
foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
$relevantContent[$key] = $content[$key];
}
if (isset($content['config']['platform'])) {
$relevantContent['config']['platform'] = $content['config']['platform'];
}
ksort($relevantContent);
return md5(JsonFile::encode($relevantContent, 0));
}
/**
* Checks whether locker has been locked (lockfile found).
*
* @return bool
*/
public function isLocked()
{
if (!$this->virtualFileWritten && !$this->lockFile->exists()) {
return false;
}
$data = $this->getLockData();
return isset($data['packages']);
}
/**
* Checks whether the lock file is still up to date with the current hash
*
* @return bool
*/
public function isFresh()
{
$lock = $this->lockFile->read();
if (!empty($lock['content-hash'])) {
// There is a content hash key, use that instead of the file hash
return $this->contentHash === $lock['content-hash'];
}
// BC support for old lock files without content-hash
if (!empty($lock['hash'])) {
return $this->hash === $lock['hash'];
}
// should not be reached unless the lock file is corrupted, so assume it's out of date
return false;
}
/**
* Searches and returns an array of locked packages, retrieved from registered repositories.
*
* @param bool $withDevReqs true to retrieve the locked dev packages
* @throws \RuntimeException
* @return \Composer\Repository\LockArrayRepository
*/
public function getLockedRepository($withDevReqs = false)
{
$lockData = $this->getLockData();
$packages = new LockArrayRepository();
$lockedPackages = $lockData['packages'];
if ($withDevReqs) {
if (isset($lockData['packages-dev'])) {
$lockedPackages = array_merge($lockedPackages, $lockData['packages-dev']);
} else {
throw new \RuntimeException('The lock file does not contain require-dev information, run install with the --no-dev option or delete it and run composer update to generate a new lock file.');
}
}
if (empty($lockedPackages)) {
return $packages;
}
if (isset($lockedPackages[0]['name'])) {
$packageByName = array();
foreach ($lockedPackages as $info) {
$package = $this->loader->load($info);
$packages->addPackage($package);
$packageByName[$package->getName()] = $package;
if ($package instanceof AliasPackage) {
$packageByName[$package->getAliasOf()->getName()] = $package->getAliasOf();
}
}
if (isset($lockData['aliases'])) {
foreach ($lockData['aliases'] as $alias) {
if (isset($packageByName[$alias['package']])) {
$aliasPkg = new CompleteAliasPackage($packageByName[$alias['package']], $alias['alias_normalized'], $alias['alias']);
$aliasPkg->setRootPackageAlias(true);
$packages->addPackage($aliasPkg);
}
}
}
return $packages;
}
throw new \RuntimeException('Your composer.lock is invalid. Run "composer update" to generate a new one.');
}
/**
* @return string[] Names of dependencies installed through require-dev
*/
public function getDevPackageNames()
{
$names = array();
$lockData = $this->getLockData();
if (isset($lockData['packages-dev'])) {
foreach ($lockData['packages-dev'] as $package) {
$names[] = strtolower($package['name']);
}
}
return $names;
}
/**
* Returns the platform requirements stored in the lock file
*
* @param bool $withDevReqs if true, the platform requirements from the require-dev block are also returned
* @return \Composer\Package\Link[]
*/
public function getPlatformRequirements($withDevReqs = false)
{
$lockData = $this->getLockData();
$requirements = array();
if (!empty($lockData['platform'])) {
$requirements = $this->loader->parseLinks(
'__root__',
'1.0.0',
Link::TYPE_REQUIRE,
isset($lockData['platform']) ? $lockData['platform'] : array()
);
}
if ($withDevReqs && !empty($lockData['platform-dev'])) {
$devRequirements = $this->loader->parseLinks(
'__root__',
'1.0.0',
Link::TYPE_REQUIRE,
isset($lockData['platform-dev']) ? $lockData['platform-dev'] : array()
);
$requirements = array_merge($requirements, $devRequirements);
}
return $requirements;
}
/**
* @return string
*/
public function getMinimumStability()
{
$lockData = $this->getLockData();
return isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : 'stable';
}
/**
* @return array<string, string>
*/
public function getStabilityFlags()
{
$lockData = $this->getLockData();
return isset($lockData['stability-flags']) ? $lockData['stability-flags'] : array();
}
/**
* @return bool|null
*/
public function getPreferStable()
{
$lockData = $this->getLockData();
// return null if not set to allow caller logic to choose the
// right behavior since old lock files have no prefer-stable
return isset($lockData['prefer-stable']) ? $lockData['prefer-stable'] : null;
}
/**
* @return bool|null
*/
public function getPreferLowest()
{
$lockData = $this->getLockData();
// return null if not set to allow caller logic to choose the
// right behavior since old lock files have no prefer-lowest
return isset($lockData['prefer-lowest']) ? $lockData['prefer-lowest'] : null;
}
/**
* @return array<string, string>
*/
public function getPlatformOverrides()
{
$lockData = $this->getLockData();
return isset($lockData['platform-overrides']) ? $lockData['platform-overrides'] : array();
}
/**
* @return string[][]
*
* @phpstan-return list<array{package: string, version: string, alias: string, alias_normalized: string}>
*/
public function getAliases()
{
$lockData = $this->getLockData();
return isset($lockData['aliases']) ? $lockData['aliases'] : array();
}
/**
* @return array<string, mixed>
*/
public function getLockData()
{
if (null !== $this->lockDataCache) {
return $this->lockDataCache;
}
if (!$this->lockFile->exists()) {
throw new \LogicException('No lockfile found. Unable to read locked packages');
}
return $this->lockDataCache = $this->lockFile->read();
}
/**
* Locks provided data into lockfile.
*
* @param PackageInterface[] $packages array of packages
* @param PackageInterface[]|null $devPackages array of dev packages or null if installed without --dev
* @param array<string, string> $platformReqs array of package name => constraint for required platform packages
* @param array<string, string> $platformDevReqs array of package name => constraint for dev-required platform packages
* @param string[][] $aliases array of aliases
* @param string $minimumStability
* @param array<string, int> $stabilityFlags
* @param bool $preferStable
* @param bool $preferLowest
* @param array<string, string> $platformOverrides
* @param bool $write Whether to actually write data to disk, useful in tests and for --dry-run
*
* @return bool
*
* @phpstan-param list<array{package: string, version: string, alias: string, alias_normalized: string}> $aliases
*/
public function setLockData(array $packages, $devPackages, array $platformReqs, $platformDevReqs, array $aliases, $minimumStability, array $stabilityFlags, $preferStable, $preferLowest, array $platformOverrides, $write = true)
{
// keep old default branch names normalized to DEFAULT_BRANCH_ALIAS for BC as that is how Composer 1 outputs the lock file
// when loading the lock file the version is anyway ignored in Composer 2, so it has no adverse effect
$aliases = array_map(function ($alias) {
if (in_array($alias['version'], array('dev-master', 'dev-trunk', 'dev-default'), true)) {
$alias['version'] = VersionParser::DEFAULT_BRANCH_ALIAS;
}
return $alias;
}, $aliases);
$lock = array(
'_readme' => array('This file locks the dependencies of your project to a known state',
'Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies',
'This file is @gener'.'ated automatically', ),
'content-hash' => $this->contentHash,
'packages' => null,
'packages-dev' => null,
'aliases' => $aliases,
'minimum-stability' => $minimumStability,
'stability-flags' => $stabilityFlags,
'prefer-stable' => $preferStable,
'prefer-lowest' => $preferLowest,
);
$lock['packages'] = $this->lockPackages($packages);
if (null !== $devPackages) {
$lock['packages-dev'] = $this->lockPackages($devPackages);
}
$lock['platform'] = $platformReqs;
$lock['platform-dev'] = $platformDevReqs;
if ($platformOverrides) {
$lock['platform-overrides'] = $platformOverrides;
}
$lock['plugin-api-version'] = PluginInterface::PLUGIN_API_VERSION;
try {
$isLocked = $this->isLocked();
} catch (ParsingException $e) {
$isLocked = false;
}
if (!$isLocked || $lock !== $this->getLockData()) {
if ($write) {
$this->lockFile->write($lock);
$this->lockDataCache = null;
$this->virtualFileWritten = false;
} else {
$this->virtualFileWritten = true;
$this->lockDataCache = JsonFile::parseJson(JsonFile::encode($lock, 448 & JsonFile::JSON_PRETTY_PRINT));
}
return true;
}
return false;
}
/**
* @param PackageInterface[] $packages
*
* @return mixed[][]
*
* @phpstan-return list<array<string, mixed>>
*/
private function lockPackages(array $packages)
{
$locked = array();
foreach ($packages as $package) {
if ($package instanceof AliasPackage) {
continue;
}
$name = $package->getPrettyName();
$version = $package->getPrettyVersion();
if (!$name || !$version) {
throw new \LogicException(sprintf(
'Package "%s" has no version or name and can not be locked',
$package
));
}
$spec = $this->dumper->dump($package);
unset($spec['version_normalized']);
// always move time to the end of the package definition
$time = isset($spec['time']) ? $spec['time'] : null;
unset($spec['time']);
if ($package->isDev() && $package->getInstallationSource() === 'source') {
// use the exact commit time of the current reference if it's a dev package
$time = $this->getPackageTime($package) ?: $time;
}
if (null !== $time) {
$spec['time'] = $time;
}
unset($spec['installation-source']);
$locked[] = $spec;
}
usort($locked, function ($a, $b) {
$comparison = strcmp($a['name'], $b['name']);
if (0 !== $comparison) {
return $comparison;
}
// If it is the same package, compare the versions to make the order deterministic
return strcmp($a['version'], $b['version']);
});
return $locked;
}
/**
* Returns the packages's datetime for its source reference.
*
* @param PackageInterface $package The package to scan.
* @return string|null The formatted datetime or null if none was found.
*/
private function getPackageTime(PackageInterface $package)
{
if (!function_exists('proc_open')) {
return null;
}
$path = realpath($this->installationManager->getInstallPath($package));
$sourceType = $package->getSourceType();
$datetime = null;
if ($path && in_array($sourceType, array('git', 'hg'))) {
$sourceRef = $package->getSourceReference() ?: $package->getDistReference();
switch ($sourceType) {
case 'git':
GitUtil::cleanEnv();
if (0 === $this->process->execute('git log -n1 --pretty=%ct '.ProcessExecutor::escape($sourceRef).GitUtil::getNoShowSignatureFlag($this->process), $output, $path) && Preg::isMatch('{^\s*\d+\s*$}', $output)) {
$datetime = new \DateTime('@'.trim($output), new \DateTimeZone('UTC'));
}
break;
case 'hg':
if (0 === $this->process->execute('hg log --template "{date|hgdate}" -r '.ProcessExecutor::escape($sourceRef), $output, $path) && Preg::isMatch('{^\s*(\d+)\s*}', $output, $match)) {
$datetime = new \DateTime('@'.$match[1], new \DateTimeZone('UTC'));
}
break;
}
}
return $datetime ? $datetime->format(DATE_RFC3339) : null;
}
}