From 9965f02951c5db1edfc173ee1dc8236eaf1d6874 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 23 Aug 2012 16:12:46 +0200 Subject: [PATCH] Clean up link creation --- src/Composer/DependencyResolver/Pool.php | 17 +++++--------- src/Composer/Package/Loader/ArrayLoader.php | 22 +++++------------- .../Package/Version/VersionParser.php | 23 +++++++++++++++++++ 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/Composer/DependencyResolver/Pool.php b/src/Composer/DependencyResolver/Pool.php index 940178ed3..955be6939 100644 --- a/src/Composer/DependencyResolver/Pool.php +++ b/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(); diff --git a/src/Composer/Package/Loader/ArrayLoader.php b/src/Composer/Package/Loader/ArrayLoader.php index 0a8c9fb53..73c43a908 100644 --- a/src/Composer/Package/Loader/ArrayLoader.php +++ b/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; - } } diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index cd1ca30d0..ce06891b5 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/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 *