Using Events

Using Events

The Application class of the Console component allows you to optionally hook into the lifecycle of a console application via events. Instead of reinventing the wheel, it uses the Symfony EventDispatcher component to do the work:

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();

Caution

Console events are only triggered by the main command being executed. Commands called by the main command will not trigger any event.

The ConsoleEvents::COMMAND Event

Typical Purposes: Doing something before any command is run (like logging which command is going to be executed), or displaying something about the event to be executed.

Just before executing any command, the ConsoleEvents::COMMAND event is dispatched. Listeners receive a ConsoleCommandEvent event:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\ConsoleEvents;

$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
    // get the input instance
    $input = $event->getInput();

    // get the output instance
    $output = $event->getOutput();

    // get the command to be executed
    $command = $event->getCommand();

    // write something about the command
    $output->writeln(sprintf('Before running command <info>%s</info>', $command->getName()));

    // get the application
    $application = $command->getApplication();
});

Disable Commands inside Listeners

Using the disableCommand() method, you can disable a command inside a listener. The application will then not execute the command, but instead will return the code 113 (defined in ConsoleCommandEvent::RETURN_CODE_DISABLED). This code is one of the reserved exit codes for console commands that conform with the C/C++ standard.:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\ConsoleEvents;

$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
    // get the command to be executed
    $command = $event->getCommand();

    // ... check if the command can be executed

    // disable the command, this will result in the command being skipped
    // and code 113 being returned from the Application
    $event->disableCommand();

    // it is possible to enable the command in a later listener
    if (!$event->commandShouldRun()) {
        $event->enableCommand();
    }
});

The ConsoleEvents::EXCEPTION Event

New in version 3.3: The ConsoleEvents::EXCEPTION event was deprecated in Symfony 3.3. Use the ConsoleEvents::ERROR event instead.

Typical Purposes: Handle exceptions thrown during the execution of a command.

Whenever an exception is thrown by a command, the ConsoleEvents::EXCEPTION event is dispatched. A listener can wrap or change the exception or do anything useful before the exception is thrown by the application.

Listeners receive a ConsoleExceptionEvent event:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\ConsoleEvents;

$dispatcher->addListener(ConsoleEvents::EXCEPTION, function (ConsoleExceptionEvent $event) {
    $output = $event->getOutput();

    $command = $event->getCommand();

    $output->writeln(sprintf('Oops, exception thrown while running command <info>%s</info>', $command->getName()));

    // get the current exit code (the exception code or the exit code set by a ConsoleEvents::TERMINATE event)
    $exitCode = $event->getExitCode();

    // change the exception to another one
    $event->setException(new \LogicException('Caught exception', $exitCode, $event->getException()));
});

The ConsoleEvents::ERROR Event

New in version 3.3: The ConsoleEvents::ERROR event was introduced in Symfony 3.3.

Typical Purposes: Handle exceptions thrown during the execution of a command.

This event is an improved version of the ConsoleEvents::EXCEPTION event, because it can handle every exception thrown during the execution of a command, including those triggered from event listeners.

The ConsoleEvents::TERMINATE Event

Typical Purposes: To perform some cleanup actions after the command has been executed.

After the command has been executed, the ConsoleEvents::TERMINATE event is dispatched. It can be used to do any actions that need to be executed for all commands or to cleanup what you initiated in a ConsoleEvents::COMMAND listener (like sending logs, closing a database connection, sending emails, ...). A listener might also change the exit code.

Listeners receive a ConsoleTerminateEvent event:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\ConsoleEvents;

$dispatcher->addListener(ConsoleEvents::TERMINATE, function (ConsoleTerminateEvent $event) {
    // get the output
    $output = $event->getOutput();

    // get the command that has been executed
    $command = $event->getCommand();

    // display something
    $output->writeln(sprintf('After running command <info>%s</info>', $command->getName()));

    // change the exit code
    $event->setExitCode(128);
});

Tip

This event is also dispatched when an exception is thrown by the command. It is then dispatched just after the ConsoleEvents::EXCEPTION event. The exit code received in this case is the exception code.


This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.