From 7f4cf53a7f65666ad7832ec03a83788210e7770b Mon Sep 17 00:00:00 2001 From: Jamison Bryant Date: Mon, 15 Sep 2025 10:07:18 -0400 Subject: [PATCH 1/6] Add link to events subpage --- en/console-commands.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/en/console-commands.rst b/en/console-commands.rst index c40f966c9c..40372d90bd 100644 --- a/en/console-commands.rst +++ b/en/console-commands.rst @@ -135,6 +135,7 @@ command. Then learn more about commands: console-commands/commands console-commands/input-output console-commands/option-parsers + console-commands/lifecycle-events console-commands/cron-jobs CakePHP Provided Commands From 93df5e6e5e4eeaddadc719bb7352c066d22fe530 Mon Sep 17 00:00:00 2001 From: Jamison Bryant Date: Mon, 15 Sep 2025 10:07:27 -0400 Subject: [PATCH 2/6] Add subpage --- en/console-commands/lifecycle-events.rst | 82 ++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 en/console-commands/lifecycle-events.rst diff --git a/en/console-commands/lifecycle-events.rst b/en/console-commands/lifecycle-events.rst new file mode 100644 index 0000000000..91327008e9 --- /dev/null +++ b/en/console-commands/lifecycle-events.rst @@ -0,0 +1,82 @@ +Command Lifecycle Events +######################## + +.. php:namespace:: Cake\Console + +CakePHP commands trigger events during execution that allow you to hook into +different stages of the command lifecycle. These events work similarly to +controller lifecycle callbacks. + +Event List +========== + +The following events are triggered during command execution: + +* ``Command.beforeExecute`` - Before the ``execute()`` method +* ``Command.afterExecute`` - After the ``execute()`` method completes + +Lifecycle Callback Methods +========================== + +beforeExecute() +--------------- + +.. php:method:: beforeExecute(EventInterface $event) + +Called before the ``execute()`` method runs. Useful for initialization and +validation:: + + use Cake\Event\EventInterface; + + class MyCommand extends Command + { + public function beforeExecute(EventInterface $event): void + { + parent::beforeExecute($event); + + $this->log('Starting command execution'); + + if (!$this->checkPrerequisites()) { + $event->stopPropagation(); + $this->io()->error('Prerequisites not met'); + } + } + } + +afterExecute() +-------------- + +.. php:method:: afterExecute(EventInterface $event) + +Called after the ``execute()`` method completes. Useful for cleanup and +logging:: + + public function afterExecute(EventInterface $event): void + { + parent::afterExecute($event); + + $this->cleanup(); + $this->log('Command execution completed'); + } + +Stopping Command Execution +========================== + +You can prevent the ``execute()`` method from running by stopping event +propagation in ``beforeExecute()``:: + + public function beforeExecute(EventInterface $event): void + { + if (!$this->isValid()) { + $event->stopPropagation(); + $event->setResult(static::CODE_ERROR); + } + } + +When propagation is stopped, ``execute()`` won't run but ``afterExecute()`` +will still be called for cleanup. + +.. note:: + + Remember to call ``parent::beforeExecute($event)`` and + ``parent::afterExecute($event)`` when overriding these methods. \ No newline at end of file From 588ba60a0b1d98239a5b28bd9a4e31682259a916 Mon Sep 17 00:00:00 2001 From: Jamison Bryant Date: Mon, 15 Sep 2025 11:02:08 -0400 Subject: [PATCH 3/6] Update the existing docs --- en/console-commands.rst | 1 - en/console-commands/commands.rst | 56 ++++++++++++++-- en/console-commands/lifecycle-events.rst | 82 ------------------------ 3 files changed, 49 insertions(+), 90 deletions(-) delete mode 100644 en/console-commands/lifecycle-events.rst diff --git a/en/console-commands.rst b/en/console-commands.rst index 40372d90bd..c40f966c9c 100644 --- a/en/console-commands.rst +++ b/en/console-commands.rst @@ -135,7 +135,6 @@ command. Then learn more about commands: console-commands/commands console-commands/input-output console-commands/option-parsers - console-commands/lifecycle-events console-commands/cron-jobs CakePHP Provided Commands diff --git a/en/console-commands/commands.rst b/en/console-commands/commands.rst index a19ace8f23..60a2fae8e7 100644 --- a/en/console-commands/commands.rst +++ b/en/console-commands/commands.rst @@ -570,10 +570,52 @@ 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. + +beforeExecute() +--------------- + +.. php:method:: beforeExecute(EventInterface $event) + +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->error('Prerequisites not met'); + die; + } + } + } + +afterExecute() +-------------- + +.. php:method:: afterExecute(EventInterface $event) + +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/console-commands/lifecycle-events.rst b/en/console-commands/lifecycle-events.rst deleted file mode 100644 index 91327008e9..0000000000 --- a/en/console-commands/lifecycle-events.rst +++ /dev/null @@ -1,82 +0,0 @@ -Command Lifecycle Events -######################## - -.. php:namespace:: Cake\Console - -CakePHP commands trigger events during execution that allow you to hook into -different stages of the command lifecycle. These events work similarly to -controller lifecycle callbacks. - -Event List -========== - -The following events are triggered during command execution: - -* ``Command.beforeExecute`` - Before the ``execute()`` method -* ``Command.afterExecute`` - After the ``execute()`` method completes - -Lifecycle Callback Methods -========================== - -beforeExecute() ---------------- - -.. php:method:: beforeExecute(EventInterface $event) - -Called before the ``execute()`` method runs. Useful for initialization and -validation:: - - use Cake\Event\EventInterface; - - class MyCommand extends Command - { - public function beforeExecute(EventInterface $event): void - { - parent::beforeExecute($event); - - $this->log('Starting command execution'); - - if (!$this->checkPrerequisites()) { - $event->stopPropagation(); - $this->io()->error('Prerequisites not met'); - } - } - } - -afterExecute() --------------- - -.. php:method:: afterExecute(EventInterface $event) - -Called after the ``execute()`` method completes. Useful for cleanup and -logging:: - - public function afterExecute(EventInterface $event): void - { - parent::afterExecute($event); - - $this->cleanup(); - $this->log('Command execution completed'); - } - -Stopping Command Execution -========================== - -You can prevent the ``execute()`` method from running by stopping event -propagation in ``beforeExecute()``:: - - public function beforeExecute(EventInterface $event): void - { - if (!$this->isValid()) { - $event->stopPropagation(); - $event->setResult(static::CODE_ERROR); - } - } - -When propagation is stopped, ``execute()`` won't run but ``afterExecute()`` -will still be called for cleanup. - -.. note:: - - Remember to call ``parent::beforeExecute($event)`` and - ``parent::afterExecute($event)`` when overriding these methods. \ No newline at end of file From ab1128665afa41c0d29badb1ca67dd2305c329d4 Mon Sep 17 00:00:00 2001 From: Jamison Bryant Date: Mon, 15 Sep 2025 11:03:28 -0400 Subject: [PATCH 4/6] Add versionadded meta --- en/console-commands/commands.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/en/console-commands/commands.rst b/en/console-commands/commands.rst index 60a2fae8e7..3d8f991a54 100644 --- a/en/console-commands/commands.rst +++ b/en/console-commands/commands.rst @@ -579,10 +579,13 @@ the framework calling your application code. Commands have: ``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) +.. php:method:: beforeExecute(EventInterface $event, Arguments $args, ConsoleIo $io) Called before the ``execute()`` method runs. Useful for initialization and validation:: @@ -607,7 +610,7 @@ validation:: afterExecute() -------------- -.. php:method:: afterExecute(EventInterface $event) +.. php:method:: afterExecute(EventInterface $event, Arguments $args, ConsoleIo $io) Called after the ``execute()`` method completes. Useful for cleanup and logging:: From b7dff98868f5c67e4cf130fb24558863e475be2d Mon Sep 17 00:00:00 2001 From: Jamison Bryant Date: Mon, 15 Sep 2025 11:05:36 -0400 Subject: [PATCH 5/6] Add a space --- en/views/helpers/html.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 0177c4d9758c2120ba720420e81299e730dd7018 Mon Sep 17 00:00:00 2001 From: Jamison Bryant Date: Mon, 15 Sep 2025 11:08:05 -0400 Subject: [PATCH 6/6] Switch out die for abort() --- en/console-commands/commands.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/en/console-commands/commands.rst b/en/console-commands/commands.rst index 3d8f991a54..797c69a337 100644 --- a/en/console-commands/commands.rst +++ b/en/console-commands/commands.rst @@ -601,8 +601,7 @@ validation:: $io->out('Starting command execution'); if (!$this->checkPrerequisites()) { - $io->error('Prerequisites not met'); - die; + $io->abort('Prerequisites not met'); } } }