Browse Source

Added support for the alias of an aliased package

Martin Hasoň 12 years ago
parent
commit
753a8345cb

+ 5 - 3
src/Composer/DependencyResolver/Pool.php

@@ -170,12 +170,14 @@ class Pool
                         $name = $package->getName();
                         if (isset($rootAliases[$name][$package->getVersion()])) {
                             $alias = $rootAliases[$name][$package->getVersion()];
-                            $package->setAlias($alias['alias_normalized']);
-                            $package->setPrettyAlias($alias['alias']);
-                            $package->getRepository()->addPackage($aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']));
+                            if ($package instanceof AliasPackage) {
+                                $package = $package->getAliasOf();
+                            }
+                            $aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']);
                             $aliasPackage->setRootPackageAlias(true);
                             $aliasPackage->setId($this->id++);
 
+                            $package->getRepository()->addPackage($aliasPackage);
                             $this->packages[] = $aliasPackage;
 
                             foreach ($aliasPackage->getNames() as $name) {

+ 0 - 2
src/Composer/Installer.php

@@ -687,8 +687,6 @@ class Installer
             foreach ($versions as $version => $alias) {
                 $packages = $platformRepo->findPackages($package, $version);
                 foreach ($packages as $package) {
-                    $package->setAlias($alias['alias_normalized']);
-                    $package->setPrettyAlias($alias['alias']);
                     $aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']);
                     $aliasPackage->setRootPackageAlias(true);
                     $platformRepo->addPackage($aliasPackage);

+ 0 - 24
src/Composer/Package/AliasPackage.php

@@ -175,22 +175,6 @@ class AliasPackage extends BasePackage implements CompletePackageInterface
         return $this->rootPackageAlias;
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    public function getAlias()
-    {
-        return '';
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function getPrettyAlias()
-    {
-        return '';
-    }
-
     /***************************************
      * Wrappers around the aliased package *
      ***************************************/
@@ -251,14 +235,6 @@ class AliasPackage extends BasePackage implements CompletePackageInterface
     {
         return $this->aliasOf->getScripts();
     }
-    public function setAliases(array $aliases)
-    {
-        return $this->aliasOf->setAliases($aliases);
-    }
-    public function getAliases()
-    {
-        return $this->aliasOf->getAliases();
-    }
     public function getLicense()
     {
         return $this->aliasOf->getLicense();

+ 5 - 5
src/Composer/Package/Loader/ArrayLoader.php

@@ -13,6 +13,7 @@
 namespace Composer\Package\Loader;
 
 use Composer\Package;
+use Composer\Package\AliasPackage;
 use Composer\Package\Version\VersionParser;
 
 /**
@@ -100,11 +101,6 @@ class ArrayLoader implements LoaderInterface
             $package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null);
         }
 
-        if ($aliasNormalized = $this->getBranchAlias($config)) {
-            $package->setAlias($aliasNormalized);
-            $package->setPrettyAlias(preg_replace('{(\.9{7})+}', '.x', $aliasNormalized));
-        }
-
         foreach (Package\BasePackage::$supportedLinkTypes as $type => $opts) {
             if (isset($config[$type])) {
                 $method = 'set'.ucfirst($opts['method']);
@@ -187,6 +183,10 @@ class ArrayLoader implements LoaderInterface
             }
         }
 
+        if ($aliasNormalized = $this->getBranchAlias($config)) {
+            $package = new AliasPackage($package, $aliasNormalized, preg_replace('{(\.9{7})+}', '.x', $aliasNormalized));
+        }
+
         return $package;
     }
 

+ 0 - 51
src/Composer/Package/Package.php

@@ -36,9 +36,6 @@ class Package extends BasePackage
     protected $releaseDate;
     protected $extra = array();
     protected $binaries = array();
-    protected $aliases = array();
-    protected $alias;
-    protected $prettyAlias;
     protected $dev;
     protected $stability;
     protected $notificationUrl;
@@ -155,54 +152,6 @@ class Package extends BasePackage
         return $this->binaries;
     }
 
-    /**
-     * @param array $aliases
-     */
-    public function setAliases(array $aliases)
-    {
-        $this->aliases = $aliases;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function getAliases()
-    {
-        return $this->aliases;
-    }
-
-    /**
-     * @param string $alias
-     */
-    public function setAlias($alias)
-    {
-        $this->alias = $alias;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function getAlias()
-    {
-        return $this->alias;
-    }
-
-    /**
-     * @param string $prettyAlias
-     */
-    public function setPrettyAlias($prettyAlias)
-    {
-        $this->prettyAlias = $prettyAlias;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public function getPrettyAlias()
-    {
-        return $this->prettyAlias;
-    }
-
     /**
      * {@inheritDoc}
      */

+ 0 - 14
src/Composer/Package/PackageInterface.php

@@ -267,20 +267,6 @@ interface PackageInterface
      */
     public function getBinaries();
 
-    /**
-     * Returns a version this package should be aliased to
-     *
-     * @return string
-     */
-    public function getAlias();
-
-    /**
-     * Returns a non-normalized version this package should be aliased to
-     *
-     * @return string
-     */
-    public function getPrettyAlias();
-
     /**
      * Returns package unique name, constructed from name and version.
      *

+ 19 - 0
src/Composer/Package/RootPackage.php

@@ -23,6 +23,7 @@ class RootPackage extends CompletePackage implements RootPackageInterface
     protected $preferStable = false;
     protected $stabilityFlags = array();
     protected $references = array();
+    protected $aliases = array();
 
     /**
      * Set the minimumStability
@@ -95,4 +96,22 @@ class RootPackage extends CompletePackage implements RootPackageInterface
     {
         return $this->references;
     }
+
+    /**
+     * Set the aliases
+     *
+     * @param array $aliases
+     */
+    public function setAliases(array $aliases)
+    {
+        $this->aliases = $aliases;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getAliases()
+    {
+        return $this->aliases;
+    }
 }

+ 7 - 0
src/Composer/Package/RootPackageInterface.php

@@ -19,6 +19,13 @@ namespace Composer\Package;
  */
 interface RootPackageInterface extends CompletePackageInterface
 {
+    /**
+     * Returns a set of package names and theirs aliases
+     *
+     * @return array
+     */
+    public function getAliases();
+
     /**
      * Returns the minimum stability of the package
      *

+ 6 - 7
src/Composer/Repository/ArrayRepository.php

@@ -124,18 +124,17 @@ class ArrayRepository implements RepositoryInterface
         $package->setRepository($this);
         $this->packages[] = $package;
 
-        // create alias package on the fly if needed
-        if ($package->getAlias()) {
-            $alias = $this->createAliasPackage($package);
-            if (!$this->hasPackage($alias)) {
-                $this->addPackage($alias);
+        if ($package instanceof AliasPackage) {
+            $aliasedPackage = $package->getAliasOf();
+            if (null === $aliasedPackage->getRepository()) {
+                $this->addPackage($aliasedPackage);
             }
         }
     }
 
-    protected function createAliasPackage(PackageInterface $package, $alias = null, $prettyAlias = null)
+    protected function createAliasPackage(PackageInterface $package, $alias, $prettyAlias)
     {
-        return new AliasPackage($package, $alias ?: $package->getAlias(), $prettyAlias ?: $package->getPrettyAlias());
+        return new AliasPackage($package instanceof AliasPackage ? $package->getAliasOf() : $package, $alias, $prettyAlias);
     }
 
     /**

+ 11 - 9
src/Composer/Repository/ComposerRepository.php

@@ -322,16 +322,18 @@ class ComposerRepository extends ArrayRepository implements StreamableRepository
                     $package = $this->createPackage($version, 'Composer\Package\Package');
                     $package->setRepository($this);
 
-                    $this->providers[$name][$version['uid']] = $package;
-                    $this->providersByUid[$version['uid']] = $package;
+                    if ($package instanceof AliasPackage) {
+                        $aliased = $package->getAliasOf();
+                        $aliased->setRepository($this);
 
-                    if ($package->getAlias()) {
-                        $alias = $this->createAliasPackage($package);
-                        $alias->setRepository($this);
+                        $this->providers[$name][$version['uid']] = $aliased;
+                        $this->providers[$name][$version['uid'].'-alias'] = $package;
 
-                        $this->providers[$name][$version['uid'].'-alias'] = $alias;
                         // override provider with its alias so it can be expanded in the if block above
-                        $this->providersByUid[$version['uid']] = $alias;
+                        $this->providersByUid[$version['uid']] = $package;
+                    } else {
+                        $this->providers[$name][$version['uid']] = $package;
+                        $this->providersByUid[$version['uid']] = $package;
                     }
 
                     // handle root package aliases
@@ -339,8 +341,8 @@ class ComposerRepository extends ArrayRepository implements StreamableRepository
 
                     if (isset($this->rootAliases[$name][$package->getVersion()])) {
                         $rootAliasData = $this->rootAliases[$name][$package->getVersion()];
-                    } elseif (($aliasNormalized = $package->getAlias()) && isset($this->rootAliases[$name][$aliasNormalized])) {
-                        $rootAliasData = $this->rootAliases[$name][$aliasNormalized];
+                    } elseif ($package instanceof AliasPackage && isset($this->rootAliases[$name][$package->getAliasOf()->getVersion()])) {
+                        $rootAliasData = $this->rootAliases[$name][$package->getAliasOf()->getVersion()];
                     }
 
                     if (isset($rootAliasData)) {

+ 36 - 0
tests/Composer/Test/Fixtures/installer/install-aliased-alias.test

@@ -0,0 +1,36 @@
+--TEST--
+Installing double aliased package
+--COMPOSER--
+{
+    "repositories": [
+        {
+            "type": "package",
+            "package": [
+                {
+                    "name": "a/a", "version": "dev-master",
+                    "dist": { "type": "file", "url": "" },
+                    "require": {
+                        "b/b": "dev-master"
+                    }
+                },
+                {
+                    "name": "b/b", "version": "dev-foo",
+                    "extra": { "branch-alias": { "dev-foo": "1.0.x-dev" } },
+                    "dist": { "type": "file", "url": "" }
+                }
+            ]
+        }
+    ],
+    "require": {
+        "a/a": "dev-master",
+        "b/b": "1.0.x-dev as dev-master"
+    },
+    "minimum-stability": "dev"
+}
+--RUN--
+install
+--EXPECT--
+Installing b/b (dev-foo)
+Marking b/b (dev-master) as installed, alias of b/b (dev-foo)
+Installing a/a (dev-master)
+Marking b/b (1.0.x-dev) as installed, alias of b/b (dev-foo)

+ 3 - 4
tests/Composer/Test/Mock/InstallationManagerMock.php

@@ -67,16 +67,15 @@ class InstallationManagerMock extends InstallationManager
         $this->installed[] = $package;
         $this->trace[] = (string) $operation;
 
-        if (!$repo->hasPackage($package)) {
-            $repo->addPackage($package);
-        }
+        parent::markAliasInstalled($repo, $operation);
     }
 
     public function markAliasUninstalled(RepositoryInterface $repo, MarkAliasUninstalledOperation $operation)
     {
         $this->uninstalled[] = $operation->getPackage();
         $this->trace[] = (string) $operation;
-        $repo->removePackage($operation->getPackage());
+
+        parent::markAliasUninstalled($repo, $operation);
     }
 
     public function getTrace()

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

@@ -123,4 +123,18 @@ class ArrayLoaderTest extends \PHPUnit_Framework_TestCase
         $dumper = new ArrayDumper;
         $this->assertEquals($config, $dumper->dump($package));
     }
+
+    public function testPackageWithBranchAlias()
+    {
+        $config = array(
+            'name' => 'A',
+            'version' => 'dev-master',
+            'extra' => array('branch-alias' => array('dev-master' => '1.0.x-dev')),
+        );
+
+        $package = $this->loader->load($config);
+
+        $this->assertInstanceOf('Composer\Package\AliasPackage', $package);
+        $this->assertEquals('1.0.x-dev', $package->getPrettyVersion());
+    }
 }

+ 14 - 0
tests/Composer/Test/Repository/ArrayRepositoryTest.php

@@ -66,4 +66,18 @@ class ArrayRepositoryTest extends TestCase
         $this->assertCount(2, $bar);
         $this->assertEquals('bar', $bar[0]->getName());
     }
+
+    public function testAutomaticallyAddAliasedPackage()
+    {
+        $repo = new ArrayRepository();
+
+        $package = $this->getPackage('foo', '1');
+        $alias = $this->getAliasPackage($package, '2');
+
+        $repo->addPackage($alias);
+
+        $this->assertEquals(2, count($repo));
+        $this->assertTrue($repo->hasPackage($this->getPackage('foo', '1')));
+        $this->assertTrue($repo->hasPackage($this->getPackage('foo', '2')));
+    }
 }