Browse Source

Clean up link creation

Jordi Boggiano 13 years ago
parent
commit
9965f02951

+ 6 - 11
src/Composer/DependencyResolver/Pool.php

@@ -339,17 +339,12 @@ class Pool
         if (is_array($candidate)) {
             $candidateName = $candidate['name'];
             $candidateVersion = $candidate['version'];
-            foreach (array('provides', 'replaces') as $linkType) {
-                ${$linkType} = isset($candidate[rtrim($linkType, 's')]) ? $candidate[rtrim($linkType, 's')] : array();
-                foreach (${$linkType} as $target => $constraintDef) {
-                    if ('self.version' === $constraintDef) {
-                        $parsedConstraint = $this->versionParser->parseConstraints($candidateVersion);
-                    } else {
-                        $parsedConstraint = $this->versionParser->parseConstraints($constraintDef);
-                    }
-                    ${$linkType}[$target] = new Link($candidateName, $target, $parsedConstraint, $linkType, $constraintDef);
-                }
-            }
+            $provides = isset($candidate['provide'])
+                ? $this->versionParser->parseLinks($candidateName, $candidateVersion, 'provides', $candidate['provide'])
+                : array();
+            $replaces = isset($candidate['replace'])
+                ? $this->versionParser->parseLinks($candidateName, $candidateVersion, 'replaces', $candidate['replace'])
+                : array();
         } else {
             // handle object packages
             $candidateName = $candidate->getName();

+ 6 - 16
src/Composer/Package/Loader/ArrayLoader.php

@@ -107,7 +107,12 @@ class ArrayLoader implements LoaderInterface
             if (isset($config[$type])) {
                 $method = 'set'.ucfirst($opts['method']);
                 $package->{$method}(
-                    $this->loadLinksFromConfig($package, $opts['description'], $config[$type])
+                    $this->versionParser->parseLinks(
+                        $package->getName(),
+                        $package->getPrettyVersion(),
+                        $opts['description'],
+                        $config[$type]
+                    )
                 );
             }
         }
@@ -209,19 +214,4 @@ class ArrayLoader implements LoaderInterface
             return $validatedTargetBranch;
         }
     }
-
-    private function loadLinksFromConfig($package, $description, array $linksSpecs)
-    {
-        $links = array();
-        foreach ($linksSpecs as $packageName => $constraint) {
-            if ('self.version' === $constraint) {
-                $parsedConstraint = $this->versionParser->parseConstraints($package->getPrettyVersion());
-            } else {
-                $parsedConstraint = $this->versionParser->parseConstraints($constraint);
-            }
-            $links[] = new Package\Link($package->getName(), $packageName, $parsedConstraint, $description, $constraint);
-        }
-
-        return $links;
-    }
 }

+ 23 - 0
src/Composer/Package/Version/VersionParser.php

@@ -14,6 +14,7 @@ namespace Composer\Package\Version;
 
 use Composer\Package\BasePackage;
 use Composer\Package\PackageInterface;
+use Composer\Package\Link;
 use Composer\Package\LinkConstraint\MultiConstraint;
 use Composer\Package\LinkConstraint\VersionConstraint;
 
@@ -164,6 +165,28 @@ class VersionParser
         return 'dev-'.$name;
     }
 
+    /**
+     * @param string $source source package name
+     * @param string $sourceVersion source package version (pretty version ideally)
+     * @param string $description link description (e.g. requires, replaces, ..)
+     * @param array $links array of package name => constraint mappings
+     * @return Link[]
+     */
+    public function parseLinks($source, $sourceVersion, $description, $links)
+    {
+        $res = array();
+        foreach ($links as $target => $constraint) {
+            if ('self.version' === $constraint) {
+                $parsedConstraint = $this->parseConstraints($sourceVersion);
+            } else {
+                $parsedConstraint = $this->parseConstraints($constraint);
+            }
+            $res[] = new Link($source, $target, $parsedConstraint, $description, $constraint);
+        }
+
+        return $res;
+    }
+
     /**
      * Parses as constraint string into LinkConstraint objects
      *