From 16f22727f7d25689132e97f00a616e400b498b83 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Mon, 6 Apr 2026 17:34:46 +0800 Subject: [PATCH 1/2] feat: get the last executed spark command name --- system/Boot.php | 6 +++++- system/CLI/Console.php | 14 ++++++++++++-- tests/system/CLI/ConsoleTest.php | 14 ++++++++++++++ user_guide_src/source/changelogs/v4.8.0.rst | 1 + 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index adff06c46194..19856e8c676b 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -431,7 +431,11 @@ protected static function runCommand(Console $console): int $exitCode = $console->initialize()->run(); if (! is_int($exitCode)) { - @trigger_error(sprintf('Starting with CodeIgniter v4.8.0, commands must return an integer exit code. Last command exited with %s. Defaulting to EXIT_SUCCESS.', get_debug_type($exitCode)), E_USER_DEPRECATED); + @trigger_error(sprintf( + 'Since v4.8.0, commands must return an integer exit code. Last command "%s" exited with %s. Defaulting to EXIT_SUCCESS.', + $console->getCommand(), + get_debug_type($exitCode), + ), E_USER_DEPRECATED); $exitCode = EXIT_SUCCESS; } diff --git a/system/CLI/Console.php b/system/CLI/Console.php index bd2950b7ae53..d24f6a0ac476 100644 --- a/system/CLI/Console.php +++ b/system/CLI/Console.php @@ -24,6 +24,8 @@ class Console { private const DEFAULT_COMMAND = 'list'; + private string $command = ''; + /** * @var array */ @@ -60,9 +62,9 @@ public function run(array $tokens = []) } } - $command = array_shift($arguments) ?? self::DEFAULT_COMMAND; + $this->command = array_shift($arguments) ?? self::DEFAULT_COMMAND; - return service('commands')->run($command, array_merge($arguments, $this->options)); + return service('commands')->run($this->command, array_merge($arguments, $this->options)); } public function initialize(): static @@ -73,6 +75,14 @@ public function initialize(): static return $this; } + /** + * Returns the command that is being executed. + */ + public function getCommand(): string + { + return $this->command; + } + /** * Displays basic information about the Console. * diff --git a/tests/system/CLI/ConsoleTest.php b/tests/system/CLI/ConsoleTest.php index d4dc6247a0b7..02e5263ccd2e 100644 --- a/tests/system/CLI/ConsoleTest.php +++ b/tests/system/CLI/ConsoleTest.php @@ -160,6 +160,20 @@ public function testHelpArgumentAndHelpOptionCombined(): void $this->assertStringContainsString('Displays basic usage information.', $this->getStreamFilterBuffer()); } + public function testConsoleReturnsTheLastExecutedCommand(): void + { + $console = new Console(); + $this->assertSame('', $console->getCommand()); + + $this->initializeConsole('help'); + $console->run(); + $this->assertSame('help', $console->getCommand()); + + $this->initializeConsole('list'); + $console->run(); + $this->assertSame('list', $console->getCommand()); + } + private function initializeConsole(string ...$tokens): void { service('superglobals') diff --git a/user_guide_src/source/changelogs/v4.8.0.rst b/user_guide_src/source/changelogs/v4.8.0.rst index 045c38f11ce5..e026fdadaa11 100644 --- a/user_guide_src/source/changelogs/v4.8.0.rst +++ b/user_guide_src/source/changelogs/v4.8.0.rst @@ -172,6 +172,7 @@ Enhancements Commands ======== +- You can now retrieve the last executed command in the console using the new ``Console::getCommand()`` method. This is useful for logging, debugging, or any situation where you need to know which command was run. - ``CLI`` now supports the ``--`` separator to mean that what follows are arguments, not options. This allows you to have arguments that start with ``-`` without them being treated as options. For example: ``spark my:command -- --myarg`` will pass ``--myarg`` as an argument instead of an option. - ``CLI`` now supports options with values specified using an equals sign (e.g., ``--option=value``) in addition to the existing space-separated syntax (e.g., ``--option value``). From 1d34ae7b1738241fbfdd2d48b4f390cf5882b898 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Mon, 6 Apr 2026 19:53:58 +0800 Subject: [PATCH 2/2] add test for DEFAULT_COMMAND --- system/CLI/Console.php | 5 ++++- tests/system/CLI/ConsoleTest.php | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/system/CLI/Console.php b/system/CLI/Console.php index d24f6a0ac476..93500a28a728 100644 --- a/system/CLI/Console.php +++ b/system/CLI/Console.php @@ -22,7 +22,10 @@ */ class Console { - private const DEFAULT_COMMAND = 'list'; + /** + * @internal + */ + public const DEFAULT_COMMAND = 'list'; private string $command = ''; diff --git a/tests/system/CLI/ConsoleTest.php b/tests/system/CLI/ConsoleTest.php index 02e5263ccd2e..bf4e34f58b7e 100644 --- a/tests/system/CLI/ConsoleTest.php +++ b/tests/system/CLI/ConsoleTest.php @@ -165,6 +165,10 @@ public function testConsoleReturnsTheLastExecutedCommand(): void $console = new Console(); $this->assertSame('', $console->getCommand()); + $this->initializeConsole(); + $console->run(); + $this->assertSame(Console::DEFAULT_COMMAND, $console->getCommand()); + $this->initializeConsole('help'); $console->run(); $this->assertSame('help', $console->getCommand());