/
Request.php
263 lines (231 loc) · 8.21 KB
/
Request.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
<?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\DependencyResolver;
use Composer\Package\BasePackage;
use Composer\Package\PackageInterface;
use Composer\Repository\LockArrayRepository;
use Composer\Semver\Constraint\ConstraintInterface;
use Composer\Semver\Constraint\MatchAllConstraint;
/**
* @author Nils Adermann <naderman@naderman.de>
*/
class Request
{
/**
* Identifies a partial update for listed packages only, all dependencies will remain at locked versions
*/
public const UPDATE_ONLY_LISTED = 0;
/**
* Identifies a partial update for listed packages and recursively all their dependencies, however dependencies
* also directly required by the root composer.json and their dependencies will remain at the locked version.
*/
public const UPDATE_LISTED_WITH_TRANSITIVE_DEPS_NO_ROOT_REQUIRE = 1;
/**
* Identifies a partial update for listed packages and recursively all their dependencies, even dependencies
* also directly required by the root composer.json will be updated.
*/
public const UPDATE_LISTED_WITH_TRANSITIVE_DEPS = 2;
/** @var ?LockArrayRepository */
protected $lockedRepository;
/** @var array<string, ConstraintInterface> */
protected $requires = array();
/** @var array<string, BasePackage> */
protected $fixedPackages = array();
/** @var array<string, BasePackage> */
protected $lockedPackages = array();
/** @var array<string, BasePackage> */
protected $fixedLockedPackages = array();
/** @var string[] */
protected $updateAllowList = array();
/** @var false|self::UPDATE_* */
protected $updateAllowTransitiveDependencies = false;
public function __construct(LockArrayRepository $lockedRepository = null)
{
$this->lockedRepository = $lockedRepository;
}
/**
* @param string $packageName
* @return void
*/
public function requireName(string $packageName, ConstraintInterface $constraint = null): void
{
$packageName = strtolower($packageName);
if ($constraint === null) {
$constraint = new MatchAllConstraint();
}
if (isset($this->requires[$packageName])) {
throw new \LogicException('Overwriting requires seems like a bug ('.$packageName.' '.$this->requires[$packageName]->getPrettyString().' => '.$constraint->getPrettyString().', check why it is happening, might be a root alias');
}
$this->requires[$packageName] = $constraint;
}
/**
* Mark a package as currently present and having to remain installed
*
* This is used for platform packages which cannot be modified by Composer. A rule enforcing their installation is
* generated for dependency resolution. Partial updates with dependencies cannot in any way modify these packages.
*
* @return void
*/
public function fixPackage(BasePackage $package): void
{
$this->fixedPackages[spl_object_hash($package)] = $package;
}
/**
* Mark a package as locked to a specific version but removable
*
* This is used for lock file packages which need to be treated similar to fixed packages by the pool builder in
* that by default they should really only have the currently present version loaded and no remote alternatives.
*
* However unlike fixed packages there will not be a special rule enforcing their installation for the solver, so
* if nothing requires these packages they will be removed. Additionally in a partial update these packages can be
* unlocked, meaning other versions can be installed if explicitly requested as part of the update.
*
* @return void
*/
public function lockPackage(BasePackage $package): void
{
$this->lockedPackages[spl_object_hash($package)] = $package;
}
/**
* Marks a locked package fixed. So it's treated irremovable like a platform package.
*
* This is necessary for the composer install step which verifies the lock file integrity and should not allow
* removal of any packages. At the same time lock packages there cannot simply be marked fixed, as error reporting
* would then report them as platform packages, so this still marks them as locked packages at the same time.
*
* @return void
*/
public function fixLockedPackage(BasePackage $package): void
{
$this->fixedPackages[spl_object_hash($package)] = $package;
$this->fixedLockedPackages[spl_object_hash($package)] = $package;
}
/**
* @return void
*/
public function unlockPackage(BasePackage $package): void
{
unset($this->lockedPackages[spl_object_hash($package)]);
}
/**
* @param string[] $updateAllowList
* @param false|self::UPDATE_* $updateAllowTransitiveDependencies
* @return void
*/
public function setUpdateAllowList(array $updateAllowList, $updateAllowTransitiveDependencies): void
{
$this->updateAllowList = $updateAllowList;
$this->updateAllowTransitiveDependencies = $updateAllowTransitiveDependencies;
}
/**
* @return string[]
*/
public function getUpdateAllowList(): array
{
return $this->updateAllowList;
}
/**
* @return bool
*/
public function getUpdateAllowTransitiveDependencies(): bool
{
return $this->updateAllowTransitiveDependencies !== self::UPDATE_ONLY_LISTED;
}
/**
* @return bool
*/
public function getUpdateAllowTransitiveRootDependencies(): bool
{
return $this->updateAllowTransitiveDependencies === self::UPDATE_LISTED_WITH_TRANSITIVE_DEPS;
}
/**
* @return array<string, ConstraintInterface>
*/
public function getRequires(): array
{
return $this->requires;
}
/**
* @return array<string, BasePackage>
*/
public function getFixedPackages(): array
{
return $this->fixedPackages;
}
/**
* @return bool
*/
public function isFixedPackage(BasePackage $package): bool
{
return isset($this->fixedPackages[spl_object_hash($package)]);
}
/**
* @return array<string, BasePackage>
*/
public function getLockedPackages(): array
{
return $this->lockedPackages;
}
/**
* @return bool
*/
public function isLockedPackage(PackageInterface $package): bool
{
return isset($this->lockedPackages[spl_object_hash($package)]) || isset($this->fixedLockedPackages[spl_object_hash($package)]);
}
/**
* @return array<string, BasePackage>
*/
public function getFixedOrLockedPackages(): array
{
return array_merge($this->fixedPackages, $this->lockedPackages);
}
/**
* @param bool $packageIds
* @return array<int|string, BasePackage>
*
* @TODO look into removing the packageIds option, the only place true is used
* is for the installed map in the solver problems.
* Some locked packages may not be in the pool,
* so they have a package->id of -1
*/
public function getPresentMap(bool $packageIds = false): array
{
$presentMap = array();
if ($this->lockedRepository) {
foreach ($this->lockedRepository->getPackages() as $package) {
$presentMap[$packageIds ? $package->getId() : spl_object_hash($package)] = $package;
}
}
foreach ($this->fixedPackages as $package) {
$presentMap[$packageIds ? $package->getId() : spl_object_hash($package)] = $package;
}
return $presentMap;
}
/**
* @return BasePackage[]
*/
public function getFixedPackagesMap(): array
{
$fixedPackagesMap = array();
foreach ($this->fixedPackages as $package) {
$fixedPackagesMap[$package->getId()] = $package;
}
return $fixedPackagesMap;
}
/**
* @return ?LockArrayRepository
*/
public function getLockedRepository(): ?LockArrayRepository
{
return $this->lockedRepository;
}
}