Browse Source

Merge pull request #832 from Seldaek/instinterface

Expose the whole composer context to the custom installers
Nils Adermann 12 years ago
parent
commit
11e67bdf52

+ 21 - 15
src/Composer/Factory.php

@@ -158,10 +158,7 @@ class Factory
         $dm = $this->createDownloadManager($io);
 
         // initialize installation manager
-        $im = $this->createInstallationManager($rm, $dm, $vendorDir, $binDir, $io);
-
-        // purge packages if they have been deleted on the filesystem
-        $this->purgePackages($rm, $im);
+        $im = $this->createInstallationManager($config);
 
         // initialize composer
         $composer = new Composer();
@@ -171,6 +168,12 @@ class Factory
         $composer->setDownloadManager($dm);
         $composer->setInstallationManager($im);
 
+        // add installers to the manager
+        $this->createDefaultInstallers($im, $composer, $io);
+
+        // purge packages if they have been deleted on the filesystem
+        $this->purgePackages($rm, $im);
+
         // init locker if possible
         if (isset($composerFile)) {
             $lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
@@ -232,21 +235,24 @@ class Factory
     }
 
     /**
-     * @param  Repository\RepositoryManager  $rm
-     * @param  Downloader\DownloadManager    $dm
-     * @param  string                        $vendorDir
-     * @param  string                        $binDir
-     * @param  IO\IOInterface                $io
+     * @param  Config                        $config
      * @return Installer\InstallationManager
      */
-    protected function createInstallationManager(Repository\RepositoryManager $rm, Downloader\DownloadManager $dm, $vendorDir, $binDir, IOInterface $io)
+    protected function createInstallationManager(Config $config)
     {
-        $im = new Installer\InstallationManager($vendorDir);
-        $im->addInstaller(new Installer\LibraryInstaller($vendorDir, $binDir, $dm, $io, null));
-        $im->addInstaller(new Installer\InstallerInstaller($vendorDir, $binDir, $dm, $io, $im, $rm->getLocalRepositories()));
-        $im->addInstaller(new Installer\MetapackageInstaller($io));
+        return new Installer\InstallationManager($config->get('vendor-dir'));
+    }
 
-        return $im;
+    /**
+     * @param Installer\InstallationManager $im
+     * @param Composer                      $composer
+     * @param IO\IOInterface                $io
+     */
+    protected function createDefaultInstallers(Installer\InstallationManager $im, Composer $composer, IOInterface $io)
+    {
+        $im->addInstaller(new Installer\LibraryInstaller($io, $composer, null));
+        $im->addInstaller(new Installer\InstallerInstaller($io, $composer));
+        $im->addInstaller(new Installer\MetapackageInstaller($io));
     }
 
     /**

+ 8 - 12
src/Composer/Installer/InstallerInstaller.php

@@ -12,9 +12,9 @@
 
 namespace Composer\Installer;
 
+use Composer\Composer;
 use Composer\IO\IOInterface;
 use Composer\Autoload\AutoloadGenerator;
-use Composer\Downloader\DownloadManager;
 use Composer\Repository\InstalledRepositoryInterface;
 use Composer\Package\PackageInterface;
 
@@ -29,19 +29,15 @@ class InstallerInstaller extends LibraryInstaller
     private static $classCounter = 0;
 
     /**
-     * @param string              $vendorDir         relative path for packages home
-     * @param string              $binDir            relative path for binaries
-     * @param DownloadManager     $dm                download manager
-     * @param IOInterface         $io                io instance
-     * @param InstallationManager $im                installation manager
-     * @param array               $localRepositories array of InstalledRepositoryInterface
+     * @param IOInterface $io
+     * @param Composer    $composer
      */
-    public function __construct($vendorDir, $binDir, DownloadManager $dm, IOInterface $io, InstallationManager $im, array $localRepositories)
+    public function __construct(IOInterface $io, Composer $composer, $type = 'library')
     {
-        parent::__construct($vendorDir, $binDir, $dm, $io, 'composer-installer');
-        $this->installationManager = $im;
+        parent::__construct($io, $composer, 'composer-installer');
+        $this->installationManager = $composer->getInstallationManager();
 
-        foreach ($localRepositories as $repo) {
+        foreach ($composer->getRepositoryManager()->getLocalRepositories() as $repo) {
             foreach ($repo->getPackages() as $package) {
                 if ('composer-installer' === $package->getType()) {
                     $this->registerInstaller($package);
@@ -99,7 +95,7 @@ class InstallerInstaller extends LibraryInstaller
                 self::$classCounter++;
             }
 
-            $installer = new $class($this->vendorDir, $this->binDir, $this->downloadManager, $this->io);
+            $installer = new $class($this->io, $this->composer);
             $this->installationManager->addInstaller($installer);
         }
     }

+ 11 - 11
src/Composer/Installer/LibraryInstaller.php

@@ -12,6 +12,7 @@
 
 namespace Composer\Installer;
 
+use Composer\Composer;
 use Composer\IO\IOInterface;
 use Composer\Downloader\DownloadManager;
 use Composer\Repository\InstalledRepositoryInterface;
@@ -26,31 +27,30 @@ use Composer\Util\Filesystem;
  */
 class LibraryInstaller implements InstallerInterface
 {
+    protected $composer;
     protected $vendorDir;
     protected $binDir;
     protected $downloadManager;
     protected $io;
-    private $type;
-    private $filesystem;
+    protected $type;
+    protected $filesystem;
 
     /**
      * Initializes library installer.
      *
-     * @param string          $vendorDir relative path for packages home
-     * @param string          $binDir    relative path for binaries
-     * @param DownloadManager $dm        download manager
-     * @param IOInterface     $io        io instance
-     * @param string          $type      package type that this installer handles
+     * @param IOInterface $io
+     * @param Composer    $composer
      */
-    public function __construct($vendorDir, $binDir, DownloadManager $dm, IOInterface $io, $type = 'library')
+    public function __construct(IOInterface $io, Composer $composer, $type = 'library')
     {
-        $this->downloadManager = $dm;
+        $this->composer = $composer;
+        $this->downloadManager = $composer->getDownloadManager();
         $this->io = $io;
         $this->type = $type;
 
         $this->filesystem = new Filesystem();
-        $this->vendorDir = rtrim($vendorDir, '/');
-        $this->binDir = rtrim($binDir, '/');
+        $this->vendorDir = rtrim($composer->getConfig()->get('vendor-dir'), '/');
+        $this->binDir = rtrim($composer->getConfig()->get('bin-dir'), '/');
     }
 
     /**

+ 34 - 12
tests/Composer/Test/Installer/InstallerInstallerTest.php

@@ -12,12 +12,20 @@
 
 namespace Composer\Test\Installer;
 
+use Composer\Composer;
+use Composer\Config;
 use Composer\Installer\InstallerInstaller;
 use Composer\Package\Loader\JsonLoader;
 use Composer\Package\PackageInterface;
 
 class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
 {
+    protected $composer;
+    protected $packages;
+    protected $im;
+    protected $repository;
+    protected $io;
+
     protected function setUp()
     {
         $loader = new JsonLoader();
@@ -26,7 +34,7 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
             $this->packages[] = $loader->load(__DIR__.'/Fixtures/installer-v'.$i.'/composer.json');
         }
 
-        $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
+        $dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
             ->disableOriginalConstructor()
             ->getMock();
 
@@ -36,7 +44,28 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
 
         $this->repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface');
 
+        $rm = $this->getMockBuilder('Composer\Repository\RepositoryManager')
+            ->disableOriginalConstructor()
+            ->getMock();
+        $rm->expects($this->any())
+            ->method('getLocalRepositories')
+            ->will($this->returnValue(array($this->repository)));
+
         $this->io = $this->getMock('Composer\IO\IOInterface');
+
+        $this->composer = new Composer();
+        $config = new Config();
+        $this->composer->setConfig($config);
+        $this->composer->setDownloadManager($dm);
+        $this->composer->setInstallationManager($this->im);
+        $this->composer->setRepositoryManager($rm);
+
+        $config->merge(array(
+            'config' => array(
+                'vendor-dir' => __DIR__.'/Fixtures/',
+                'bin-dir' => __DIR__.'/Fixtures/bin',
+            ),
+        ));
     }
 
     public function testInstallNewInstaller()
@@ -45,7 +74,7 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
             ->expects($this->once())
             ->method('getPackages')
             ->will($this->returnValue(array()));
-        $installer = new InstallerInstallerMock(__DIR__.'/Fixtures/', __DIR__.'/Fixtures/bin', $this->dm, $this->io, $this->im, array($this->repository));
+        $installer = new InstallerInstallerMock($this->io, $this->composer);
 
         $test = $this;
         $this->im
@@ -65,14 +94,7 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
             ->method('getPackages')
             ->will($this->returnValue(array()));
 
-        $installer = new InstallerInstallerMock(
-            __DIR__.'/Fixtures/',
-            __DIR__.'/Fixtures/bin',
-            $this->dm,
-            $this->io,
-            $this->im,
-            array($this->repository)
-        );
+        $installer = new InstallerInstallerMock($this->io, $this->composer);
 
         $test = $this;
 
@@ -105,7 +127,7 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
             ->expects($this->exactly(2))
             ->method('hasPackage')
             ->will($this->onConsecutiveCalls(true, false));
-        $installer = new InstallerInstallerMock(__DIR__.'/Fixtures/', __DIR__.'/Fixtures/bin', $this->dm, $this->io, $this->im, array($this->repository));
+        $installer = new InstallerInstallerMock($this->io, $this->composer);
 
         $test = $this;
         $this->im
@@ -128,7 +150,7 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
             ->expects($this->exactly(2))
             ->method('hasPackage')
             ->will($this->onConsecutiveCalls(true, false));
-        $installer = new InstallerInstallerMock(__DIR__.'/Fixtures/', __DIR__.'/Fixtures/bin', $this->dm, $this->io, $this->im, array($this->repository));
+        $installer = new InstallerInstallerMock($this->io, $this->composer);
 
         $test = $this;
         $this->im

+ 24 - 9
tests/Composer/Test/Installer/LibraryInstallerTest.php

@@ -15,9 +15,13 @@ namespace Composer\Test\Installer;
 use Composer\Installer\LibraryInstaller;
 use Composer\Util\Filesystem;
 use Composer\Test\TestCase;
+use Composer\Composer;
+use Composer\Config;
 
 class LibraryInstallerTest extends TestCase
 {
+    private $composer;
+    private $config;
     private $vendorDir;
     private $binDir;
     private $dm;
@@ -29,18 +33,29 @@ class LibraryInstallerTest extends TestCase
     {
         $this->fs = new Filesystem;
 
+        $this->composer = new Composer();
+        $this->config = new Config();
+        $this->composer->setConfig($this->config);
+
         $this->vendorDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-vendor';
         $this->ensureDirectoryExistsAndClear($this->vendorDir);
 
         $this->binDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-bin';
         $this->ensureDirectoryExistsAndClear($this->binDir);
 
+        $this->config->merge(array(
+            'config' => array(
+                'vendor-dir' => $this->vendorDir,
+                'bin-dir' => $this->binDir,
+            ),
+        ));
+
         $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
             ->disableOriginalConstructor()
             ->getMock();
+        $this->composer->setDownloadManager($this->dm);
 
         $this->repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface');
-
         $this->io = $this->getMock('Composer\IO\IOInterface');
     }
 
@@ -54,7 +69,7 @@ class LibraryInstallerTest extends TestCase
     {
         $this->fs->removeDirectory($this->vendorDir);
 
-        new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->io);
+        new LibraryInstaller($this->io, $this->composer);
         $this->assertFileNotExists($this->vendorDir);
     }
 
@@ -62,13 +77,13 @@ class LibraryInstallerTest extends TestCase
     {
         $this->fs->removeDirectory($this->binDir);
 
-        new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->io);
+        new LibraryInstaller($this->io, $this->composer);
         $this->assertFileNotExists($this->binDir);
     }
 
     public function testIsInstalled()
     {
-        $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->io);
+        $library = new LibraryInstaller($this->io, $this->composer);
         $package = $this->createPackageMock();
 
         $this->repository
@@ -87,7 +102,7 @@ class LibraryInstallerTest extends TestCase
      */
     public function testInstall()
     {
-        $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->io);
+        $library = new LibraryInstaller($this->io, $this->composer);
         $package = $this->createPackageMock();
 
         $package
@@ -116,7 +131,7 @@ class LibraryInstallerTest extends TestCase
      */
     public function testUpdate()
     {
-        $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->io);
+        $library = new LibraryInstaller($this->io, $this->composer);
         $initial = $this->createPackageMock();
         $target  = $this->createPackageMock();
 
@@ -156,7 +171,7 @@ class LibraryInstallerTest extends TestCase
 
     public function testUninstall()
     {
-        $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->io);
+        $library = new LibraryInstaller($this->io, $this->composer);
         $package = $this->createPackageMock();
 
         $package
@@ -190,7 +205,7 @@ class LibraryInstallerTest extends TestCase
 
     public function testGetInstallPath()
     {
-        $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->io);
+        $library = new LibraryInstaller($this->io, $this->composer);
         $package = $this->createPackageMock();
 
         $package
@@ -203,7 +218,7 @@ class LibraryInstallerTest extends TestCase
 
     public function testGetInstallPathWithTargetDir()
     {
-        $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->io);
+        $library = new LibraryInstaller($this->io, $this->composer);
         $package = $this->createPackageMock();
 
         $package

+ 6 - 1
tests/Composer/Test/Mock/FactoryMock.php

@@ -11,6 +11,7 @@
 
 namespace Composer\Test\Mock;
 
+use Composer\Composer;
 use Composer\Config;
 use Composer\Factory;
 use Composer\Repository;
@@ -37,11 +38,15 @@ class FactoryMock extends Factory
     {
     }
 
-    protected function createInstallationManager(Repository\RepositoryManager $rm, Downloader\DownloadManager $dm, $vendorDir, $binDir, IOInterface $io)
+    protected function createInstallationManager(Config $config)
     {
         return new InstallationManagerMock;
     }
 
+    protected function createDefaultInstallers(Installer\InstallationManager $im, Composer $composer, IOInterface $io)
+    {
+    }
+
     protected function purgePackages(Repository\RepositoryManager $rm, Installer\InstallationManager $im)
     {
     }