瀏覽代碼

Hide not-yet installed packages from "require" and "init" suggestions

xy2z 5 年之前
父節點
當前提交
a91c946e27
共有 1 個文件被更改,包括 29 次插入0 次删除
  1. 29 0
      src/Composer/Command/InitCommand.php

+ 29 - 0
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) {