diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 2ccaec890..e717404eb 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -27,6 +27,7 @@ use Composer\IO\IOInterface; use Composer\IO\ConsoleIO; use Composer\Json\JsonValidationException; use Composer\Util\ErrorHandler; +use Composer\Plugin\CommandsProviderInterface; /** * The console application that handles the commands @@ -144,6 +145,10 @@ class Application extends BaseApplication $this->io->enableDebugging($startTime); } + if (!$input->hasParameterOption('--no-plugins')) { + $this->addCommands($this->getPluginCommands()); + } + $result = parent::doRun($input, $output); if (isset($oldWorkingDir)) { @@ -317,6 +322,7 @@ class Application extends BaseApplication { $definition = parent::getDefaultInputDefinition(); $definition->addOption(new InputOption('--profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information')); + $definition->addOption(new InputOption('--no-plugins', null, InputOption::VALUE_NONE, 'Whether to disable plugins.')); $definition->addOption(new InputOption('--working-dir', '-d', InputOption::VALUE_REQUIRED, 'If specified, use the given directory as working directory.')); return $definition; @@ -332,4 +338,24 @@ class Application extends BaseApplication return $helperSet; } + + private function getPluginCommands() + { + $commands = array(); + + $composer = $this->getComposer(false, false); + if (null === $composer) { + $composer = Factory::createGlobal($this->io, false); + } + + if (null !== $composer) { + foreach ($composer->getPluginManager()->getPlugins() as $plugin) { + if ($plugin instanceof CommandsProviderInterface) { + $commands = array_merge($commands, $plugin->getCommands()); + } + } + } + + return $commands; + } } diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index da0a9ef63..37571682f 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -289,7 +289,11 @@ class Factory $this->createDefaultInstallers($im, $composer, $io); if ($fullLoad) { - $globalComposer = $this->createGlobalComposer($io, $config, $disablePlugins); + $globalComposer = null; + if (realpath($config->get('home')) !== $cwd) { + $globalComposer = $this->createGlobalComposer($io, $config, $disablePlugins); + } + $pm = $this->createPluginManager($io, $composer, $globalComposer); $composer->setPluginManager($pm); @@ -351,15 +355,11 @@ class Factory * @param Config $config * @return Composer|null */ - protected function createGlobalComposer(IOInterface $io, Config $config, $disablePlugins) + protected function createGlobalComposer(IOInterface $io, Config $config, $disablePlugins, $fullLoad = false) { - if (realpath($config->get('home')) === getcwd()) { - return; - } - $composer = null; try { - $composer = self::createComposer($io, $config->get('home') . '/composer.json', $disablePlugins, $config->get('home'), false); + $composer = self::createComposer($io, $config->get('home') . '/composer.json', $disablePlugins, $config->get('home'), $fullLoad); } catch (\Exception $e) { if ($io->isDebug()) { $io->writeError('Failed to initialize global composer: '.$e->getMessage()); @@ -488,4 +488,16 @@ class Factory return $factory->createComposer($io, $config, $disablePlugins); } + + /** + * @param IOInterface $io IO instance + * @param bool $disablePlugins Whether plugins should not be loaded + * @return Composer + */ + public static function createGlobal(IOInterface $io, $disablePlugins = false) + { + $factory = new static(); + + return $factory->createGlobalComposer($io, static::createConfig($io), $disablePlugins, true); + } } diff --git a/src/Composer/Plugin/CommandsProviderInterface.php b/src/Composer/Plugin/CommandsProviderInterface.php new file mode 100644 index 000000000..4879b47fb --- /dev/null +++ b/src/Composer/Plugin/CommandsProviderInterface.php @@ -0,0 +1,32 @@ + + * 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\IO\IOInterface; + +/** + * Commands Provider Interface + * + * @author Jérémy Derussé + */ +interface CommandsProviderInterface +{ + + /** + * Retreives list of commands + * + * @return array + */ + public function getCommands(); +} diff --git a/tests/Composer/Test/Plugin/Fixtures/plugin-v5/Installer/Plugin.php b/tests/Composer/Test/Plugin/Fixtures/plugin-v5/Installer/Plugin.php new file mode 100644 index 000000000..6bfd3c6cc --- /dev/null +++ b/tests/Composer/Test/Plugin/Fixtures/plugin-v5/Installer/Plugin.php @@ -0,0 +1,22 @@ +packages = array(); $this->directory = sys_get_temp_dir() . '/' . uniqid(); - for ($i = 1; $i <= 4; $i++) { + for ($i = 1; $i <= 5; $i++) { $filename = '/Fixtures/plugin-v'.$i.'/composer.json'; mkdir(dirname($this->directory . $filename), 0777, true); $this->packages[] = $loader->load(__DIR__ . $filename);