Browse Source

Add a Command event triggered by all comands which load plugins

Nils Adermann 11 years ago
parent
commit
d00ca4bcdb

+ 8 - 1
src/Composer/Command/DependsCommand.php

@@ -13,6 +13,8 @@
 namespace Composer\Command;
 
 use Composer\DependencyResolver\Pool;
+use Composer\Plugin\CommandEvent;
+use Composer\Plugin\PluginEvents;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputOption;
@@ -50,7 +52,12 @@ EOT
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $repo = $this->getComposer()->getRepositoryManager()->getLocalRepository();
+        $composer = $this->getComposer();
+
+        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'depends', $input, $output);
+        $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
+
+        $repo = $composer->getRepositoryManager()->getLocalRepository();
         $needle = $input->getArgument('package');
 
         $pool = new Pool();

+ 5 - 0
src/Composer/Command/DiagnoseCommand.php

@@ -15,6 +15,8 @@ namespace Composer\Command;
 use Composer\Composer;
 use Composer\Factory;
 use Composer\Downloader\TransportException;
+use Composer\Plugin\CommandEvent;
+use Composer\Plugin\PluginEvents;
 use Composer\Util\ConfigValidator;
 use Composer\Util\RemoteFilesystem;
 use Composer\Util\StreamContextFactory;
@@ -64,6 +66,9 @@ EOT
 
         $composer = $this->getComposer(false);
         if ($composer) {
+            $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'diagnose', $input, $output);
+            $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
+
             $output->write('Checking composer.json: ');
             $this->outputResult($output, $this->checkComposerSchema());
         }

+ 6 - 0
src/Composer/Command/DumpAutoloadCommand.php

@@ -12,6 +12,8 @@
 
 namespace Composer\Command;
 
+use Composer\Plugin\CommandEvent;
+use Composer\Plugin\PluginEvents;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -42,6 +44,10 @@ EOT
         $output->writeln('<info>Generating autoload files</info>');
 
         $composer = $this->getComposer();
+
+        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'dump-autoload', $input, $output);
+        $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
+
         $installationManager = $composer->getInstallationManager();
         $localRepo = $composer->getRepositoryManager()->getLocalRepository();
         $package = $composer->getPackage();

+ 6 - 0
src/Composer/Command/InstallCommand.php

@@ -13,6 +13,8 @@
 namespace Composer\Command;
 
 use Composer\Installer;
+use Composer\Plugin\CommandEvent;
+use Composer\Plugin\PluginEvents;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -66,6 +68,10 @@ EOT
         $composer = $this->getComposer(true, $input->getOption('no-plugins'));
         $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
         $io = $this->getIO();
+
+        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'install', $input, $output);
+        $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
+
         $install = Installer::create($io, $composer);
 
         $preferSource = false;

+ 6 - 0
src/Composer/Command/LicensesCommand.php

@@ -15,6 +15,8 @@ namespace Composer\Command;
 use Composer\Package\PackageInterface;
 use Composer\Json\JsonFile;
 use Composer\Package\Version\VersionParser;
+use Composer\Plugin\CommandEvent;
+use Composer\Plugin\PluginEvents;
 use Symfony\Component\Console\Helper\TableHelper;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputArgument;
@@ -46,6 +48,10 @@ EOT
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $composer = $this->getComposer();
+
+        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'licenses', $input, $output);
+        $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
+
         $root = $composer->getPackage();
         $repo = $composer->getRepositoryManager()->getLocalRepository();
 

+ 6 - 0
src/Composer/Command/RequireCommand.php

@@ -21,6 +21,8 @@ use Composer\Installer;
 use Composer\Json\JsonFile;
 use Composer\Json\JsonManipulator;
 use Composer\Package\Version\VersionParser;
+use Composer\Plugin\CommandEvent;
+use Composer\Plugin\PluginEvents;
 
 /**
  * @author Jérémy Romey <jeremy@free-agent.fr>
@@ -106,6 +108,10 @@ EOT
         $composer = $this->getComposer();
         $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
         $io = $this->getIO();
+
+        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'require', $input, $output);
+        $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
+
         $install = Installer::create($io, $composer);
 
         $install

+ 7 - 0
src/Composer/Command/SearchCommand.php

@@ -20,6 +20,8 @@ use Composer\Repository\CompositeRepository;
 use Composer\Repository\PlatformRepository;
 use Composer\Repository\RepositoryInterface;
 use Composer\Factory;
+use Composer\Plugin\CommandEvent;
+use Composer\Plugin\PluginEvents;
 
 /**
  * @author Robert Schönthal <seroscho@googlemail.com>
@@ -65,6 +67,11 @@ EOT
             $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
         }
 
+        if ($composer) {
+            $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'search', $input, $output);
+            $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
+        }
+
         $onlyName = $input->getOption('only-name');
 
         $flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT;

+ 7 - 0
src/Composer/Command/ShowCommand.php

@@ -18,6 +18,8 @@ use Composer\DependencyResolver\DefaultPolicy;
 use Composer\Factory;
 use Composer\Package\CompletePackageInterface;
 use Composer\Package\Version\VersionParser;
+use Composer\Plugin\CommandEvent;
+use Composer\Plugin\PluginEvents;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputOption;
@@ -94,6 +96,11 @@ EOT
             $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
         }
 
+        if ($composer) {
+            $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'show', $input, $output);
+            $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
+        }
+
         // show single package or single version
         if ($input->getArgument('package') || !empty($package)) {
             $versions = array();

+ 6 - 0
src/Composer/Command/StatusCommand.php

@@ -17,6 +17,8 @@ use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 use Composer\Downloader\ChangeReportInterface;
 use Composer\Downloader\VcsDownloader;
+use Composer\Plugin\CommandEvent;
+use Composer\Plugin\PluginEvents;
 use Composer\Script\ScriptEvents;
 
 /**
@@ -46,6 +48,10 @@ EOT
     {
         // init repos
         $composer = $this->getComposer();
+
+        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'status', $input, $output);
+        $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
+
         $installedRepo = $composer->getRepositoryManager()->getLocalRepository();
 
         $dm = $composer->getDownloadManager();

+ 6 - 0
src/Composer/Command/UpdateCommand.php

@@ -13,6 +13,8 @@
 namespace Composer\Command;
 
 use Composer\Installer;
+use Composer\Plugin\CommandEvent;
+use Composer\Plugin\PluginEvents;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Input\InputArgument;
@@ -70,6 +72,10 @@ EOT
         $composer = $this->getComposer(true, $input->getOption('no-plugins'));
         $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
         $io = $this->getIO();
+
+        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', $input, $output);
+        $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
+
         $install = Installer::create($io, $composer);
 
         $preferSource = false;

+ 87 - 0
src/Composer/Plugin/CommandEvent.php

@@ -0,0 +1,87 @@
+<?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\IO\IOInterface;
+use Composer\EventDispatcher\Event;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+/**
+ * An event for all commands.
+ *
+ * @author Nils Adermann <naderman@naderman.de>
+ */
+class CommandEvent extends Event
+{
+    /**
+     * @var string
+     */
+    private $commandName;
+
+    /**
+     * @var InputInterface
+     */
+    private $input;
+
+    /**
+     * @var OutputInterface
+     */
+    private $output;
+
+    /**
+     * Constructor.
+     *
+     * @param string          $name        The event name
+     * @param string          $commandName The command name
+     * @param InputInterface  $input
+     * @param OutputInterface $output
+     */
+    public function __construct($name, $commandName, $input, $output)
+    {
+        parent::__construct($name);
+        $this->commandName = $commandName;
+        $this->input = $input;
+        $this->output = $output;
+    }
+
+    /**
+     * Returns the command input interface
+     *
+     * @return InputInterface
+     */
+    public function getInput()
+    {
+        return $this->input;
+    }
+
+    /**
+     * Retrieves the command output interface
+     *
+     * @return OutputInterface
+     */
+    public function getOutput()
+    {
+        return $this->output;
+    }
+
+    /**
+     * Retrieves the name of the command being run
+     *
+     * @return string
+     */
+    public function getCommandName()
+    {
+        return $this->commandName;
+    }
+}

+ 10 - 0
src/Composer/Plugin/PluginEvents.php

@@ -19,6 +19,16 @@ namespace Composer\Plugin;
  */
 class PluginEvents
 {
+    /**
+     * The COMMAND event occurs as a command begins
+     *
+     * The event listener method receives a
+     * Composer\Plugin\CommandEvent instance.
+     *
+     * @var string
+     */
+    const COMMAND = 'command';
+
     /**
      * The PRE_FILE_DOWNLOAD event occurs before downloading a file
      *