New in Symfony 2.3: Events in the Console Component
Today, I'm about to write about one of the oldest feature requests for the Console component: the possibility to hook during the lifecycle of a command execution. I have been delaying this feature for a long time as I did not want to introduce a hard dependency between the Console component and the Event Dispatcher component.
But as of Symfony 2.3, you can listen to several events that are dispatched by the main console application:
1 2 3 4 5 6 7 8
use Symfony\Component\Console\Application; use Symfony\Component\EventDispatcher\EventDispatcher; $dispatcher = new EventDispatcher(); $application = new Application(); $application->setDispatcher($dispatcher); $application->run();
Note that events are only dispatched if you inject an event dispatcher.
Three events are automatically dispatched:
ConsoleEvents::COMMANDlets you do something before a command is executed;
ConsoleEvents::TERMINATElets you perform some cleanup actions after the command has been executed;
ConsoleEvents::EXCEPTIONlets you handle exceptions thrown during the execution of a command.
If you want to see some code example, read the full documentation about this new feature in the Console component documentation.
New in Symfony 2.3: Events in the Console Component symfony.com/index.php/blog/new-in-symfony-2-3-events-in-the-console-componentTweet this
Become a certified developer! Exams are online and available in all countries.Register Now
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.