From a91c946e273f673cc4cbe6e5b4f13f58003251b9 Mon Sep 17 00:00:00 2001 From: xy2z Date: Tue, 12 Nov 2019 16:32:04 +0100 Subject: [PATCH] Hide not-yet installed packages from "require" and "init" suggestions --- src/Composer/Command/InitCommand.php | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index d50e90c4e..d2e2c9e75 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -436,11 +436,40 @@ EOT } $versionParser = new VersionParser(); + + $composer = $this->getComposer(false); + $installedRepo = ($composer) ? $composer->getRepositoryManager()->getLocalRepository() : null; $io = $this->getIO(); while (null !== $package = $io->ask('Search for a package: ')) { $matches = $this->findPackages($package); if (count($matches)) { + // Exclude existing packages + $existingPackages = []; + foreach ($matches as $position => $foundPackage) { + if ($installedRepo && $installedRepo->findPackage($foundPackage['name'], '*')) { + $existingPackages[] = $position; + continue; + } + + foreach ($requires as $requiredPackage) { + $existingPackageName = substr($requiredPackage, 0, strpos($requiredPackage, ' ')); + + if ($foundPackage['name'] == $existingPackageName) { + $existingPackages[] = $position; + break; + } + } + } + + // Remove existing packages from search results. + if (!empty($existingPackages)) { + foreach ($existingPackages as $position) { + unset($matches[$position]); + } + $matches = array_values($matches); + } + $exactMatch = null; $choices = array(); foreach ($matches as $position => $foundPackage) {