1
0
Fork 0

Add prompt to require command if a package name is not found but there are similar names that could be used (#11284)

fixes #11279
pull/11526/head
Jordi Boggiano 2023-06-23 11:15:03 +02:00 committed by GitHub
parent 196ac10505
commit 1db0a47080
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 3 deletions

View File

@ -15,6 +15,7 @@ namespace Composer\Command;
use Composer\Factory; use Composer\Factory;
use Composer\Filter\PlatformRequirementFilter\IgnoreAllPlatformRequirementFilter; use Composer\Filter\PlatformRequirementFilter\IgnoreAllPlatformRequirementFilter;
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory; use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
use Composer\IO\IOInterface;
use Composer\Package\CompletePackageInterface; use Composer\Package\CompletePackageInterface;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
@ -100,7 +101,7 @@ trait PackageDiscoveryTrait
if (!isset($requirement['version'])) { if (!isset($requirement['version'])) {
// determine the best version automatically // determine the best version automatically
[$name, $version] = $this->findBestVersionAndNameForPackage($input, $requirement['name'], $platformRepo, $preferredStability, $fixed); [$name, $version] = $this->findBestVersionAndNameForPackage($this->getIO(), $input, $requirement['name'], $platformRepo, $preferredStability, $fixed);
// replace package name from packagist.org // replace package name from packagist.org
$requirement['name'] = $name; $requirement['name'] = $name;
@ -243,7 +244,7 @@ trait PackageDiscoveryTrait
); );
if (false === $constraint) { if (false === $constraint) {
[, $constraint] = $this->findBestVersionAndNameForPackage($input, $package, $platformRepo, $preferredStability); [, $constraint] = $this->findBestVersionAndNameForPackage($this->getIO(), $input, $package, $platformRepo, $preferredStability);
$io->writeError(sprintf( $io->writeError(sprintf(
'Using version <info>%s</info> for <info>%s</info>', 'Using version <info>%s</info> for <info>%s</info>',
@ -273,7 +274,7 @@ trait PackageDiscoveryTrait
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @return array{string, string} name version * @return array{string, string} name version
*/ */
private function findBestVersionAndNameForPackage(InputInterface $input, string $name, ?PlatformRepository $platformRepo = null, string $preferredStability = 'stable', bool $fixed = false): array private function findBestVersionAndNameForPackage(IOInterface $io, InputInterface $input, string $name, ?PlatformRepository $platformRepo = null, string $preferredStability = 'stable', bool $fixed = false): array
{ {
// handle ignore-platform-reqs flag if present // handle ignore-platform-reqs flag if present
if ($input->hasOption('ignore-platform-reqs') && $input->hasOption('ignore-platform-req')) { if ($input->hasOption('ignore-platform-reqs') && $input->hasOption('ignore-platform-req')) {
@ -358,6 +359,13 @@ trait PackageDiscoveryTrait
)); ));
} }
if ($input->isInteractive()) {
$result = $io->select("<error>Could not find package $name.</error>\nPick one of these or leave empty to abort:", $similar, false, 1);
if ($result !== false) {
return $this->findBestVersionAndNameForPackage($io, $input, $similar[$result], $platformRepo, $preferredStability, $fixed);
}
}
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
"Could not find package %s.\n\nDid you mean " . (count($similar) > 1 ? 'one of these' : 'this') . "?\n %s", "Could not find package %s.\n\nDid you mean " . (count($similar) > 1 ? 'one of these' : 'this') . "?\n %s",
$name, $name,