diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Application.php b/src/Symfony/Bundle/FrameworkBundle/Console/Application.php index c091443949975..e00d018905126 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Application.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Application.php @@ -15,9 +15,12 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\HttpKernel\Kernel; -use Symfony\Component\HttpKernel\Bundle\Bundle; +use Symfony\Bundle\FrameworkBundle\Console\ConsoleEvents; +use Symfony\Bundle\FrameworkBundle\Console\Event\ConsoleEvent; +use Symfony\Bundle\FrameworkBundle\Console\Event\ConsoleTerminateEvent; /** * Application. @@ -75,7 +78,17 @@ public function doRun(InputInterface $input, OutputInterface $output) return 0; } - return parent::doRun($input, $output); + $dispatcher = $this->kernel->getContainer()->get('event_dispatcher'); + + $initEvent = new ConsoleEvent($input, $output); + $dispatcher->dispatch(ConsoleEvents::INIT, $initEvent); + + $exitCode = parent::doRun($input, $output); + + $terminateEvent = new ConsoleTerminateEvent($input, $output, $exitCode); + $dispatcher->dispatch(ConsoleEvents::TERMINATE, $terminateEvent); + + return $exitCode; } protected function registerCommands() diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/ConsoleEvents.php b/src/Symfony/Bundle/FrameworkBundle/Console/ConsoleEvents.php new file mode 100644 index 0000000000000..9751b97109f65 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Console/ConsoleEvents.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Console; + +/** + * Contains all events thrown during Console commands execution + * + * @author Francesco Levorato + * + */ +final class ConsoleEvents +{ + /** + * The INIT event allows you to attach listeners before any command is + * executed by the console. It also allows you to modify the input and output + * before they are handled to the command. + * + * The event listener method receives a \Symfony\Bundle\FrameworkBundle\Console\Event\ConsoleEvent + * instance. + * + * @var string + */ + const INIT = 'console.init'; + + /** + * The TERMINATE event allows you to attach listeners after a command is + * executed by the console. + * + * The event listener method receives a \Symfony\Bundle\FrameworkBundle\Console\Event\ConsoleTerminateEvent + * instance. + * + * @var string + */ + const TERMINATE = 'console.terminate'; + +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Event/ConsoleEvent.php b/src/Symfony/Bundle/FrameworkBundle/Console/Event/ConsoleEvent.php new file mode 100644 index 0000000000000..d9dd723180332 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Event/ConsoleEvent.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Console\Event; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\EventDispatcher\Event; + +/** + * Allows to inspect input and output of a command. + * + * @author Francesco Levorato + */ +class ConsoleEvent extends Event +{ + + private $input; + + private $output; + + public function __construct(InputInterface $input, OutputInterface $output) + { + $this->input = $input; + $this->output = $output; + } + + /** + * Returns the input object + * + * @return InputInterface + */ + public function getInput() + { + return $this->input; + } + + /** + * Returns the output object + * + * @return OutputInterface + */ + public function getOutput() + { + return $this->output; + } + +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Event/ConsoleTerminateEvent.php b/src/Symfony/Bundle/FrameworkBundle/Console/Event/ConsoleTerminateEvent.php new file mode 100644 index 0000000000000..9ebfbb1cf0b47 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Event/ConsoleTerminateEvent.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Console\Event; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * Allows to receive the exit code of a command after its execution. + * + * @author Francesco Levorato + */ +class ConsoleTerminateEvent extends ConsoleEvent +{ + + /** + * The exit code of the command. + * + * @var integer + */ + private $exitCode; + + public function __construct(InputInterface $input, OutputInterface $output, $exitCode) + { + parent::__construct($input, $output); + $this->exitCode = $exitCode; + } + + /** + * Returns the exit code. + * + * @return integer + */ + public function getExitCode() + { + return $this->exitCode; + } + +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php index 1427453b10650..478723ad8d4eb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php @@ -47,6 +47,19 @@ private function getKernel(array $bundles) ->method('getBundles') ->will($this->returnValue($bundles)) ; + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + + $container + ->expects($this->any()) + ->method('get') + ->with($this->equalTo('event_dispatcher')) + ->will($this->returnValue($eventDispatcher)); + $kernel + ->expects($this->any()) + ->method('getContainer') + ->will($this->returnValue($container)) + ; return $kernel; }