Merge pull request #832 from Seldaek/instinterface

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

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

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

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

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

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

@ -11,6 +11,7 @@
namespace Composer\Test\Mock; namespace Composer\Test\Mock;
use Composer\Composer;
use Composer\Config; use Composer\Config;
use Composer\Factory; use Composer\Factory;
use Composer\Repository; 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; return new InstallationManagerMock;
} }
protected function createDefaultInstallers(Installer\InstallationManager $im, Composer $composer, IOInterface $io)
{
}
protected function purgePackages(Repository\RepositoryManager $rm, Installer\InstallationManager $im) protected function purgePackages(Repository\RepositoryManager $rm, Installer\InstallationManager $im)
{ {
} }

Loading…
Cancel
Save