From a4c829749e40978018f966bae3212da6222ba337 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 24 Jun 2012 20:11:06 +0200 Subject: [PATCH] Expose the whole composer context to the custom installers, fixes #672 --- src/Composer/Factory.php | 36 +++++++++------ src/Composer/Installer/InstallerInstaller.php | 20 ++++---- src/Composer/Installer/LibraryInstaller.php | 22 ++++----- .../Test/Installer/InstallerInstallerTest.php | 46 ++++++++++++++----- .../Test/Installer/LibraryInstallerTest.php | 33 +++++++++---- tests/Composer/Test/Mock/FactoryMock.php | 7 ++- 6 files changed, 104 insertions(+), 60 deletions(-) diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 01f8cc390..18567c14b 100644 --- a/src/Composer/Factory.php +++ b/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)); } /** diff --git a/src/Composer/Installer/InstallerInstaller.php b/src/Composer/Installer/InstallerInstaller.php index 70bf22b9c..315334c84 100644 --- a/src/Composer/Installer/InstallerInstaller.php +++ b/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); } } diff --git a/src/Composer/Installer/LibraryInstaller.php b/src/Composer/Installer/LibraryInstaller.php index cf5805c38..6652edec8 100644 --- a/src/Composer/Installer/LibraryInstaller.php +++ b/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'), '/'); } /** diff --git a/tests/Composer/Test/Installer/InstallerInstallerTest.php b/tests/Composer/Test/Installer/InstallerInstallerTest.php index 33c941600..db84105b2 100644 --- a/tests/Composer/Test/Installer/InstallerInstallerTest.php +++ b/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 diff --git a/tests/Composer/Test/Installer/LibraryInstallerTest.php b/tests/Composer/Test/Installer/LibraryInstallerTest.php index 4fa83dfe9..4badc2f37 100644 --- a/tests/Composer/Test/Installer/LibraryInstallerTest.php +++ b/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 diff --git a/tests/Composer/Test/Mock/FactoryMock.php b/tests/Composer/Test/Mock/FactoryMock.php index c98b378cb..c4014a0f3 100644 --- a/tests/Composer/Test/Mock/FactoryMock.php +++ b/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) { }