1
0
Fork 0

Merge pull request #4214 from legoktm/parse-links

Move VersionParser::parseLinks() to ArrayLoader::parseLinks()
pull/6447/head
Jordi Boggiano 2015-07-13 14:28:10 +01:00
commit c36d2a2e50
5 changed files with 79 additions and 62 deletions

View File

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

View File

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

View File

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

View File

@ -17,6 +17,12 @@ use Composer\Package\Dumper\ArrayDumper;
class ArrayLoaderTest extends \PHPUnit_Framework_TestCase class ArrayLoaderTest extends \PHPUnit_Framework_TestCase
{ {
/**
* @var ArrayLoader
*/
private $loader;
public function setUp() public function setUp()
{ {
$this->loader = new ArrayLoader(null, true); $this->loader = new ArrayLoader(null, true);
@ -207,4 +213,44 @@ class ArrayLoaderTest extends \PHPUnit_Framework_TestCase
$package = $this->loader->load($config); $package = $this->loader->load($config);
$this->assertFalse($package->isAbandoned()); $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());
}
} }

View File

@ -469,50 +469,4 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase
array('RC', '2.0.0rc1') 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());
}
} }