-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathPackageFilter.php
155 lines (133 loc) · 5.21 KB
/
PackageFilter.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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Flex;
use Composer\IO\IOInterface;
use Composer\Package\AliasPackage;
use Composer\Package\PackageInterface;
use Composer\Package\RootPackageInterface;
use Composer\Semver\Constraint\Constraint;
use Composer\Semver\Intervals;
use Composer\Semver\VersionParser;
/**
* @author Nicolas Grekas <[email protected]>
*/
class PackageFilter
{
private $versions;
private $versionParser;
private $symfonyRequire;
private $symfonyConstraints;
private $downloader;
private $io;
public function __construct(IOInterface $io, string $symfonyRequire, Downloader $downloader)
{
$this->versionParser = new VersionParser();
$this->symfonyRequire = $symfonyRequire;
$this->symfonyConstraints = $this->versionParser->parseConstraints($symfonyRequire);
$this->downloader = $downloader;
$this->io = $io;
}
/**
* @param PackageInterface[] $data
* @param PackageInterface[] $lockedPackages
*
* @return PackageInterface[]
*/
public function removeLegacyPackages(array $data, RootPackageInterface $rootPackage, array $lockedPackages): array
{
if (!$this->symfonyConstraints || !$data) {
return $data;
}
$lockedVersions = [];
foreach ($lockedPackages as $package) {
$lockedVersions[$package->getName()] = [$package->getVersion()];
if ($package instanceof AliasPackage) {
$lockedVersions[$package->getName()][] = $package->getAliasOf()->getVersion();
}
}
$rootConstraints = [];
foreach ($rootPackage->getRequires() + $rootPackage->getDevRequires() as $name => $link) {
$rootConstraints[$name] = $link->getConstraint();
}
$knownVersions = $this->getVersions();
$filteredPackages = [];
$symfonyPackages = [];
$oneSymfony = false;
foreach ($data as $package) {
$name = $package->getName();
$versions = [$package->getVersion()];
if ($package instanceof AliasPackage) {
$versions[] = $package->getAliasOf()->getVersion();
}
if ('symfony/symfony' !== $name && (
!isset($knownVersions['splits'][$name])
|| array_intersect($versions, $lockedVersions[$name] ?? [])
|| (isset($rootConstraints[$name]) && !Intervals::haveIntersections($this->symfonyConstraints, $rootConstraints[$name]))
|| ('symfony/psr-http-message-bridge' === $name && 6.4 > $versions[0])
)) {
$filteredPackages[] = $package;
continue;
}
if (null !== $alias = $package->getExtra()['branch-alias'][$package->getVersion()] ?? null) {
$versions[] = $this->versionParser->normalize($alias);
}
foreach ($versions as $version) {
if ($this->symfonyConstraints->matches(new Constraint('==', $version))) {
$filteredPackages[] = $package;
$oneSymfony = $oneSymfony || 'symfony/symfony' === $name;
continue 2;
}
}
if ('symfony/symfony' === $name) {
$symfonyPackages[] = $package;
} elseif (null !== $this->io) {
$this->io->writeError(\sprintf('<info>Restricting packages listed in "symfony/symfony" to "%s"</>', $this->symfonyRequire));
$this->io = null;
}
}
if ($symfonyPackages && !$oneSymfony) {
$filteredPackages = array_merge($filteredPackages, $symfonyPackages);
}
return $filteredPackages;
}
private function getVersions(): array
{
if (null !== $this->versions) {
return $this->versions;
}
$versions = $this->downloader->getVersions();
$this->downloader = null;
$okVersions = [];
if (!isset($versions['splits'])) {
throw new \LogicException('The Flex index is missing a "splits" entry. Did you forget to add "flex://defaults" in the "extra.symfony.endpoint" array of your composer.json?');
}
foreach ($versions['splits'] as $name => $vers) {
foreach ($vers as $i => $v) {
if (!isset($okVersions[$v])) {
$okVersions[$v] = false;
$w = '.x' === substr($v, -2) ? $versions['next'] : $v;
for ($j = 0; $j < 60; ++$j) {
if ($this->symfonyConstraints->matches(new Constraint('==', $w.'.'.$j.'.0'))) {
$okVersions[$v] = true;
break;
}
}
}
if (!$okVersions[$v]) {
unset($vers[$i]);
}
}
if (!$vers || $vers === $versions['splits'][$name]) {
unset($versions['splits'][$name]);
}
}
return $this->versions = $versions;
}
}