diff --git a/en/console-commands/commands.rst b/en/console-commands/commands.rst index a19ace8f23..797c69a337 100644 --- a/en/console-commands/commands.rst +++ b/en/console-commands/commands.rst @@ -570,10 +570,54 @@ Lifecycle Callbacks Like Controllers, Commands offer lifecycle events that allow you to observe the framework calling your application code. Commands have: -- ``Command.beforeExecute`` Is called before a command's ``execute()`` method - is. The event is passed the ``ConsoleArguments`` parameter as ``args``. This - event cannot be stopped or have its result replaced. -- ``Command.afterExecute`` Is called after a command's ``execute()`` method is - complete. The event contains ``ConsoleArguments`` as ``args`` and the command - result as ``result``. This event cannot be stopped or have its result - replaced. +- ``Command.beforeExecute`` is called before a command's ``execute()`` method. + The event is passed the ``Arguments`` parameter as ``args`` and the + ``ConsoleIo`` parameter as ``io``. This event cannot be stopped or have its + result replaced. +- ``Command.afterExecute`` is called after a command's ``execute()`` method is + complete. The event contains ``Arguments`` as ``args``, ``ConsoleIo`` as + ``io`` and the command result as ``result``. This event cannot be stopped or + have its result replaced. + +.. versionadded:: 5.3.0 + The ``beforeExecute()`` and ``afterExecute()`` hook methods were added. + +beforeExecute() +--------------- + +.. php:method:: beforeExecute(EventInterface $event, Arguments $args, ConsoleIo $io) + +Called before the ``execute()`` method runs. Useful for initialization and +validation:: + + use Cake\Event\EventInterface; + + class MyCommand extends Command + { + public function beforeExecute(EventInterface $event, Arguments $args, ConsoleIo $io): void + { + parent::beforeExecute($event); + + $io->out('Starting command execution'); + + if (!$this->checkPrerequisites()) { + $io->abort('Prerequisites not met'); + } + } + } + +afterExecute() +-------------- + +.. php:method:: afterExecute(EventInterface $event, Arguments $args, ConsoleIo $io) + +Called after the ``execute()`` method completes. Useful for cleanup and +logging:: + + public function afterExecute(EventInterface $event, Arguments $args, ConsoleIo $io, mixed $result): void + { + parent::afterExecute($event); + + $this->cleanup(); + $io->out('Command execution completed'); + } diff --git a/en/views/helpers/html.rst b/en/views/helpers/html.rst index 0f5d94de3a..439afe6f27 100644 --- a/en/views/helpers/html.rst +++ b/en/views/helpers/html.rst @@ -566,7 +566,7 @@ enable syntax highlighting and LSP support in many editors:: Html->scriptEnd() ?> -The wrapping ``script``tag will be removed and replaced with a script tag +The wrapping ``script`` tag will be removed and replaced with a script tag generated by the helper that includes a CSP nonce if available. Once you have buffered javascript, you can output it as you would any other