Skip to content

Commit 9a22ef2

Browse files
committed
bug #800 Fix support for versions with slashes (fabpot)
This PR was merged into the 1.13-dev branch. Discussion ---------- Fix support for versions with slashes Commits ------- 083521a Fix support for versions with slashes
2 parents f16f107 + 083521a commit 9a22ef2

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

src/PackageResolver.php

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,20 @@ public function __construct(Downloader $downloader)
3131

3232
public function resolve(array $arguments = [], bool $isRequire = false): array
3333
{
34-
$versionParser = new VersionParser();
35-
36-
// first pass split on : and = to separate package names and versions
37-
$explodedArguments = [];
38-
foreach ($arguments as $argument) {
34+
// first pass split on : and = to resolve package names
35+
$packages = [];
36+
foreach ($arguments as $i => $argument) {
3937
if ((false !== $pos = strpos($argument, ':')) || (false !== $pos = strpos($argument, '='))) {
40-
$explodedArguments[] = substr($argument, 0, $pos);
41-
$explodedArguments[] = substr($argument, $pos + 1);
38+
$package = $this->resolvePackageName(substr($argument, 0, $pos), $i);
39+
$version = substr($argument, $pos + 1);
40+
$packages[] = $package.':'.$version;
4241
} else {
43-
$explodedArguments[] = $argument;
42+
$packages[] = $this->resolvePackageName($argument, $i);
4443
}
4544
}
4645

47-
// second pass to resolve package names
48-
$packages = [];
49-
foreach ($explodedArguments as $i => $argument) {
50-
if (false === strpos($argument, '/') && !preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $argument) && !preg_match('{(?<=[a-z0-9_/-])\*|\*(?=[a-z0-9_/-])}i', $argument) && !\in_array($argument, ['mirrors', 'nothing'])) {
51-
if (null === self::$aliases) {
52-
self::$aliases = $this->downloader->get('/aliases.json')->getBody();
53-
}
54-
55-
if (isset(self::$aliases[$argument])) {
56-
$argument = self::$aliases[$argument];
57-
} else {
58-
// is it a version or an alias that does not exist?
59-
try {
60-
$versionParser->parseConstraints($argument);
61-
} catch (\UnexpectedValueException $e) {
62-
// is it a special Symfony version?
63-
if (!\in_array($argument, self::$SYMFONY_VERSIONS, true)) {
64-
$this->throwAlternatives($argument, $i);
65-
}
66-
}
67-
}
68-
}
69-
70-
$packages[] = $argument;
71-
}
72-
73-
// third pass to resolve versions
46+
// second pass to resolve versions
47+
$versionParser = new VersionParser();
7448
$requires = [];
7549
foreach ($versionParser->parseNameVersionPairs($packages) as $package) {
7650
$requires[] = $package['name'].$this->parseVersion($package['name'], $package['version'] ?? '', $isRequire);
@@ -111,6 +85,34 @@ public function parseVersion(string $package, string $version, bool $isRequire):
11185
return ':'.$version;
11286
}
11387

88+
private function resolvePackageName(string $argument, int $position): string
89+
{
90+
if (false !== strpos($argument, '/') || preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $argument) || preg_match('{(?<=[a-z0-9_/-])\*|\*(?=[a-z0-9_/-])}i', $argument) || \in_array($argument, ['mirrors', 'nothing'])) {
91+
return $argument;
92+
}
93+
94+
if (null === self::$aliases) {
95+
self::$aliases = $this->downloader->get('/aliases.json')->getBody();
96+
}
97+
98+
if (isset(self::$aliases[$argument])) {
99+
$argument = self::$aliases[$argument];
100+
} else {
101+
// is it a version or an alias that does not exist?
102+
try {
103+
$versionParser = new VersionParser();
104+
$versionParser->parseConstraints($argument);
105+
} catch (\UnexpectedValueException $e) {
106+
// is it a special Symfony version?
107+
if (!\in_array($argument, self::$SYMFONY_VERSIONS, true)) {
108+
$this->throwAlternatives($argument, $position);
109+
}
110+
}
111+
}
112+
113+
return $argument;
114+
}
115+
114116
/**
115117
* @throws \UnexpectedValueException
116118
*/

tests/PackageResolverTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public function getPackages()
3131
['cli'],
3232
['symfony/console'],
3333
],
34+
[
35+
['cli', 'symfony/workflow'],
36+
['symfony/console', 'symfony/workflow'],
37+
],
3438
[
3539
['console', 'validator', 'translation'],
3640
['symfony/console', 'symfony/validator', 'symfony/translation'],
@@ -43,6 +47,10 @@ public function getPackages()
4347
['cli:lts', 'validator=3.2', 'translation', 'next'],
4448
['symfony/console:^3.4', 'symfony/validator:3.2', 'symfony/translation:^4.0@dev'],
4549
],
50+
[
51+
['cli:dev-feature/abc'],
52+
['symfony/console:dev-feature/abc'],
53+
],
4654
[
4755
['php'],
4856
['php'],
@@ -83,10 +91,6 @@ public function getWrongPackages()
8391
['qwerty'],
8492
'"qwerty" is not a valid alias.',
8593
],
86-
[
87-
['lts:'],
88-
'Could not parse version constraint "".',
89-
],
9094
];
9195
}
9296

0 commit comments

Comments
 (0)