Skip to content

Commit c548e76

Browse files
authored
add docs for Command.beforeExecute and Command.afterExecute (#8081)
1 parent 0dbf2cb commit c548e76

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

en/core-libraries/events.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,50 @@ Or using the ``events`` hook in your Application/Plugin class::
189189
The ``Server.terminate`` event only works for PHP-FPM implementations which
190190
support the ``fastcgi_finish_request`` function.
191191

192+
``Command.beforeExecute`` & ``Command.afterExecute``
193+
----------------------------------------------------
194+
195+
.. versionadded:: 5.0.0
196+
The ``Command.beforeExecute`` & ``Command.afterExecute`` events were added in 5.0
197+
198+
The ``Command.beforeExecute`` & ``Command.afterExecute`` events are triggered before and after
199+
a command has been executed. Both events contain the ``args`` which are passed to the command
200+
and the ``afterExecute`` event also contains the ``exitCode`` which is returned by the command.
201+
202+
You can listen to this event using an event manager instance::
203+
204+
use Cake\Event\EventManager;
205+
206+
EventManager::instance()->on('Command.beforeExecute', function ($event, $args) {
207+
$command = $event->getSubject();
208+
// Do stuff here
209+
});
210+
211+
EventManager::instance()->on('Command.afterExecute', function ($event, $args, $result) {
212+
$command = $event->getSubject();
213+
// Do stuff here
214+
});
215+
216+
Or using the ``events`` hook in your Application/Plugin class::
217+
218+
use Cake\Event\EventManagerInterface;
219+
220+
public function events(EventManagerInterface $eventManager): EventManagerInterface
221+
{
222+
$eventManager->on('Command.beforeExecute', function ($event, $args) {
223+
$command = $event->getSubject();
224+
// Do stuff here
225+
});
226+
227+
$eventManager->on('Command.afterExecute', function ($event, $args, $result) {
228+
$command = $event->getSubject();
229+
// Do stuff here
230+
});
231+
232+
return $eventManager;
233+
}
234+
235+
192236
.. _registering-event-listeners:
193237

194238
Registering Listeners

0 commit comments

Comments
 (0)