Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions en/console-commands/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
2 changes: 1 addition & 1 deletion en/views/helpers/html.rst
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ enable syntax highlighting and LSP support in many editors::
</script>
<?php $this->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
Expand Down