Browse Source

Merge pull request #4214 from legoktm/parse-links

Move VersionParser::parseLinks() to ArrayLoader::parseLinks()
Jordi Boggiano 9 years ago
parent
commit
c36d2a2e50

+ 25 - 1
src/Composer/Package/Loader/ArrayLoader.php

@@ -14,6 +14,7 @@ namespace Composer\Package\Loader;
 
 use Composer\Package;
 use Composer\Package\AliasPackage;
+use Composer\Package\Link;
 use Composer\Package\RootAliasPackage;
 use Composer\Package\RootPackageInterface;
 use Composer\Package\Version\VersionParser;
@@ -115,7 +116,7 @@ class ArrayLoader implements LoaderInterface
             if (isset($config[$type])) {
                 $method = 'set'.ucfirst($opts['method']);
                 $package->{$method}(
-                    $this->versionParser->parseLinks(
+                    $this->parseLinks(
                         $package->getName(),
                         $package->getPrettyVersion(),
                         $opts['description'],
@@ -216,6 +217,29 @@ class ArrayLoader implements LoaderInterface
         return $package;
     }
 
+    /**
+     * @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->versionParser->parseConstraints($sourceVersion);
+            } else {
+                $parsedConstraint = $this->versionParser->parseConstraints($constraint);
+            }
+
+            $res[strtolower($target)] = new Link($source, $target, $parsedConstraint, $description, $constraint);
+        }
+
+        return $res;
+    }
+
     /**
      * Retrieves a branch alias (dev-master => 1.0.x-dev for example) if it exists
      *

+ 2 - 4
src/Composer/Package/Locker.php

@@ -19,7 +19,6 @@ use Composer\Util\ProcessExecutor;
 use Composer\Repository\ArrayRepository;
 use Composer\Package\Dumper\ArrayDumper;
 use Composer\Package\Loader\ArrayLoader;
-use Composer\Package\Version\VersionParser;
 use Composer\Util\Git as GitUtil;
 use Composer\IO\IOInterface;
 
@@ -133,11 +132,10 @@ class Locker
     public function getPlatformRequirements($withDevReqs = false)
     {
         $lockData = $this->getLockData();
-        $versionParser = new VersionParser();
         $requirements = array();
 
         if (!empty($lockData['platform'])) {
-            $requirements = $versionParser->parseLinks(
+            $requirements = $this->loader->parseLinks(
                 '__ROOT__',
                 '1.0.0',
                 'requires',
@@ -146,7 +144,7 @@ class Locker
         }
 
         if ($withDevReqs && !empty($lockData['platform-dev'])) {
-            $devRequirements = $versionParser->parseLinks(
+            $devRequirements = $this->loader->parseLinks(
                 '__ROOT__',
                 '1.0.0',
                 'requires',

+ 6 - 11
src/Composer/Package/Version/VersionParser.php

@@ -17,6 +17,7 @@ use Composer\Package\Link;
 use Composer\Package\LinkConstraint\EmptyConstraint;
 use Composer\Package\LinkConstraint\MultiConstraint;
 use Composer\Package\LinkConstraint\VersionConstraint;
+use Composer\Package\Loader\ArrayLoader;
 
 /**
  * Version parser
@@ -212,6 +213,7 @@ class VersionParser
     }
 
     /**
+     * @deprecated use ArrayLoader::parseLinks() instead
      * @param  string $source        source package name
      * @param  string $sourceVersion source package version (pretty version ideally)
      * @param  string $description   link description (e.g. requires, replaces, ..)
@@ -220,18 +222,11 @@ class VersionParser
      */
     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[strtolower($target)] = new Link($source, $target, $parsedConstraint, $description, $constraint);
-        }
+        trigger_error(__METHOD__.' is deprecated. Use '.
+            '\Composer\Package\Loader\ArrayLoader::parseLinks() instead', E_USER_DEPRECATED);
+        $loader = new ArrayLoader($this, false);
 
-        return $res;
+        return $loader->parseLinks($source, $sourceVersion, $description, $links);
     }
 
     /**

+ 46 - 0
tests/Composer/Test/Package/Loader/ArrayLoaderTest.php

@@ -17,6 +17,12 @@ use Composer\Package\Dumper\ArrayDumper;
 
 class ArrayLoaderTest extends \PHPUnit_Framework_TestCase
 {
+
+    /**
+     * @var ArrayLoader
+     */
+    private $loader;
+
     public function setUp()
     {
         $this->loader = new ArrayLoader(null, true);
@@ -207,4 +213,44 @@ class ArrayLoaderTest extends \PHPUnit_Framework_TestCase
         $package = $this->loader->load($config);
         $this->assertFalse($package->isAbandoned());
     }
+
+    public function pluginApiVersions()
+    {
+        return array(
+            array('1.0'),
+            array('1.0.0'),
+            array('1.0.0.0'),
+            array('1'),
+            array('=1.0.0'),
+            array('==1.0'),
+            array('~1.0.0'),
+            array('*'),
+            array('3.0.*'),
+            array('@stable'),
+            array('1.0.0@stable'),
+            array('^5.1'),
+            array('>=1.0.0 <2.5'),
+            array('x'),
+            array('1.0.0-dev'),
+        );
+    }
+
+    /**
+     * @dataProvider pluginApiVersions
+     */
+    public function testPluginApiVersionAreKeptAsDeclared($apiVersion)
+    {
+        $links = $this->loader->parseLinks('Plugin', '9.9.9', '', array('composer-plugin-api' => $apiVersion));
+
+        $this->assertArrayHasKey('composer-plugin-api', $links);
+        $this->assertSame($apiVersion, $links['composer-plugin-api']->getConstraint()->getPrettyString());
+    }
+
+    public function testPluginApiVersionDoesSupportSelfVersion()
+    {
+        $links = $this->loader->parseLinks('Plugin', '6.6.6', '', array('composer-plugin-api' => 'self.version'));
+
+        $this->assertArrayHasKey('composer-plugin-api', $links);
+        $this->assertSame('6.6.6', $links['composer-plugin-api']->getConstraint()->getPrettyString());
+    }
 }

+ 0 - 46
tests/Composer/Test/Package/Version/VersionParserTest.php

@@ -469,50 +469,4 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase
             array('RC',     '2.0.0rc1')
         );
     }
-
-    public function pluginApiVersions()
-    {
-        return array(
-            array('1.0'),
-            array('1.0.0'),
-            array('1.0.0.0'),
-            array('1'),
-            array('=1.0.0'),
-            array('==1.0'),
-            array('~1.0.0'),
-            array('*'),
-            array('3.0.*'),
-            array('@stable'),
-            array('1.0.0@stable'),
-            array('^5.1'),
-            array('>=1.0.0 <2.5'),
-            array('x'),
-            array('1.0.0-dev'),
-        );
-    }
-
-    /**
-     * @dataProvider pluginApiVersions
-     */
-    public function testPluginApiVersionAreKeptAsDeclared($apiVersion)
-    {
-        $parser = new VersionParser;
-
-        /** @var Link[] $links */
-        $links = $parser->parseLinks('Plugin', '9.9.9', '', array('composer-plugin-api' => $apiVersion));
-
-        $this->assertArrayHasKey('composer-plugin-api', $links);
-        $this->assertSame($apiVersion, $links['composer-plugin-api']->getConstraint()->getPrettyString());
-    }
-
-    public function testPluginApiVersionDoesSupportSelfVersion()
-    {
-        $parser = new VersionParser;
-
-        /** @var Link[] $links */
-        $links = $parser->parseLinks('Plugin', '6.6.6', '', array('composer-plugin-api' => 'self.version'));
-
-        $this->assertArrayHasKey('composer-plugin-api', $links);
-        $this->assertSame('6.6.6', $links['composer-plugin-api']->getConstraint()->getPrettyString());
-    }
 }