forked from symfony/flex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PackageResolver.php
145 lines (125 loc) · 5.17 KB
/
PackageResolver.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
<?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\Factory;
use Composer\Package\Version\VersionParser;
use Composer\Repository\PlatformRepository;
/**
* @author Fabien Potencier <[email protected]>
*/
class PackageResolver
{
private static $SYMFONY_VERSIONS = ['lts', 'previous', 'stable', 'next', 'dev'];
private $downloader;
public function __construct(Downloader $downloader)
{
$this->downloader = $downloader;
}
public function resolve(array $arguments = [], bool $isRequire = false): array
{
// first pass split on : and = to resolve package names
$packages = [];
foreach ($arguments as $i => $argument) {
if ((false !== $pos = strpos($argument, ':')) || (false !== $pos = strpos($argument, '='))) {
$package = $this->resolvePackageName(substr($argument, 0, $pos), $i);
$version = substr($argument, $pos + 1);
$packages[] = $package.':'.$version;
} else {
$packages[] = $this->resolvePackageName($argument, $i);
}
}
// second pass to resolve versions
$versionParser = new VersionParser();
$requires = [];
foreach ($versionParser->parseNameVersionPairs($packages) as $package) {
$requires[] = $package['name'].$this->parseVersion($package['name'], $package['version'] ?? '', $isRequire);
}
return array_unique($requires);
}
public function parseVersion(string $package, string $version, bool $isRequire): string
{
if (0 !== strpos($package, 'symfony/')) {
return $version ? ':'.$version : '';
}
$versions = $this->downloader->getVersions();
if (!isset($versions['splits'][$package])) {
return $version ? ':'.$version : '';
}
if (!$version || '*' === $version) {
try {
$config = @json_decode(file_get_contents(Factory::getComposerFile()), true);
} finally {
if (!$isRequire || !(isset($config['extra']['symfony']['require']) || isset($config['require']['symfony/framework-bundle']))) {
return '';
}
}
$version = $config['extra']['symfony']['require'] ?? $config['require']['symfony/framework-bundle'];
} elseif ('dev' === $version) {
$version = '^'.$versions['dev-name'].'@dev';
} elseif ('next' === $version) {
$version = '^'.$versions[$version].'@dev';
} elseif (\in_array($version, self::$SYMFONY_VERSIONS, true)) {
$version = '^'.$versions[$version];
}
return ':'.$version;
}
private function resolvePackageName(string $argument, int $position): string
{
if (false !== strpos($argument, '/') || preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $argument) || preg_match('{(?<=[a-z0-9_/-])\*|\*(?=[a-z0-9_/-])}i', $argument) || \in_array($argument, ['lock', 'mirrors', 'nothing', ''])) {
return $argument;
}
$aliases = $this->downloader->getAliases();
if (isset($aliases[$argument])) {
$argument = $aliases[$argument];
} else {
// is it a version or an alias that does not exist?
try {
$versionParser = new VersionParser();
$versionParser->parseConstraints($argument);
} catch (\UnexpectedValueException $e) {
// is it a special Symfony version?
if (!\in_array($argument, self::$SYMFONY_VERSIONS, true)) {
$this->throwAlternatives($argument, $position);
}
}
}
return $argument;
}
/**
* @throws \UnexpectedValueException
*/
private function throwAlternatives(string $argument, int $position)
{
$alternatives = [];
foreach ($this->downloader->getAliases() as $alias => $package) {
$lev = levenshtein($argument, $alias);
if ($lev <= \strlen($argument) / 3 || ('' !== $argument && false !== strpos($alias, $argument))) {
$alternatives[$package][] = $alias;
}
}
// First position can only be a package name, not a version
if ($alternatives || 0 === $position) {
$message = sprintf('"%s" is not a valid alias.', $argument);
if ($alternatives) {
if (1 === \count($alternatives)) {
$message .= " Did you mean this:\n";
} else {
$message .= " Did you mean one of these:\n";
}
foreach ($alternatives as $package => $aliases) {
$message .= sprintf(" \"%s\", supported aliases: \"%s\"\n", $package, implode('", "', $aliases));
}
}
} else {
$message = sprintf('Could not parse version constraint "%s".', $argument);
}
throw new \UnexpectedValueException($message);
}
}