Browse Source

Implement a plugin manager and interface, update installer plugin tests

Nils Adermann 12 năm trước cách đây
mục cha
commit
eb966d347f
23 tập tin đã thay đổi với 238 bổ sung156 xóa
  1. 22 0
      src/Composer/Composer.php
  2. 12 0
      src/Composer/Factory.php
  3. 15 11
      src/Composer/Installer/PluginInstaller.php
  4. 30 0
      src/Composer/Plugin/PluginInterface.php
  5. 49 0
      src/Composer/Plugin/PluginManager.php
  6. 0 19
      tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Custom.php
  7. 0 7
      tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Exception.php
  8. 15 0
      tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Plugin.php
  9. 1 1
      tests/Composer/Test/Installer/Fixtures/installer-v1/composer.json
  10. 0 19
      tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Custom2.php
  11. 0 7
      tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Exception.php
  12. 15 0
      tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Plugin2.php
  13. 1 1
      tests/Composer/Test/Installer/Fixtures/installer-v2/composer.json
  14. 0 19
      tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Custom2.php
  15. 0 7
      tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Exception.php
  16. 15 0
      tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Plugin2.php
  17. 1 1
      tests/Composer/Test/Installer/Fixtures/installer-v3/composer.json
  18. 0 20
      tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Custom1.php
  19. 0 20
      tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Custom2.php
  20. 16 0
      tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Plugin1.php
  21. 16 0
      tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Plugin2.php
  22. 2 2
      tests/Composer/Test/Installer/Fixtures/installer-v4/composer.json
  23. 28 22
      tests/Composer/Test/Installer/PluginInstallerTest.php

+ 22 - 0
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
      */

+ 12 - 0
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
      */

+ 15 - 11
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);
+            }
         }
     }
 }

+ 30 - 0
src/Composer/Plugin/PluginInterface.php

@@ -0,0 +1,30 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * 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 <naderman@naderman.de>
+ */
+interface PluginInterface
+{
+    /**
+     * Apply plugin modifications to the passed in composer object
+     *
+     * @param Composer $composer
+     */
+    public function activate(Composer $composer);
+}

+ 49 - 0
src/Composer/Plugin/PluginManager.php

@@ -0,0 +1,49 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * 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 <naderman@naderman.de>
+ */
+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);
+    }
+}

+ 0 - 19
tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Custom.php

@@ -1,19 +0,0 @@
-<?php
-
-namespace Installer;
-
-use Composer\Installer\InstallerInterface;
-use Composer\Package\PackageInterface;
-use Composer\Repository\InstalledRepositoryInterface;
-
-class Custom implements InstallerInterface
-{
-    public $version = 'installer-v1';
-
-    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 - 7
tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Exception.php

@@ -1,7 +0,0 @@
-<?php
-
-namespace Installer;
-
-class Exception extends \Exception
-{
-}

+ 15 - 0
tests/Composer/Test/Installer/Fixtures/installer-v1/Installer/Plugin.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace Installer;
+
+use Composer\Composer;
+use Composer\Plugin\PluginInterface;
+
+class Plugin implements PluginInterface
+{
+    public $version = 'installer-v1';
+
+    public function activate(Composer $composer)
+    {
+    }
+}

+ 1 - 1
tests/Composer/Test/Installer/Fixtures/installer-v1/composer.json

@@ -4,6 +4,6 @@
     "type": "composer-plugin",
     "autoload": { "psr-0": { "Installer": "" } },
     "extra": {
-        "class": "Installer\\Custom"
+        "class": "Installer\\Plugin"
     }
 }

+ 0 - 19
tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Custom2.php

@@ -1,19 +0,0 @@
-<?php
-
-namespace Installer;
-
-use Composer\Installer\InstallerInterface;
-use Composer\Package\PackageInterface;
-use Composer\Repository\InstalledRepositoryInterface;
-
-class Custom2 implements InstallerInterface
-{
-    public $version = 'installer-v2';
-
-    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 - 7
tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Exception.php

@@ -1,7 +0,0 @@
-<?php
-
-namespace Installer;
-
-class Exception extends \Exception
-{
-}

+ 15 - 0
tests/Composer/Test/Installer/Fixtures/installer-v2/Installer/Plugin2.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace Installer;
+
+use Composer\Composer;
+use Composer\Plugin\PluginInterface;
+
+class Plugin2 implements PluginInterface
+{
+    public $version = 'installer-v2';
+
+    public function activate(Composer $composer)
+    {
+    }
+}

+ 1 - 1
tests/Composer/Test/Installer/Fixtures/installer-v2/composer.json

@@ -4,6 +4,6 @@
     "type": "composer-plugin",
     "autoload": { "psr-0": { "Installer": "" } },
     "extra": {
-        "class": "Installer\\Custom2"
+        "class": "Installer\\Plugin2"
     }
 }

+ 0 - 19
tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Custom2.php

@@ -1,19 +0,0 @@
-<?php
-
-namespace Installer;
-
-use Composer\Installer\InstallerInterface;
-use Composer\Package\PackageInterface;
-use Composer\Repository\InstalledRepositoryInterface;
-
-class Custom2 implements InstallerInterface
-{
-    public $version = 'installer-v3';
-
-    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 - 7
tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Exception.php

@@ -1,7 +0,0 @@
-<?php
-
-namespace Installer;
-
-class Exception extends \Exception
-{
-}

+ 15 - 0
tests/Composer/Test/Installer/Fixtures/installer-v3/Installer/Plugin2.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace Installer;
+
+use Composer\Composer;
+use Composer\Plugin\PluginInterface;
+
+class Plugin2 implements PluginInterface
+{
+    public $version = 'installer-v3';
+
+    public function activate(Composer $composer)
+    {
+    }
+}

+ 1 - 1
tests/Composer/Test/Installer/Fixtures/installer-v3/composer.json

@@ -4,6 +4,6 @@
     "type": "composer-plugin",
     "autoload": { "psr-0": { "Installer": "" } },
     "extra": {
-        "class": "Installer\\Custom2"
+        "class": "Installer\\Plugin2"
     }
 }

+ 0 - 20
tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Custom1.php

@@ -1,20 +0,0 @@
-<?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 - 20
tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Custom2.php

@@ -1,20 +0,0 @@
-<?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) {}
-}

+ 16 - 0
tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Plugin1.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace Installer;
+
+use Composer\Composer;
+use Composer\Plugin\PluginInterface;
+
+class Plugin1 implements PluginInterface
+{
+    public $name = 'plugin1';
+    public $version = 'installer-v4';
+
+    public function activate(Composer $composer)
+    {
+    }
+}

+ 16 - 0
tests/Composer/Test/Installer/Fixtures/installer-v4/Installer/Plugin2.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace Installer;
+
+use Composer\Composer;
+use Composer\Plugin\PluginInterface;
+
+class Plugin2 implements PluginInterface
+{
+    public $name = 'plugin2';
+    public $version = 'installer-v4';
+
+    public function activate(Composer $composer)
+    {
+    }
+}

+ 2 - 2
tests/Composer/Test/Installer/Fixtures/installer-v4/composer.json

@@ -5,8 +5,8 @@
     "autoload": { "psr-0": { "Installer": "" } },
     "extra": {
         "class": [
-            "Installer\\Custom1",
-            "Installer\\Custom2"
+            "Installer\\Plugin1",
+            "Installer\\Plugin2"
         ]
     }
 }

+ 28 - 22
tests/Composer/Test/Installer/PluginInstallerTest.php

@@ -25,6 +25,7 @@ class PluginInstallerTest extends \PHPUnit_Framework_TestCase
     protected $composer;
     protected $packages;
     protected $im;
+    protected $pm;
     protected $repository;
     protected $io;
     protected $autoloadGenerator;
@@ -45,6 +46,10 @@ class PluginInstallerTest extends \PHPUnit_Framework_TestCase
             ->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]);