Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add event points for spark commands #8496

Merged
merged 4 commits into from
Feb 4, 2024
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
11 changes: 9 additions & 2 deletions system/CLI/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CodeIgniter\CLI;

use CodeIgniter\Autoloader\FileLocatorInterface;
use CodeIgniter\Events\Events;
use CodeIgniter\Log\Logger;
use ReflectionClass;
use ReflectionException;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion system/CLI/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Console
/**
* Runs the current command discovered on the CLI.
*
* @return int|void
* @return int|void Exit code
*
* @throws Exception
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/system/CLI/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <event-points-for-cli-apps>`.
- **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
Expand Down
22 changes: 21 additions & 1 deletion user_guide_src/source/extending/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,34 @@ 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
of "before" controller filters have not yet occurred.
* **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.
Loading