From eb966d347fae748a2571927d40a3273af7a6ac5b Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Tue, 13 Aug 2013 15:55:52 +0200 Subject: [PATCH] Implement a plugin manager and interface, update installer plugin tests --- src/Composer/Composer.php | 22 ++++++++ src/Composer/Factory.php | 12 +++++ src/Composer/Installer/PluginInstaller.php | 26 ++++++---- src/Composer/Plugin/PluginInterface.php | 30 +++++++++++ src/Composer/Plugin/PluginManager.php | 49 ++++++++++++++++++ .../installer-v1/Installer/Custom.php | 19 ------- .../installer-v1/Installer/Exception.php | 7 --- .../installer-v1/Installer/Plugin.php | 15 ++++++ .../Fixtures/installer-v1/composer.json | 2 +- .../installer-v2/Installer/Custom2.php | 19 ------- .../installer-v2/Installer/Exception.php | 7 --- .../installer-v2/Installer/Plugin2.php | 15 ++++++ .../Fixtures/installer-v2/composer.json | 2 +- .../installer-v3/Installer/Custom2.php | 19 ------- .../installer-v3/Installer/Exception.php | 7 --- .../installer-v3/Installer/Plugin2.php | 15 ++++++ .../Fixtures/installer-v3/composer.json | 2 +- .../installer-v4/Installer/Custom1.php | 20 -------- .../installer-v4/Installer/Custom2.php | 20 -------- .../installer-v4/Installer/Plugin1.php | 16 ++++++ .../installer-v4/Installer/Plugin2.php | 16 ++++++ .../Fixtures/installer-v4/composer.json | 4 +- .../Test/Installer/PluginInstallerTest.php | 50 +++++++++++-------- 23 files changed, 238 insertions(+), 156 deletions(-) create mode 100644 src/Composer/Plugin/PluginInterface.php create mode 100644 src/Composer/Plugin/PluginManager.php delete mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Custom.php delete mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Exception.php create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Plugin.php delete mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Custom2.php delete mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Exception.php create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Plugin2.php delete mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Custom2.php delete mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Exception.php create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Plugin2.php delete mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Custom1.php delete mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Custom2.php create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Plugin1.php create mode 100644 tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Plugin2.php diff --git a/src/Composer/Composer.php b/src/Composer/Composer.php index 8c5d0785f..32d48b771 100644 --- a/src/Composer/Composer.php +++ b/src/Composer/Composer.php @@ -16,6 +16,7 @@ use Composer\Package\RootPackageInterface; use Composer\Package\Locker; use Composer\Repository\RepositoryManager; use Composer\Installer\InstallationManager; +use Composer\Plugin\PluginManager; use Composer\Downloader\DownloadManager; use Composer\Script\EventDispatcher; use Composer\Autoload\AutoloadGenerator; @@ -53,6 +54,11 @@ class Composer */ private $installationManager; + /** + * + */ + private $pluginManager; + /** * @var Config */ @@ -165,6 +171,22 @@ class Composer return $this->installationManager; } + /** + * @param Plugin\PluginManager $manager + */ + public function setPluginManager(PluginManager $manager) + { + $this->pluginManager = $manager; + } + + /** + * @return Plugin\PluginManager + */ + public function getPluginManager() + { + return $this->pluginManager; + } + /** * @param Script\EventDispatcher $eventDispatcher */ diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 749e1ad88..1dd612b13 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -264,6 +264,10 @@ class Factory $composer->setLocker($locker); } + $pm = $this->createPluginManager($composer); + + $composer->setPluginManager($pm); + return $composer; } @@ -353,6 +357,14 @@ class Factory return $am; } + /** + * @return Plugin\PluginManager + */ + protected function createPluginManager(Composer $composer) + { + return new Plugin\PluginManager($composer); + } + /** * @return Installer\InstallationManager */ diff --git a/src/Composer/Installer/PluginInstaller.php b/src/Composer/Installer/PluginInstaller.php index 9be0a9155..dfcc97669 100644 --- a/src/Composer/Installer/PluginInstaller.php +++ b/src/Composer/Installer/PluginInstaller.php @@ -29,7 +29,7 @@ class PluginInstaller extends LibraryInstaller private static $classCounter = 0; /** - * Initializes Installer installer. + * Initializes Plugin installer. * * @param IOInterface $io * @param Composer $composer @@ -42,11 +42,8 @@ class PluginInstaller extends LibraryInstaller $repo = $composer->getRepositoryManager()->getLocalRepository(); foreach ($repo->getPackages() as $package) { - if ('composer-installer' === $package->getType()) { - $this->registerInstaller($package); - } - if ('composer-plugin' === $package->getType()) { - $this->registerInstaller($package); + if ('composer-plugin' === $package->getType() || 'composer-installer' === $package->getType()) { + $this->registerPlugin($package); } } } @@ -62,7 +59,7 @@ class PluginInstaller extends LibraryInstaller } parent::install($repo, $package); - $this->registerInstaller($package); + $this->registerPlugin($package); } /** @@ -76,11 +73,13 @@ class PluginInstaller extends LibraryInstaller } parent::update($repo, $initial, $target); - $this->registerInstaller($target); + $this->registerPlugin($target); } - private function registerInstaller(PackageInterface $package) + private function registerPlugin(PackageInterface $package) { + $oldInstallerPlugin = ($package->getType() === 'composer-installer'); + $downloadPath = $this->getInstallPath($package); $extra = $package->getExtra(); @@ -100,8 +99,13 @@ class PluginInstaller extends LibraryInstaller self::$classCounter++; } - $installer = new $class($this->io, $this->composer); - $this->installationManager->addInstaller($installer); + $plugin = new $class($this->io, $this->composer); + + if ($oldInstallerPlugin) { + $this->installationManager->addInstaller($installer); + } else { + $this->composer->getPluginManager()->addPlugin($plugin); + } } } } diff --git a/src/Composer/Plugin/PluginInterface.php b/src/Composer/Plugin/PluginInterface.php new file mode 100644 index 000000000..ebbbe0026 --- /dev/null +++ b/src/Composer/Plugin/PluginInterface.php @@ -0,0 +1,30 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Plugin; + +use Composer\Composer; + +/** + * Plugin interface + * + * @author Nils Adermann + */ +interface PluginInterface +{ + /** + * Apply plugin modifications to the passed in composer object + * + * @param Composer $composer + */ + public function activate(Composer $composer); +} diff --git a/src/Composer/Plugin/PluginManager.php b/src/Composer/Plugin/PluginManager.php new file mode 100644 index 000000000..aa08de2ef --- /dev/null +++ b/src/Composer/Plugin/PluginManager.php @@ -0,0 +1,49 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Plugin; + +use Composer\Composer; +use Composer\Package\PackageInterface; + +/** + * Plugin manager + * + * @author Nils Adermann + */ +class PluginManager +{ + protected $composer; + + protected $plugins = array(); + + /** + * Initializes plugin manager + * + * @param Composer $composer + */ + public function __construct(Composer $composer) + { + $this->composer = $composer; + } + + /** + * Adds plugin + * + * @param PluginInterface $plugin plugin instance + */ + public function addPlugin(PluginInterface $plugin) + { + $this->plugins[] = $plugin; + $plugin->activate($this->composer); + } +} diff --git a/tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Custom.php b/tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Custom.php deleted file mode 100644 index bfad4a88a..000000000 --- a/tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Custom.php +++ /dev/null @@ -1,19 +0,0 @@ -disableOriginalConstructor() ->getMock(); + $this->pm = $this->getMockBuilder('Composer\Plugin\PluginManager') + ->disableOriginalConstructor() + ->getMock(); + $this->repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface'); $rm = $this->getMockBuilder('Composer\Repository\RepositoryManager') @@ -64,6 +69,7 @@ class PluginInstallerTest extends \PHPUnit_Framework_TestCase $this->composer->setConfig($config); $this->composer->setDownloadManager($dm); $this->composer->setInstallationManager($this->im); + $this->composer->setPluginManager($this->pm); $this->composer->setRepositoryManager($rm); $this->composer->setAutoloadGenerator($this->autoloadGenerator); @@ -75,7 +81,7 @@ class PluginInstallerTest extends \PHPUnit_Framework_TestCase )); } - public function testInstallNewInstaller() + public function testInstallNewPlugin() { $this->repository ->expects($this->once()) @@ -84,9 +90,9 @@ class PluginInstallerTest extends \PHPUnit_Framework_TestCase $installer = new PluginInstallerMock($this->io, $this->composer); $test = $this; - $this->im + $this->pm ->expects($this->once()) - ->method('addInstaller') + ->method('addPlugin') ->will($this->returnCallback(function ($installer) use ($test) { $test->assertEquals('installer-v1', $installer->version); })); @@ -94,7 +100,7 @@ class PluginInstallerTest extends \PHPUnit_Framework_TestCase $installer->install($this->repository, $this->packages[0]); } - public function testInstallMultipleInstallers() + public function testInstallMultiplePlugins() { $this->repository ->expects($this->once()) @@ -105,20 +111,20 @@ class PluginInstallerTest extends \PHPUnit_Framework_TestCase $test = $this; - $this->im + $this->pm ->expects($this->at(0)) - ->method('addInstaller') - ->will($this->returnCallback(function ($installer) use ($test) { - $test->assertEquals('custom1', $installer->name); - $test->assertEquals('installer-v4', $installer->version); + ->method('addPlugin') + ->will($this->returnCallback(function ($plugin) use ($test) { + $test->assertEquals('plugin1', $plugin->name); + $test->assertEquals('installer-v4', $plugin->version); })); - $this->im + $this->pm ->expects($this->at(1)) - ->method('addInstaller') - ->will($this->returnCallback(function ($installer) use ($test) { - $test->assertEquals('custom2', $installer->name); - $test->assertEquals('installer-v4', $installer->version); + ->method('addPlugin') + ->will($this->returnCallback(function ($plugin) use ($test) { + $test->assertEquals('plugin2', $plugin->name); + $test->assertEquals('installer-v4', $plugin->version); })); $installer->install($this->repository, $this->packages[3]); @@ -137,11 +143,11 @@ class PluginInstallerTest extends \PHPUnit_Framework_TestCase $installer = new PluginInstallerMock($this->io, $this->composer); $test = $this; - $this->im + $this->pm ->expects($this->once()) - ->method('addInstaller') - ->will($this->returnCallback(function ($installer) use ($test) { - $test->assertEquals('installer-v2', $installer->version); + ->method('addPlugin') + ->will($this->returnCallback(function ($plugin) use ($test) { + $test->assertEquals('installer-v2', $plugin->version); })); $installer->update($this->repository, $this->packages[0], $this->packages[1]); @@ -160,11 +166,11 @@ class PluginInstallerTest extends \PHPUnit_Framework_TestCase $installer = new PluginInstallerMock($this->io, $this->composer); $test = $this; - $this->im + $this->pm ->expects($this->once()) - ->method('addInstaller') - ->will($this->returnCallback(function ($installer) use ($test) { - $test->assertEquals('installer-v3', $installer->version); + ->method('addPlugin') + ->will($this->returnCallback(function ($plugin) use ($test) { + $test->assertEquals('installer-v3', $plugin->version); })); $installer->update($this->repository, $this->packages[1], $this->packages[2]);