Procházet zdrojové kódy

Merge pull request #208 from stof/find_packages

Find packages
Jordi Boggiano před 13 roky
rodič
revize
05c84e9dfd

+ 5 - 9
src/Composer/Command/ShowCommand.php

@@ -85,11 +85,9 @@ EOT
             $composer->getRepositoryManager()->getRepositories()
         );
         foreach ($repos as $repository) {
-            foreach ($repository->getPackages() as $package) {
-                if ($package->getName() === $input->getArgument('package')) {
-                    if (null === $highestVersion || version_compare($package->getVersion(), $highestVersion->getVersion(), '>=')) {
-                        $highestVersion = $package;
-                    }
+            foreach ($repository->findPackagesByName($input->getArgument('package')) as $package) {
+                if (null === $highestVersion || version_compare($package->getVersion(), $highestVersion->getVersion(), '>=')) {
+                    $highestVersion = $package;
                 }
             }
         }
@@ -135,10 +133,8 @@ EOT
         $versions = array();
 
         foreach ($composer->getRepositoryManager()->getRepositories() as $repository) {
-            foreach ($repository->getPackages() as $version) {
-                if ($version->getName() === $package->getName()) {
-                    $versions[] = $version->getPrettyVersion();
-                }
+            foreach ($repository->findPackagesByName($package->getName()) as $version) {
+                $versions[] = $version->getPrettyVersion();
             }
         }
 

+ 13 - 0
src/Composer/Repository/ArrayRepository.php

@@ -41,6 +41,19 @@ class ArrayRepository implements RepositoryInterface
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    public function findPackagesByName($name)
+    {
+        // normalize name
+        $name = strtolower($name);
+
+        return array_filter($this->getPackages(), function (PackageInterface $package) use ($name) {
+            return $package->getName() === $name;
+        });
+    }
+
     /**
      * {@inheritDoc}
      */

+ 9 - 0
src/Composer/Repository/RepositoryInterface.php

@@ -41,6 +41,15 @@ interface RepositoryInterface extends \Countable
      */
     function findPackage($name, $version);
 
+    /**
+     * Searches for packages by it's name.
+     *
+     * @param   string  $name       package name
+     *
+     * @return  array
+     */
+    function findPackagesByName($name);
+
     /**
      * Returns list of registered packages.
      *