diff --git a/src/Composer/Package/Loader/ArrayLoader.php b/src/Composer/Package/Loader/ArrayLoader.php index 60cff3b33..5d34d1b31 100644 --- a/src/Composer/Package/Loader/ArrayLoader.php +++ b/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 * diff --git a/src/Composer/Package/Locker.php b/src/Composer/Package/Locker.php index 9f9a0abf8..5aacf4450 100644 --- a/src/Composer/Package/Locker.php +++ b/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', diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index dabf40a7b..719748a3e 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -18,6 +18,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 @@ -209,6 +210,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, ..) @@ -217,18 +219,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); - } + trigger_error(__METHOD__.' is deprecated. Use '. + '\Composer\Package\Loader\ArrayLoader::parseLinks() instead', E_USER_DEPRECATED); + $loader = new ArrayLoader($this, false); - $res[strtolower($target)] = new Link($source, $target, $parsedConstraint, $description, $constraint); - } - - return $res; + return $loader->parseLinks($source, $sourceVersion, $description, $links); } /** diff --git a/tests/Composer/Test/Package/Loader/ArrayLoaderTest.php b/tests/Composer/Test/Package/Loader/ArrayLoaderTest.php index 1491571a1..b452a4095 100644 --- a/tests/Composer/Test/Package/Loader/ArrayLoaderTest.php +++ b/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()); + } } diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index 632727165..fe2dab749 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/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()); - } }