Skip to content

Commit 75b5701

Browse files
authored
Merge pull request #8496 from kenjis/feat-spark-events
feat: add event points for spark commands
2 parents 01d8280 + 8ad763e commit 75b5701

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

system/CLI/Commands.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\CLI;
1515

1616
use CodeIgniter\Autoloader\FileLocatorInterface;
17+
use CodeIgniter\Events\Events;
1718
use CodeIgniter\Log\Logger;
1819
use ReflectionClass;
1920
use ReflectionException;
@@ -51,7 +52,7 @@ public function __construct($logger = null)
5152
/**
5253
* Runs a command given
5354
*
54-
* @return int|void
55+
* @return int|void Exit code
5556
*/
5657
public function run(string $command, array $params)
5758
{
@@ -64,7 +65,13 @@ public function run(string $command, array $params)
6465
$className = $this->commands[$command]['class'];
6566
$class = new $className($this->logger, $this);
6667

67-
return $class->run($params);
68+
Events::trigger('pre_command');
69+
70+
$exit = $class->run($params);
71+
72+
Events::trigger('post_command');
73+
74+
return $exit;
6875
}
6976

7077
/**

system/CLI/Console.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Console
2828
/**
2929
* Runs the current command discovered on the CLI.
3030
*
31-
* @return int|void
31+
* @return int|void Exit code
3232
*
3333
* @throws Exception
3434
*/

tests/system/CLI/ConsoleTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\CodeIgniter;
1717
use CodeIgniter\Config\DotEnv;
18+
use CodeIgniter\Events\Events;
1819
use CodeIgniter\Test\CIUnitTestCase;
1920
use CodeIgniter\Test\Mock\MockCLIConfig;
2021
use CodeIgniter\Test\Mock\MockCodeIgniter;
@@ -79,6 +80,38 @@ public function testRun(): void
7980
$this->assertStringContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());
8081
}
8182

83+
public function testRunEventsPreCommand(): void
84+
{
85+
$result = '';
86+
Events::on('pre_command', static function () use (&$result): void {
87+
$result = 'fired';
88+
});
89+
90+
$this->initCLI();
91+
92+
$console = new Console();
93+
$console->run();
94+
95+
$this->assertEventTriggered('pre_command');
96+
$this->assertSame('fired', $result);
97+
}
98+
99+
public function testRunEventsPostCommand(): void
100+
{
101+
$result = '';
102+
Events::on('post_command', static function () use (&$result): void {
103+
$result = 'fired';
104+
});
105+
106+
$this->initCLI();
107+
108+
$console = new Console();
109+
$console->run();
110+
111+
$this->assertEventTriggered('post_command');
112+
$this->assertSame('fired', $result);
113+
}
114+
82115
public function testBadCommand(): void
83116
{
84117
$this->initCLI('bogus');

user_guide_src/source/changelogs/v4.5.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ Others
366366
usage in your view files, which was supported by CodeIgniter 3.
367367
- **CSP:** Added ``ContentSecurityPolicy::clearDirective()`` method to clear
368368
existing CSP directives. See :ref:`csp-clear-directives`.
369+
- **Events:** Added event points ``pre_command`` and ``post_command`` for Spark
370+
commands. See :ref:`Event Points <event-points-for-cli-apps>`.
369371
- **HTTP:** Added ``Message::addHeader()`` method to add another header with
370372
the same name. See :php:meth:`CodeIgniter\\HTTP\\Message::addHeader()`.
371373
- **Web Page Caching:** ``ResponseCache`` has been improved to include the request

user_guide_src/source/extending/events.rst

+21-1
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,34 @@ You can stop simulation by passing false:
8787
Event Points
8888
============
8989

90-
The following is a list of available event points within the CodeIgniter core code:
90+
For Web Apps
91+
------------
92+
93+
The following is a list of available event points for web applications that are
94+
invoked by **public/index.php**:
9195

9296
* **pre_system** Called early during system execution. The URI, Request, and
9397
Response have been instantiated, but page cache checking, routing, and execution
9498
of "before" controller filters have not yet occurred.
9599
* **post_controller_constructor** Called immediately after your controller is instantiated, but prior to any method calls happening.
96100
* **post_system** Called right before the final rendered page is sent to the browser,
97101
at the end of system execution, after the execution of "after" controller filters.
102+
103+
.. _event-points-for-cli-apps:
104+
105+
For CLI Apps
106+
------------
107+
108+
The following is a list of available event points for :doc:`../cli/spark_commands`:
109+
110+
* **pre_command** Called right before the command code execution.
111+
* **post_command** Called right after the command code execution.
112+
113+
Others
114+
------
115+
116+
The following is a list of event points available for each of the libraries:
117+
98118
* **email** Called after an email sent successfully from ``CodeIgniter\Email\Email``. Receives an array of the ``Email`` class's properties as a parameter.
99119
* **DBQuery** Called after a database query whether successful or not. Receives the ``Query`` object.
100120
* **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.

0 commit comments

Comments
 (0)