Browse Source

Move loadPackages impl out of BaseRepository

Jordi Boggiano 5 years ago
parent
commit
926afab1f4

+ 38 - 2
src/Composer/Repository/ArrayRepository.php

@@ -28,8 +28,8 @@ class ArrayRepository extends BaseRepository
 {
     /** @var PackageInterface[] */
     protected $packages;
-    
-    /** 
+
+    /**
       * @var PackageInterface[] indexed by package unique name and used to cache hasPackage calls
       */
     protected $packageMap;
@@ -41,6 +41,42 @@ class ArrayRepository extends BaseRepository
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    public function loadPackages(array $packageMap, $isPackageAcceptableCallable)
+    {
+        $packages = $this->getPackages();
+
+        $result = array();
+        $namesFound = array();
+        foreach ($packages as $package) {
+            if (array_key_exists($package->getName(), $packageMap)) {
+                if (
+                    (!$packageMap[$package->getName()] || $packageMap[$package->getName()]->matches(new Constraint('==', $package->getVersion())))
+                    && call_user_func($isPackageAcceptableCallable, $package->getNames(), $package->getStability())
+                ) {
+                    $result[spl_object_hash($package)] = $package;
+                    if ($package instanceof AliasPackage && !isset($result[spl_object_hash($package->getAliasOf())])) {
+                        $result[spl_object_hash($package->getAliasOf())] = $package->getAliasOf();
+                    }
+                }
+
+                $namesFound[$package->getName()] = true;
+            }
+        }
+
+        foreach ($packages as $package) {
+            if ($package instanceof AliasPackage) {
+                if (isset($result[spl_object_hash($package->getAliasOf())])) {
+                    $result[spl_object_hash($package)] = $package;
+                }
+            }
+        }
+
+        return array('namesFound' => array_keys($namesFound), 'packages' => $result);
+    }
+
     /**
      * {@inheritDoc}
      */

+ 0 - 34
src/Composer/Repository/BaseRepository.php

@@ -25,40 +25,6 @@ use Composer\Package\Link;
  */
 abstract class BaseRepository implements RepositoryInterface
 {
-    // TODO should this stay here? some repos need a better implementation
-    public function loadPackages(array $packageMap, $isPackageAcceptableCallable)
-    {
-        $packages = $this->getPackages();
-
-        $result = array();
-        $namesFound = array();
-        foreach ($packages as $package) {
-            if (array_key_exists($package->getName(), $packageMap)) {
-                if (
-                    (!$packageMap[$package->getName()] || $packageMap[$package->getName()]->matches(new Constraint('==', $package->getVersion())))
-                    && call_user_func($isPackageAcceptableCallable, $package->getNames(), $package->getStability())
-                ) {
-                    $result[spl_object_hash($package)] = $package;
-                    if ($package instanceof AliasPackage && !isset($result[spl_object_hash($package->getAliasOf())])) {
-                        $result[spl_object_hash($package->getAliasOf())] = $package->getAliasOf();
-                    }
-                }
-
-                $namesFound[$package->getName()] = true;
-            }
-        }
-
-        foreach ($packages as $package) {
-            if ($package instanceof AliasPackage) {
-                if (isset($result[spl_object_hash($package->getAliasOf())])) {
-                    $result[spl_object_hash($package)] = $package;
-                }
-            }
-        }
-
-        return array('namesFound' => array_keys($namesFound), 'packages' => $result);
-    }
-
     /**
      * Returns a list of links causing the requested needle packages to be installed, as an associative array with the
      * dependent's name as key, and an array containing in order the PackageInterface and Link describing the relationship

+ 20 - 0
src/Composer/Repository/CompositeRepository.php

@@ -94,6 +94,26 @@ class CompositeRepository extends BaseRepository
         return $packages ? call_user_func_array('array_merge', $packages) : array();
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    public function loadPackages(array $packageMap, $isPackageAcceptableCallable)
+    {
+        $packages = array();
+        $namesFound = array();
+        foreach ($this->repositories as $repository) {
+            /* @var $repository RepositoryInterface */
+            $result = $repository->findPackages($name, $constraint);
+            $packages[] = $result['packages'];
+            $namesFound[] = $result['namesFound'];
+        }
+
+        return array(
+            'packages' => $packages ? call_user_func_array('array_merge', $packages) : array(),
+            'namesFound' => $namesFound ? array_unique(call_user_func_array('array_merge', $namesFound)) : array(),
+        );
+    }
+
     /**
      * {@inheritdoc}
      */