@@ -24,8 +24,8 @@ The ``ConsoleEvents::COMMAND`` Event
24
24
------------------------------------
25
25
26
26
**Typical Purposes **: Doing something before any command is run (like logging
27
- which command is going to be executed), or displaying something about the event
28
- to be executed.
27
+ which command is going to be executed), or displaying something about the event to be
28
+ executed.
29
29
30
30
Just before executing any command, the ``ConsoleEvents::COMMAND `` event is
31
31
dispatched. Listeners receive a
@@ -51,6 +51,29 @@ dispatched. Listeners receive a
51
51
$application = $command->getApplication();
52
52
});
53
53
54
+ .. versionadded :: 2.6
55
+ From Symfony 2.6 you can disable commands inside the listeners. The application
56
+ will the return the code `113 ` (defined in ``ConsoleCommandEvent::RETURN_CODE_DISABLED ``)::
57
+
58
+ use Symfony\C omponent\C onsole\E vent\C onsoleCommandEvent;
59
+ use Symfony\C omponent\C onsole\C onsoleEvents;
60
+
61
+ $dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
62
+ // get the command to be executed
63
+ $command = $event->getCommand();
64
+
65
+ // ...check if the command can be executed
66
+
67
+ // disable the command, this will result in the command being skipped
68
+ // and code 113 being returned from the Application
69
+ $event->disableCommand();
70
+
71
+ // it is possible to enable the command in a later listener
72
+ if (!$event->commandShouldRun()) {
73
+ $event->enableCommand();
74
+ }
75
+ });
76
+
54
77
The ``ConsoleEvents::TERMINATE `` Event
55
78
--------------------------------------
56
79
@@ -118,3 +141,4 @@ Listeners receive a
118
141
// change the exception to another one
119
142
$event->setException(new \LogicException('Caught exception', $exitCode, $event->getException()));
120
143
});
144
+
0 commit comments