1
0
Fork 0

Add PRE_COMMAND_RUN event, fixes #7002

pull/7040/head
Jordi Boggiano 2018-01-24 15:21:46 +01:00
parent 5f97eb1578
commit b1bfb9bb65
4 changed files with 75 additions and 0 deletions

View File

@ -63,6 +63,9 @@ Composer fires the following named events during its execution process:
- **pre-file-download**: occurs before files are downloaded and allows - **pre-file-download**: occurs before files are downloaded and allows
you to manipulate the `RemoteFilesystem` object prior to downloading files you to manipulate the `RemoteFilesystem` object prior to downloading files
based on the URL to be downloaded. based on the URL to be downloaded.
- **pre-command-run**: occurs before a command is executed and allows you to
manipulate the `InputInterface` object's options and arguments to tweak
a command's behavior.
> **Note:** Composer makes no assumptions about the state of your dependencies > **Note:** Composer makes no assumptions about the state of your dependencies
> prior to `install` or `update`. Therefore, you should not specify scripts > prior to `install` or `update`. Therefore, you should not specify scripts

View File

@ -15,8 +15,11 @@ namespace Composer\Command;
use Composer\Composer; use Composer\Composer;
use Composer\Config; use Composer\Config;
use Composer\Console\Application; use Composer\Console\Application;
use Composer\Factory;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\IO\NullIO; use Composer\IO\NullIO;
use Composer\Plugin\PreCommandRunEvent;
use Composer\Plugin\PluginEvents;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@ -123,6 +126,14 @@ abstract class BaseCommand extends Command
*/ */
protected function initialize(InputInterface $input, OutputInterface $output) protected function initialize(InputInterface $input, OutputInterface $output)
{ {
// initialize a plugin-enabled Composer instance, either local or global
$composer = $this->getComposer(false, false);
if (null === $composer) {
$composer = Factory::createGlobal($this->getIO(), false);
}
$preCommandRunEvent = new PreCommandRunEvent(PluginEvents::PRE_COMMAND_RUN, $input);
$composer->getEventDispatcher()->dispatch($preCommandRunEvent->getName(), $preCommandRunEvent);
if (true === $input->hasParameterOption(array('--no-ansi')) && $input->hasOption('no-progress')) { if (true === $input->hasParameterOption(array('--no-ansi')) && $input->hasOption('no-progress')) {
$input->setOption('no-progress', true); $input->setOption('no-progress', true);
} }

View File

@ -48,4 +48,14 @@ class PluginEvents
* @var string * @var string
*/ */
const PRE_FILE_DOWNLOAD = 'pre-file-download'; const PRE_FILE_DOWNLOAD = 'pre-file-download';
/**
* The PRE_COMMAND_RUN event occurs before a command is executed and lets you modify the input arguments/options
*
* The event listener method receives a
* Composer\Plugin\PreCommandRunEvent instance.
*
* @var string
*/
const PRE_COMMAND_RUN = 'pre-command-run';
} }

View File

@ -0,0 +1,51 @@
<?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\EventDispatcher\Event;
use Symfony\Component\Console\Input\InputInterface;
/**
* The pre command run event.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class PreCommandRunEvent extends Event
{
/**
* @var InputInterface
*/
private $input;
/**
* Constructor.
*
* @param string $name The event name
* @param InputInterface $input
*/
public function __construct($name, InputInterface $input)
{
parent::__construct($name);
$this->input = $input;
}
/**
* Returns the console input
*
* @return InputInterface
*/
public function getInput()
{
return $this->input;
}
}