/
Locker.php
154 lines (132 loc) · 4.36 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
<?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\Repository\RepositoryManager;
use Composer\Package\AliasPackage;
/**
* Reads/writes project lockfile (composer.lock).
*
* @author Konstantin Kudryashiv <ever.zet@gmail.com>
*/
class Locker
{
private $lockFile;
private $repositoryManager;
private $hash;
/**
* Initializes packages locker.
*
* @param JsonFile $lockFile lockfile loader
* @param RepositoryManager $repositoryManager repository manager instance
* @param string $hash unique hash of the current composer configuration
*/
public function __construct(JsonFile $lockFile, RepositoryManager $repositoryManager, $hash)
{
$this->lockFile = $lockFile;
$this->repositoryManager = $repositoryManager;
$this->hash = $hash;
}
/**
* Checks whether locker were been locked (lockfile found).
*
* @return Boolean
*/
public function isLocked()
{
return $this->lockFile->exists();
}
/**
* Checks whether the lock file is still up to date with the current hash
*
* @return Boolean
*/
public function isFresh()
{
$lock = $this->lockFile->read();
return $this->hash === $lock['hash'];
}
/**
* Searches and returns an array of locked packages, retrieved from registered repositories.
*
* @return array
*/
public function getLockedPackages()
{
$lockList = $this->getLockData();
$packages = array();
foreach ($lockList['packages'] as $info) {
$resolvedVersion = !empty($info['alias']) ? $info['alias'] : $info['version'];
$package = $this->repositoryManager->getLocalRepository()->findPackage($info['package'], $resolvedVersion);
if (!$package) {
$package = $this->repositoryManager->findPackage($info['package'], $info['version']);
if ($package && !empty($info['alias'])) {
$package = new AliasPackage($package, $info['alias'], $info['alias']);
}
}
if (!$package) {
throw new \LogicException(sprintf(
'Can not find "%s-%s" package in registered repositories',
$info['package'], $info['version']
));
}
$packages[] = $package;
}
return $packages;
}
public function getAliases()
{
$lockList = $this->getLockData();
return isset($lockList['aliases']) ? $lockList['aliases'] : array();
}
public function getLockData()
{
if (!$this->isLocked()) {
throw new \LogicException('No lockfile found. Unable to read locked packages');
}
return $this->lockFile->read();
}
/**
* Locks provided data into lockfile.
*
* @param array $packages array of packages
* @param array $aliases array of aliases
*/
public function setLockData(array $packages, array $aliases)
{
$lock = array(
'hash' => $this->hash,
'packages' => array(),
'aliases' => $aliases,
);
foreach ($packages as $package) {
$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 = array('package' => $name, 'version' => $version);
if ($package->isDev()) {
$spec['source-reference'] = $package->getSourceReference();
}
if ($package->getAlias() && $package->isInstalledAsAlias()) {
$spec['alias'] = $package->getAlias();
}
$lock['packages'][] = $spec;
}
usort($lock['packages'], function ($a, $b) {
return strcmp($a['package'], $b['package']);
});
$this->lockFile->write($lock);
}
}