Merge pull request #735 from ajshort/composer-installer-multiple
Support multiple installers in a composer-installer package.pull/736/merge
commit
1127cd5c8a
|
@ -66,7 +66,8 @@ requirements:
|
||||||
|
|
||||||
1. the [type][1] attribute must be `composer-installer`.
|
1. the [type][1] attribute must be `composer-installer`.
|
||||||
2. the [extra][2] attribute must contain an element `class` defining the
|
2. the [extra][2] attribute must contain an element `class` defining the
|
||||||
class name of the installer (including namespace).
|
class name of the installer (including namespace). If a package contains
|
||||||
|
multiple installers this can be array of class names.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
|
|
@ -83,13 +83,14 @@ class InstallerInstaller extends LibraryInstaller
|
||||||
$downloadPath = $this->getInstallPath($package);
|
$downloadPath = $this->getInstallPath($package);
|
||||||
|
|
||||||
$extra = $package->getExtra();
|
$extra = $package->getExtra();
|
||||||
$class = $extra['class'];
|
$classes = is_array($extra['class']) ? $extra['class'] : array($extra['class']);
|
||||||
|
|
||||||
$generator = new AutoloadGenerator;
|
$generator = new AutoloadGenerator;
|
||||||
$map = $generator->parseAutoloads(array(array($package, $downloadPath)));
|
$map = $generator->parseAutoloads(array(array($package, $downloadPath)));
|
||||||
$classLoader = $generator->createLoader($map);
|
$classLoader = $generator->createLoader($map);
|
||||||
$classLoader->register();
|
$classLoader->register();
|
||||||
|
|
||||||
|
foreach ($classes as $class) {
|
||||||
if (class_exists($class, false)) {
|
if (class_exists($class, false)) {
|
||||||
$code = file_get_contents($classLoader->findFile($class));
|
$code = file_get_contents($classLoader->findFile($class));
|
||||||
$code = preg_replace('{^class\s+(\S+)}mi', 'class $1_composer_tmp'.self::$classCounter, $code);
|
$code = preg_replace('{^class\s+(\S+)}mi', 'class $1_composer_tmp'.self::$classCounter, $code);
|
||||||
|
@ -98,8 +99,8 @@ class InstallerInstaller extends LibraryInstaller
|
||||||
self::$classCounter++;
|
self::$classCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$extra = $package->getExtra();
|
|
||||||
$installer = new $class($this->vendorDir, $this->binDir, $this->downloadManager, $this->io);
|
$installer = new $class($this->vendorDir, $this->binDir, $this->downloadManager, $this->io);
|
||||||
$this->installationManager->addInstaller($installer);
|
$this->installationManager->addInstaller($installer);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Installer;
|
||||||
|
|
||||||
|
use Composer\Installer\InstallerInterface;
|
||||||
|
use Composer\Package\PackageInterface;
|
||||||
|
use Composer\Repository\InstalledRepositoryInterface;
|
||||||
|
|
||||||
|
class Custom1 implements InstallerInterface
|
||||||
|
{
|
||||||
|
public $name = 'custom1';
|
||||||
|
public $version = 'installer-v4';
|
||||||
|
|
||||||
|
public function supports($packageType) {}
|
||||||
|
public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package) {}
|
||||||
|
public function install(InstalledRepositoryInterface $repo, PackageInterface $package) {}
|
||||||
|
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) {}
|
||||||
|
public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) {}
|
||||||
|
public function getInstallPath(PackageInterface $package) {}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Installer;
|
||||||
|
|
||||||
|
use Composer\Installer\InstallerInterface;
|
||||||
|
use Composer\Package\PackageInterface;
|
||||||
|
use Composer\Repository\InstalledRepositoryInterface;
|
||||||
|
|
||||||
|
class Custom2 implements InstallerInterface
|
||||||
|
{
|
||||||
|
public $name = 'custom2';
|
||||||
|
public $version = 'installer-v4';
|
||||||
|
|
||||||
|
public function supports($packageType) {}
|
||||||
|
public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package) {}
|
||||||
|
public function install(InstalledRepositoryInterface $repo, PackageInterface $package) {}
|
||||||
|
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) {}
|
||||||
|
public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) {}
|
||||||
|
public function getInstallPath(PackageInterface $package) {}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"name": "",
|
||||||
|
"version": "4.0.0",
|
||||||
|
"type": "composer-installer",
|
||||||
|
"autoload": { "psr-0": { "Installer": "" } },
|
||||||
|
"extra": {
|
||||||
|
"class": [
|
||||||
|
"Installer\\Custom1",
|
||||||
|
"Installer\\Custom2"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,7 +22,7 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$loader = new JsonLoader();
|
$loader = new JsonLoader();
|
||||||
$this->packages = array();
|
$this->packages = array();
|
||||||
for ($i = 1; $i <= 3; $i++) {
|
for ($i = 1; $i <= 4; $i++) {
|
||||||
$this->packages[] = $loader->load(__DIR__.'/Fixtures/installer-v'.$i.'/composer.json');
|
$this->packages[] = $loader->load(__DIR__.'/Fixtures/installer-v'.$i.'/composer.json');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,6 +58,43 @@ class InstallerInstallerTest extends \PHPUnit_Framework_TestCase
|
||||||
$installer->install($this->repository, $this->packages[0]);
|
$installer->install($this->repository, $this->packages[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testInstallMultipleInstallers()
|
||||||
|
{
|
||||||
|
$this->repository
|
||||||
|
->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)
|
||||||
|
);
|
||||||
|
|
||||||
|
$test = $this;
|
||||||
|
|
||||||
|
$this->im
|
||||||
|
->expects($this->at(0))
|
||||||
|
->method('addInstaller')
|
||||||
|
->will($this->returnCallback(function ($installer) use ($test) {
|
||||||
|
$test->assertEquals('custom1', $installer->name);
|
||||||
|
$test->assertEquals('installer-v4', $installer->version);
|
||||||
|
}));
|
||||||
|
|
||||||
|
$this->im
|
||||||
|
->expects($this->at(1))
|
||||||
|
->method('addInstaller')
|
||||||
|
->will($this->returnCallback(function ($installer) use ($test) {
|
||||||
|
$test->assertEquals('custom2', $installer->name);
|
||||||
|
$test->assertEquals('installer-v4', $installer->version);
|
||||||
|
}));
|
||||||
|
|
||||||
|
$installer->install($this->repository, $this->packages[3]);
|
||||||
|
}
|
||||||
|
|
||||||
public function testUpgradeWithNewClassName()
|
public function testUpgradeWithNewClassName()
|
||||||
{
|
{
|
||||||
$this->repository
|
$this->repository
|
||||||
|
|
Loading…
Reference in New Issue