Skip to content

Commit

Permalink
bug #960 Correctly resolve the lock alias (pierredup)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Nov 22, 2022
2 parents ab0453b + de71648 commit 2c5e9cb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/PackageResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public function resolve(array $arguments = [], bool $isRequire = false): array
$packages = [];
foreach ($arguments as $i => $argument) {
if ((false !== $pos = strpos($argument, ':')) || (false !== $pos = strpos($argument, '='))) {
$package = $this->resolvePackageName(substr($argument, 0, $pos), $i);
$package = $this->resolvePackageName(substr($argument, 0, $pos), $i, $isRequire);
$version = substr($argument, $pos + 1);
$packages[] = $package.':'.$version;
} else {
$packages[] = $this->resolvePackageName($argument, $i);
$packages[] = $this->resolvePackageName($argument, $i, $isRequire);
}
}

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

private function resolvePackageName(string $argument, int $position): string
private function resolvePackageName(string $argument, int $position, bool $isRequire): 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', ''])) {
$skippedPackages = ['mirrors', 'nothing', ''];

if (!$isRequire) {
$skippedPackages[] = 'lock';
}

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)) {
return $argument;
}

Expand Down
15 changes: 13 additions & 2 deletions tests/PackageResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class PackageResolverTest extends TestCase
/**
* @dataProvider getPackages
*/
public function testResolve($packages, $resolved)
public function testResolve($packages, $resolved, bool $isRequire = false)
{
$this->assertEquals($resolved, $this->getResolver()->resolve($packages));
$this->assertEquals($resolved, $this->getResolver()->resolve($packages, $isRequire));
}

public function getPackages()
Expand All @@ -32,6 +32,16 @@ public function getPackages()
['cli'],
['symfony/console'],
],
[
['lock'],
['lock'],
false,
],
[
['lock'],
['symfony/lock'],
true,
],
[
['cli', 'symfony/workflow'],
['symfony/console', 'symfony/workflow'],
Expand Down Expand Up @@ -116,6 +126,7 @@ private function getResolver()
'console' => 'symfony/console',
'translation' => 'symfony/translation',
'validator' => 'symfony/validator',
'lock' => 'symfony/lock',
]);

return new PackageResolver($downloader);
Expand Down

0 comments on commit 2c5e9cb

Please sign in to comment.