1
0
Fork 0

Merge pull request #350 from Seldaek/aliasing

Aliasing
pull/342/merge
Nils Adermann 2012-02-22 02:48:49 -08:00
commit 85a07affd7
20 changed files with 455 additions and 69 deletions

View File

@ -20,9 +20,12 @@ use Composer\DependencyResolver;
use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\Request;
use Composer\DependencyResolver\Operation;
use Composer\Package\AliasPackage;
use Composer\Package\MemoryPackage;
use Composer\Package\Link;
use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Package\PackageInterface;
use Composer\Repository\ArrayRepository;
use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository;
use Composer\Repository\RepositoryInterface;
@ -92,18 +95,36 @@ EOT
$composer->getDownloadManager()->setPreferSource(true);
}
$repoManager = $composer->getRepositoryManager();
// create local repo, this contains all packages that are installed in the local project
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
$localRepo = $repoManager->getLocalRepository();
// create installed repo, this contains all local packages + platform packages (php & extensions)
$installedRepo = new CompositeRepository(array($localRepo, new PlatformRepository()));
if ($additionalInstalledRepository) {
$installedRepo->addRepository($additionalInstalledRepository);
}
// prepare aliased packages
if (!$update && $composer->getLocker()->isLocked()) {
$aliases = $composer->getLocker()->getAliases();
} else {
$aliases = $composer->getPackage()->getAliases();
}
foreach ($aliases as $alias) {
foreach ($repoManager->findPackages($alias['package'], $alias['version']) as $package) {
$package->getRepository()->addPackage(new AliasPackage($package, $alias['alias']));
}
foreach ($repoManager->getLocalRepository()->findPackages($alias['package'], $alias['version']) as $package) {
$repoManager->getLocalRepository()->addPackage(new AliasPackage($package, $alias['alias']));
$repoManager->getLocalRepository()->removePackage($package);
}
}
// creating repository pool
$pool = new Pool;
$pool->addRepository($installedRepo);
foreach ($composer->getRepositoryManager()->getRepositories() as $repository) {
foreach ($repoManager->getRepositories() as $repository) {
$pool->addRepository($repository);
}
@ -118,11 +139,11 @@ EOT
$request = new Request($pool);
if ($update) {
$io->write('<info>Updating dependencies</info>');
$installedPackages = $installedRepo->getPackages();
$links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests);
$request->updateAll();
$links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests);
foreach ($links as $link) {
$request->install($link->getTarget(), $link->getConstraint());
}
@ -135,7 +156,14 @@ EOT
}
foreach ($composer->getLocker()->getLockedPackages() as $package) {
$constraint = new VersionConstraint('=', $package->getVersion());
$version = $package->getVersion();
foreach ($aliases as $alias) {
if ($alias['package'] === $package->getName() && $alias['version'] === $package->getVersion()) {
$version = $alias['alias'];
break;
}
}
$constraint = new VersionConstraint('=', $version);
$request->install($package->getName(), $constraint);
}
} else {
@ -156,14 +184,13 @@ EOT
// solve dependencies
$operations = $solver->solve($request);
// execute operations
if (!$operations) {
$io->write('<info>Nothing to install/update</info>');
}
// force dev packages to be updated to latest reference on update
if ($update) {
foreach ($localRepo->getPackages() as $package) {
if ($package instanceof AliasPackage) {
$package = $package->getAliasOf();
}
// skip non-dev packages
if (!$package->isDev()) {
continue;
@ -179,13 +206,26 @@ EOT
}
// force update
$newPackage = $composer->getRepositoryManager()->findPackage($package->getName(), $package->getVersion());
$newPackage = $repoManager->findPackage($package->getName(), $package->getVersion());
if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) {
$operations[] = new UpdateOperation($package, $newPackage);
}
}
}
// anti-alias local repository to allow updates to work fine
foreach ($repoManager->getLocalRepository()->getPackages() as $package) {
if ($package instanceof AliasPackage) {
$repoManager->getLocalRepository()->addPackage(clone $package->getAliasOf());
$repoManager->getLocalRepository()->removePackage($package);
}
}
// execute operations
if (!$operations) {
$io->write('<info>Nothing to install/update</info>');
}
foreach ($operations as $operation) {
if ($verbose) {
$io->write((string) $operation);
@ -219,7 +259,7 @@ EOT
if (!$dryRun) {
if ($update || !$composer->getLocker()->isLocked()) {
$composer->getLocker()->lockPackages($localRepo->getPackages());
$composer->getLocker()->setLockData($localRepo->getPackages(), $aliases);
$io->write('<info>Writing lock file</info>');
}

View File

@ -118,7 +118,7 @@ EOT
// we only have a name, so search for the highest version of the given package
$highestVersion = null;
foreach ($repos->findPackagesByName($input->getArgument('package')) as $package) {
foreach ($repos->findPackages($input->getArgument('package')) as $package) {
if (null === $highestVersion || version_compare($package->getVersion(), $highestVersion->getVersion(), '>=')) {
$highestVersion = $package;
}
@ -164,7 +164,7 @@ EOT
$versions = array();
foreach ($repos->findPackagesByName($package->getName()) as $version) {
foreach ($repos->findPackages($package->getName()) as $version) {
$versions[] = $version->getPrettyVersion();
}

View File

@ -14,6 +14,7 @@ namespace Composer\DependencyResolver;
use Composer\Repository\RepositoryInterface;
use Composer\Package\PackageInterface;
use Composer\Package\AliasPackage;
use Composer\Package\LinkConstraint\VersionConstraint;
/**
@ -103,6 +104,16 @@ class DefaultPolicy implements PolicyInterface
public function compareByPriorityPreferInstalled(Pool $pool, array $installedMap, PackageInterface $a, PackageInterface $b, $ignoreReplace = false)
{
if ($a->getRepository() === $b->getRepository()) {
if ($a->getName() === $b->getName()) {
$aAliased = $a instanceof AliasPackage;
$bAliased = $b instanceof AliasPackage;
if ($aAliased && !$bAliased) {
return -1; // use a
}
if (!$aAliased && $bAliased) {
return 1; // use b
}
}
if (!$ignoreReplace) {
// return original, not replaced

View File

@ -13,6 +13,7 @@
namespace Composer\Installer;
use Composer\Package\PackageInterface;
use Composer\Package\AliasPackage;
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
@ -123,8 +124,12 @@ class InstallationManager
*/
public function install(InstallOperation $operation)
{
$installer = $this->getInstaller($operation->getPackage()->getType());
$installer->install($operation->getPackage());
$package = $operation->getPackage();
if ($package instanceof AliasPackage) {
$package = $package->getAliasOf();
}
$installer = $this->getInstaller($package->getType());
$installer->install($package);
}
/**
@ -135,7 +140,13 @@ class InstallationManager
public function update(UpdateOperation $operation)
{
$initial = $operation->getInitialPackage();
if ($initial instanceof AliasPackage) {
$initial = $initial->getAliasOf();
}
$target = $operation->getTargetPackage();
if ($target instanceof AliasPackage) {
$target = $target->getAliasOf();
}
$initialType = $initial->getType();
$targetType = $target->getType();
@ -156,8 +167,12 @@ class InstallationManager
*/
public function uninstall(UninstallOperation $operation)
{
$installer = $this->getInstaller($operation->getPackage()->getType());
$installer->uninstall($operation->getPackage());
$package = $operation->getPackage();
if ($package instanceof AliasPackage) {
$package = $package->getAliasOf();
}
$installer = $this->getInstaller($package->getType());
$installer->uninstall($package);
}
/**

View File

@ -0,0 +1,236 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Package;
use Composer\Package\LinkConstraint\LinkConstraintInterface;
use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\PlatformRepository;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class AliasPackage extends BasePackage
{
protected $version;
protected $dev;
protected $aliasOf;
protected $requires;
protected $conflicts;
protected $provides;
protected $replaces;
protected $recommends;
protected $suggests;
/**
* All descendants' constructors should call this parent constructor
*
* @param PackageInterface $aliasOf The package this package is an alias of
* @param string $version The version the alias must report
*/
public function __construct($aliasOf, $version)
{
parent::__construct($aliasOf->getName());
$this->version = $version;
$this->aliasOf = $aliasOf;
$this->dev = 'dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4);
foreach (self::$supportedLinkTypes as $type => $description) {
$links = $aliasOf->{'get'.ucfirst($description)}();
$newLinks = array();
foreach ($links as $link) {
// link is self.version, but must be replacing also the replaced version
if ('self.version' === $link->getPrettyConstraint()) {
$newLinks[] = new Link($link->getSource(), $link->getTarget(), new VersionConstraint('=', $this->version), $description, $this->version);
}
}
$this->$description = array_merge($links, $newLinks);
}
}
public function getAliasOf()
{
return $this->aliasOf;
}
/**
* {@inheritDoc}
*/
public function getVersion()
{
return $this->version;
}
/**
* {@inheritDoc}
*/
public function getPrettyVersion()
{
return $this->version;
}
/**
* {@inheritDoc}
*/
public function isDev()
{
return $this->dev;
}
/**
* {@inheritDoc}
*/
function getRequires()
{
return $this->requires;
}
/**
* {@inheritDoc}
*/
function getConflicts()
{
return $this->conflicts;
}
/**
* {@inheritDoc}
*/
function getProvides()
{
return $this->provides;
}
/**
* {@inheritDoc}
*/
function getReplaces()
{
return $this->replaces;
}
/**
* {@inheritDoc}
*/
function getRecommends()
{
return $this->recommends;
}
/**
* {@inheritDoc}
*/
function getSuggests()
{
return $this->suggests;
}
/***************************************
* Wrappers around the aliased package *
***************************************/
public function getType()
{
return $this->aliasOf->getType();
}
public function getTargetDir()
{
return $this->aliasOf->getTargetDir();
}
public function getExtra()
{
return $this->aliasOf->getExtra();
}
public function setInstallationSource($type)
{
$this->aliasOf->setInstallationSource($type);
}
public function getInstallationSource()
{
return $this->aliasOf->getInstallationSource();
}
public function getSourceType()
{
return $this->aliasOf->getSourceType();
}
public function getSourceUrl()
{
return $this->aliasOf->getSourceUrl();
}
public function getSourceReference()
{
return $this->aliasOf->getSourceReference();
}
public function getDistType()
{
return $this->aliasOf->getDistType();
}
public function getDistUrl()
{
return $this->aliasOf->getDistUrl();
}
public function getDistReference()
{
return $this->aliasOf->getDistReference();
}
public function getDistSha1Checksum()
{
return $this->aliasOf->getDistSha1Checksum();
}
public function getScripts()
{
return $this->aliasOf->getScripts();
}
public function getLicense()
{
return $this->aliasOf->getLicense();
}
public function getAutoload()
{
return $this->aliasOf->getAutoload();
}
public function getRepositories()
{
return $this->aliasOf->getRepositories();
}
public function getReleaseDate()
{
return $this->aliasOf->getReleaseDate();
}
public function getBinaries()
{
return $this->aliasOf->getBinaries();
}
public function getKeywords()
{
return $this->aliasOf->getKeywords();
}
public function getDescription()
{
return $this->aliasOf->getDescription();
}
public function getHomepage()
{
return $this->aliasOf->getHomepage();
}
public function getAuthors()
{
return $this->aliasOf->getAuthors();
}
public function __toString()
{
return $this->aliasOf->__toString();
}
}

View File

@ -24,8 +24,18 @@ use Composer\Repository\PlatformRepository;
*/
abstract class BasePackage implements PackageInterface
{
public static $supportedLinkTypes = array(
'require' => 'requires',
'conflict' => 'conflicts',
'provide' => 'provides',
'replace' => 'replaces',
'recommend' => 'recommends',
'suggest' => 'suggests',
);
protected $name;
protected $prettyName;
protected $repository;
protected $id;

View File

@ -22,15 +22,6 @@ use Composer\Repository\RepositoryManager;
*/
class ArrayLoader
{
protected $supportedLinkTypes = array(
'require' => 'requires',
'conflict' => 'conflicts',
'provide' => 'provides',
'replace' => 'replaces',
'recommend' => 'recommends',
'suggest' => 'suggests',
);
protected $versionParser;
public function __construct(VersionParser $parser = null)
@ -141,7 +132,7 @@ class ArrayLoader
$package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null);
}
foreach ($this->supportedLinkTypes as $type => $description) {
foreach (Package\BasePackage::$supportedLinkTypes as $type => $description) {
if (isset($config[$type])) {
$method = 'set'.ucfirst($description);
$package->{$method}(
@ -162,9 +153,10 @@ class ArrayLoader
$links = array();
foreach ($linksSpecs as $packageName => $constraint) {
if ('self.version' === $constraint) {
$constraint = $package->getPrettyVersion();
$parsedConstraint = $this->versionParser->parseConstraints($package->getPrettyVersion());
} else {
$parsedConstraint = $this->versionParser->parseConstraints($constraint);
}
$parsedConstraint = $this->versionParser->parseConstraints($constraint);
$links[] = new Package\Link($package->getName(), $packageName, $parsedConstraint, $description, $constraint);
}

View File

@ -43,6 +43,21 @@ class RootPackageLoader extends ArrayLoader
$package = parent::load($config);
if (isset($config['require'])) {
$aliases = array();
foreach ($config['require'] as $reqName => $reqVersion) {
if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $reqVersion, $match)) {
$aliases[] = array(
'package' => strtolower($reqName),
'version' => $this->versionParser->normalize($match[1]),
'alias' => $this->versionParser->normalize($match[2]),
);
}
}
$package->setAliases($aliases);
}
if (isset($config['repositories'])) {
foreach ($config['repositories'] as $index => $repo) {
if (isset($repo['packagist']) && $repo['packagist'] === false) {

View File

@ -91,6 +91,12 @@ class Locker
return $packages;
}
public function getAliases()
{
$lockList = $this->getLockData();
return isset($lockList['aliases']) ? $lockList['aliases'] : array();
}
public function getLockData()
{
if (!$this->isLocked()) {
@ -101,15 +107,17 @@ class Locker
}
/**
* Locks provided packages into lockfile.
* Locks provided data into lockfile.
*
* @param array $packages array of packages
* @param array $aliases array of aliases
*/
public function lockPackages(array $packages)
public function setLockData(array $packages, array $aliases)
{
$lock = array(
'hash' => $this->hash,
'packages' => array(),
'aliases' => $aliases,
);
foreach ($packages as $package) {
$name = $package->getPrettyName();

View File

@ -41,6 +41,7 @@ class MemoryPackage extends BasePackage
protected $extra = array();
protected $binaries = array();
protected $scripts = array();
protected $aliases = array();
protected $dev;
protected $requires = array();
@ -156,6 +157,22 @@ class MemoryPackage extends BasePackage
return $this->scripts;
}
/**
* @param array $aliases
*/
public function setAliases(array $aliases)
{
$this->aliases = $aliases;
}
/**
* {@inheritDoc}
*/
public function getAliases()
{
return $this->aliases;
}
/**
* {@inheritDoc}
*/

View File

@ -293,6 +293,13 @@ interface PackageInterface
*/
function getDescription();
/**
* Returns the package binaries
*
* @return string
*/
function getBinaries();
/**
* Returns the package homepage
*

View File

@ -34,6 +34,11 @@ class VersionParser
{
$version = trim($version);
// ignore aliases and just assume the alias is required instead of the source
if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $version, $match)) {
$version = $match[2];
}
// match master-like branches
if (preg_match('{^(?:dev-)?(?:master|trunk|default)$}i', $version)) {
return '9999999-dev';

View File

@ -44,14 +44,21 @@ class ArrayRepository implements RepositoryInterface
/**
* {@inheritDoc}
*/
public function findPackagesByName($name)
public function findPackages($name, $version = null)
{
// normalize name
$name = strtolower($name);
// normalize version
if (null !== $version) {
$versionParser = new VersionParser();
$version = $versionParser->normalize($version);
}
$packages = array();
foreach ($this->getPackages() as $package) {
if ($package->getName() === $name) {
if ($package->getName() === $name && (null === $version || $version === $package->getVersion())) {
$packages[] = $package;
}
}

View File

@ -68,12 +68,12 @@ class CompositeRepository implements RepositoryInterface
/**
* {@inheritdoc}
*/
public function findPackagesByName($name)
public function findPackages($name, $version = null)
{
$packages = array();
foreach ($this->repositories as $repository) {
/* @var $repository RepositoryInterface */
$packages[] = $repository->findPackagesByName($name);
$packages[] = $repository->findPackages($name, $version);
}
return call_user_func_array('array_merge', $packages);
}

View File

@ -32,7 +32,7 @@ interface RepositoryInterface extends \Countable
function hasPackage(PackageInterface $package);
/**
* Searches for a package by it's name and version (if has one).
* Searches for the first match of a package by name and version.
*
* @param string $name package name
* @param string $version package version
@ -42,13 +42,14 @@ interface RepositoryInterface extends \Countable
function findPackage($name, $version);
/**
* Searches for packages by it's name.
* Searches for all packages matching a name and optionally a version.
*
* @param string $name package name
* @param string $version package version
*
* @return array
*/
function findPackagesByName($name);
function findPackages($name, $version = null);
/**
* Returns list of registered packages.

View File

@ -50,6 +50,25 @@ class RepositoryManager
}
}
/**
* Searches for all packages matching a name and optionally a version in managed repositories.
*
* @param string $name package name
* @param string $version package version
*
* @return array
*/
public function findPackages($name, $version)
{
$packages = array();
foreach ($this->repositories as $repository) {
$packages = array_merge($packages, $repository->findPackages($name, $version));
}
return $packages;
}
/**
* Adds repository
*

View File

@ -114,7 +114,7 @@ class LockerTest extends \PHPUnit_Framework_TestCase
$locker->getLockedPackages();
}
public function testLockPackages()
public function testSetLockData()
{
$json = $this->createJsonFileMock();
$repo = $this->createRepositoryManagerMock();
@ -151,9 +151,10 @@ class LockerTest extends \PHPUnit_Framework_TestCase
array('package' => 'pkg1', 'version' => '1.0.0-beta'),
array('package' => 'pkg2', 'version' => '0.1.10')
),
'aliases' => array(),
));
$locker->lockPackages(array($package1, $package2));
$locker->setLockData(array($package1, $package2), array());
}
public function testLockBadPackages()
@ -171,7 +172,7 @@ class LockerTest extends \PHPUnit_Framework_TestCase
$this->setExpectedException('LogicException');
$locker->lockPackages(array($package1));
$locker->setLockData(array($package1), array());
}
public function testIsFresh()

View File

@ -53,6 +53,7 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase
'parses trunk' => array('dev-trunk', '9999999-dev'),
'parses arbitrary' => array('dev-feature-foo', 'dev-feature-foo'),
'parses arbitrary2' => array('DEV-FOOBAR', 'dev-foobar'),
'ignores aliases' => array('dev-master as 1.0.0', '1.0.0.0'),
);
}
@ -128,6 +129,7 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase
'accepts master' => array('>=dev-master', new VersionConstraint('>=', '9999999-dev')),
'accepts master/2' => array('dev-master', new VersionConstraint('=', '9999999-dev')),
'accepts arbitrary' => array('dev-feature-a', new VersionConstraint('=', 'dev-feature-a')),
'ignores aliases' => array('dev-master as 1.0.0', new VersionConstraint('=', '1.0.0.0')),
);
}

View File

@ -51,18 +51,18 @@ class ArrayRepositoryTest extends TestCase
$this->assertFalse($repo->hasPackage($this->getPackage('bar', '1')));
}
public function testFindPackagesByName()
public function testFindPackages()
{
$repo = new ArrayRepository();
$repo->addPackage($this->getPackage('foo', '1'));
$repo->addPackage($this->getPackage('bar', '2'));
$repo->addPackage($this->getPackage('bar', '3'));
$foo = $repo->findPackagesByName('foo');
$foo = $repo->findPackages('foo');
$this->assertCount(1, $foo);
$this->assertEquals('foo', $foo[0]->getName());
$bar = $repo->findPackagesByName('bar');
$bar = $repo->findPackages('bar');
$this->assertCount(2, $bar);
$this->assertEquals('bar', $bar[0]->getName());
}

View File

@ -52,7 +52,7 @@ class CompositeRepositoryTest extends TestCase
$this->assertNull($repo->findPackage('foo', '2'), "Should not find package 'foo/2'");
}
public function testFindPackagesByName()
public function testFindPackages()
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
@ -66,15 +66,15 @@ class CompositeRepositoryTest extends TestCase
$repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
$bats = $repo->findPackagesByName('bat');
$bats = $repo->findPackages('bat');
$this->assertCount(1, $bats, "Should find one instance of 'bats' (defined in just one repository)");
$this->assertEquals('bat', $bats[0]->getName(), "Should find packages named 'bat'");
$bars = $repo->findPackagesByName('bar');
$bars = $repo->findPackages('bar');
$this->assertCount(2, $bars, "Should find two instances of 'bar' (both defined in the same repository)");
$this->assertEquals('bar', $bars[0]->getName(), "Should find packages named 'bar'");
$foos = $repo->findPackagesByName('foo');
$foos = $repo->findPackages('foo');
$this->assertCount(3, $foos, "Should find three instances of 'foo' (two defined in one repository, the third in the other)");
$this->assertEquals('foo', $foos[0]->getName(), "Should find packages named 'foo'");
}