Skip to content

Commit 2c5e9cb

Browse files
committed
bug #960 Correctly resolve the lock alias (pierredup)
This PR was merged into the 1.x branch. Discussion ---------- Correctly resolve the lock alias `composer require lock` doesn't resolve the `lock` alias because the package name resolution is skipped when the package argument is `lock`. This PR bypasses this specifically for the `require` command, so that `composer require lock` correctly resolved the alias, but `composer update lock` still skips the name resolution. Reference: symfony/recipes#1150 Commits ------- de71648 Correctly resolve the lock alias
2 parents ab0453b + de71648 commit 2c5e9cb

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/PackageResolver.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public function resolve(array $arguments = [], bool $isRequire = false): array
3434
$packages = [];
3535
foreach ($arguments as $i => $argument) {
3636
if ((false !== $pos = strpos($argument, ':')) || (false !== $pos = strpos($argument, '='))) {
37-
$package = $this->resolvePackageName(substr($argument, 0, $pos), $i);
37+
$package = $this->resolvePackageName(substr($argument, 0, $pos), $i, $isRequire);
3838
$version = substr($argument, $pos + 1);
3939
$packages[] = $package.':'.$version;
4040
} else {
41-
$packages[] = $this->resolvePackageName($argument, $i);
41+
$packages[] = $this->resolvePackageName($argument, $i, $isRequire);
4242
}
4343
}
4444

@@ -84,9 +84,15 @@ public function parseVersion(string $package, string $version, bool $isRequire):
8484
return ':'.$version;
8585
}
8686

87-
private function resolvePackageName(string $argument, int $position): string
87+
private function resolvePackageName(string $argument, int $position, bool $isRequire): string
8888
{
89-
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', ''])) {
89+
$skippedPackages = ['mirrors', 'nothing', ''];
90+
91+
if (!$isRequire) {
92+
$skippedPackages[] = 'lock';
93+
}
94+
95+
if (false !== strpos($argument, '/') || preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $argument) || preg_match('{(?<=[a-z0-9_/-])\*|\*(?=[a-z0-9_/-])}i', $argument) || \in_array($argument, $skippedPackages)) {
9096
return $argument;
9197
}
9298

tests/PackageResolverTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class PackageResolverTest extends TestCase
2020
/**
2121
* @dataProvider getPackages
2222
*/
23-
public function testResolve($packages, $resolved)
23+
public function testResolve($packages, $resolved, bool $isRequire = false)
2424
{
25-
$this->assertEquals($resolved, $this->getResolver()->resolve($packages));
25+
$this->assertEquals($resolved, $this->getResolver()->resolve($packages, $isRequire));
2626
}
2727

2828
public function getPackages()
@@ -32,6 +32,16 @@ public function getPackages()
3232
['cli'],
3333
['symfony/console'],
3434
],
35+
[
36+
['lock'],
37+
['lock'],
38+
false,
39+
],
40+
[
41+
['lock'],
42+
['symfony/lock'],
43+
true,
44+
],
3545
[
3646
['cli', 'symfony/workflow'],
3747
['symfony/console', 'symfony/workflow'],
@@ -116,6 +126,7 @@ private function getResolver()
116126
'console' => 'symfony/console',
117127
'translation' => 'symfony/translation',
118128
'validator' => 'symfony/validator',
129+
'lock' => 'symfony/lock',
119130
]);
120131

121132
return new PackageResolver($downloader);

0 commit comments

Comments
 (0)