diff --git a/system/CLI/Commands.php b/system/CLI/Commands.php index 7dbdecbc5096..67bfe5d4445c 100644 --- a/system/CLI/Commands.php +++ b/system/CLI/Commands.php @@ -14,6 +14,7 @@ namespace CodeIgniter\CLI; use CodeIgniter\Autoloader\FileLocatorInterface; +use CodeIgniter\Events\Events; use CodeIgniter\Log\Logger; use ReflectionClass; use ReflectionException; @@ -51,7 +52,7 @@ public function __construct($logger = null) /** * Runs a command given * - * @return int|void + * @return int|void Exit code */ public function run(string $command, array $params) { @@ -64,7 +65,13 @@ public function run(string $command, array $params) $className = $this->commands[$command]['class']; $class = new $className($this->logger, $this); - return $class->run($params); + Events::trigger('pre_command'); + + $exit = $class->run($params); + + Events::trigger('post_command'); + + return $exit; } /** diff --git a/system/CLI/Console.php b/system/CLI/Console.php index 90327b9bec9f..725193d424b5 100644 --- a/system/CLI/Console.php +++ b/system/CLI/Console.php @@ -28,7 +28,7 @@ class Console /** * Runs the current command discovered on the CLI. * - * @return int|void + * @return int|void Exit code * * @throws Exception */ diff --git a/tests/system/CLI/ConsoleTest.php b/tests/system/CLI/ConsoleTest.php index b6b14ef96653..54f0ff4a6813 100644 --- a/tests/system/CLI/ConsoleTest.php +++ b/tests/system/CLI/ConsoleTest.php @@ -15,6 +15,7 @@ use CodeIgniter\CodeIgniter; use CodeIgniter\Config\DotEnv; +use CodeIgniter\Events\Events; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockCLIConfig; use CodeIgniter\Test\Mock\MockCodeIgniter; @@ -79,6 +80,38 @@ public function testRun(): void $this->assertStringContainsString('Displays basic usage information.', $this->getStreamFilterBuffer()); } + public function testRunEventsPreCommand(): void + { + $result = ''; + Events::on('pre_command', static function () use (&$result): void { + $result = 'fired'; + }); + + $this->initCLI(); + + $console = new Console(); + $console->run(); + + $this->assertEventTriggered('pre_command'); + $this->assertSame('fired', $result); + } + + public function testRunEventsPostCommand(): void + { + $result = ''; + Events::on('post_command', static function () use (&$result): void { + $result = 'fired'; + }); + + $this->initCLI(); + + $console = new Console(); + $console->run(); + + $this->assertEventTriggered('post_command'); + $this->assertSame('fired', $result); + } + public function testBadCommand(): void { $this->initCLI('bogus'); diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index be8e048c49a4..11f70fbcddf1 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -355,6 +355,8 @@ Others usage in your view files, which was supported by CodeIgniter 3. - **CSP:** Added ``ContentSecurityPolicy::clearDirective()`` method to clear existing CSP directives. See :ref:`csp-clear-directives`. +- **Events:** Added event points ``pre_command`` and ``post_command`` for Spark + commands. See :ref:`Event Points `. - **HTTP:** Added ``Message::addHeader()`` method to add another header with the same name. See :php:meth:`CodeIgniter\\HTTP\\Message::addHeader()`. - **Web Page Caching:** ``ResponseCache`` has been improved to include the request diff --git a/user_guide_src/source/extending/events.rst b/user_guide_src/source/extending/events.rst index 7891249cdcf6..f536c69c7a87 100644 --- a/user_guide_src/source/extending/events.rst +++ b/user_guide_src/source/extending/events.rst @@ -87,7 +87,11 @@ You can stop simulation by passing false: Event Points ============ -The following is a list of available event points within the CodeIgniter core code: +For Web Apps +------------ + +The following is a list of available event points for web applications that are +invoked by **public/index.php**: * **pre_system** Called early during system execution. The URI, Request, and Response have been instantiated, but page cache checking, routing, and execution @@ -95,6 +99,22 @@ The following is a list of available event points within the CodeIgniter core co * **post_controller_constructor** Called immediately after your controller is instantiated, but prior to any method calls happening. * **post_system** Called right before the final rendered page is sent to the browser, at the end of system execution, after the execution of "after" controller filters. + +.. _event-points-for-cli-apps: + +For CLI Apps +------------ + +The following is a list of available event points for :doc:`../cli/spark_commands`: + +* **pre_command** Called right before the command code execution. +* **post_command** Called right after the command code execution. + +Others +------ + +The following is a list of event points available for each of the libraries: + * **email** Called after an email sent successfully from ``CodeIgniter\Email\Email``. Receives an array of the ``Email`` class's properties as a parameter. * **DBQuery** Called after a database query whether successful or not. Receives the ``Query`` object. * **migrate** Called after a successful migration call to ``latest()`` or ``regress()``. Receives the current properties of ``MigrationRunner`` as well as the name of the method.