/
Pool.php
210 lines (174 loc) · 6.1 KB
/
Pool.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
<?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\Version\VersionParser;
use Composer\Semver\CompilingMatcher;
use Composer\Semver\Constraint\ConstraintInterface;
use Composer\Semver\Constraint\Constraint;
use Composer\Package\BasePackage;
/**
* A package pool contains all packages for dependency resolution
*
* @author Nils Adermann <naderman@naderman.de>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class Pool implements \Countable
{
/** @var BasePackage[] */
protected $packages = array();
/** @var array<string, BasePackage[]> */
protected $packageByName = array();
protected $versionParser;
protected $providerCache = array();
/** @var BasePackage[] */
protected $unacceptableFixedOrLockedPackages;
public function __construct(array $packages = array(), array $unacceptableFixedOrLockedPackages = array())
{
$this->versionParser = new VersionParser;
$this->setPackages($packages);
$this->unacceptableFixedOrLockedPackages = $unacceptableFixedOrLockedPackages;
}
private function setPackages(array $packages)
{
$id = 1;
foreach ($packages as $package) {
$this->packages[] = $package;
$package->id = $id++;
foreach ($package->getNames() as $provided) {
$this->packageByName[$provided][] = $package;
}
}
}
/**
* @return BasePackage[]
*/
public function getPackages()
{
return $this->packages;
}
/**
* Retrieves the package object for a given package id.
*
* @param int $id
* @return BasePackage
*/
public function packageById($id)
{
return $this->packages[$id - 1];
}
/**
* Returns how many packages have been loaded into the pool
*/
#[\ReturnTypeWillChange]
public function count()
{
return \count($this->packages);
}
/**
* Searches all packages providing the given package name and match the constraint
*
* @param string $name The package name to be searched for
* @param ConstraintInterface $constraint A constraint that all returned
* packages must match or null to return all
* @return BasePackage[] A set of packages
*/
public function whatProvides($name, ConstraintInterface $constraint = null)
{
$key = (string) $constraint;
if (isset($this->providerCache[$name][$key])) {
return $this->providerCache[$name][$key];
}
return $this->providerCache[$name][$key] = $this->computeWhatProvides($name, $constraint);
}
/**
* @see whatProvides
*/
private function computeWhatProvides($name, $constraint)
{
if (!isset($this->packageByName[$name])) {
return array();
}
$matches = array();
foreach ($this->packageByName[$name] as $candidate) {
if ($this->match($candidate, $name, $constraint)) {
$matches[] = $candidate;
}
}
return $matches;
}
public function literalToPackage($literal)
{
$packageId = abs($literal);
return $this->packageById($packageId);
}
public function literalToPrettyString($literal, $installedMap)
{
$package = $this->literalToPackage($literal);
if (isset($installedMap[$package->id])) {
$prefix = ($literal > 0 ? 'keep' : 'remove');
} else {
$prefix = ($literal > 0 ? 'install' : 'don\'t install');
}
return $prefix.' '.$package->getPrettyString();
}
/**
* Checks if the package matches the given constraint directly or through
* provided or replaced packages
*
* @param BasePackage $candidate
* @param string $name Name of the package to be matched
* @param ConstraintInterface $constraint The constraint to verify
* @return bool
*/
public function match(BasePackage $candidate, $name, ConstraintInterface $constraint = null)
{
$candidateName = $candidate->getName();
$candidateVersion = $candidate->getVersion();
if ($candidateName === $name) {
return $constraint === null || CompilingMatcher::match($constraint, Constraint::OP_EQ, $candidateVersion);
}
$provides = $candidate->getProvides();
$replaces = $candidate->getReplaces();
// aliases create multiple replaces/provides for one target so they can not use the shortcut below
if (isset($replaces[0]) || isset($provides[0])) {
foreach ($provides as $link) {
if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
return true;
}
}
foreach ($replaces as $link) {
if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
return true;
}
}
return false;
}
if (isset($provides[$name]) && ($constraint === null || $constraint->matches($provides[$name]->getConstraint()))) {
return true;
}
if (isset($replaces[$name]) && ($constraint === null || $constraint->matches($replaces[$name]->getConstraint()))) {
return true;
}
return false;
}
public function isUnacceptableFixedOrLockedPackage(BasePackage $package)
{
return \in_array($package, $this->unacceptableFixedOrLockedPackages, true);
}
public function __toString()
{
$str = "Pool:\n";
foreach ($this->packages as $package) {
$str .= '- '.str_pad($package->id, 6, ' ', STR_PAD_LEFT).': '.$package->getName()."\n";
}
return $str;
}
}