Skip to content

Commit 16f2272

Browse files
committed
feat: get the last executed spark command name
1 parent d6e3f7b commit 16f2272

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

system/Boot.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,11 @@ protected static function runCommand(Console $console): int
431431
$exitCode = $console->initialize()->run();
432432

433433
if (! is_int($exitCode)) {
434-
@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);
434+
@trigger_error(sprintf(
435+
'Since v4.8.0, commands must return an integer exit code. Last command "%s" exited with %s. Defaulting to EXIT_SUCCESS.',
436+
$console->getCommand(),
437+
get_debug_type($exitCode),
438+
), E_USER_DEPRECATED);
435439
$exitCode = EXIT_SUCCESS;
436440
}
437441

system/CLI/Console.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class Console
2424
{
2525
private const DEFAULT_COMMAND = 'list';
2626

27+
private string $command = '';
28+
2729
/**
2830
* @var array<string, string|null>
2931
*/
@@ -60,9 +62,9 @@ public function run(array $tokens = [])
6062
}
6163
}
6264

63-
$command = array_shift($arguments) ?? self::DEFAULT_COMMAND;
65+
$this->command = array_shift($arguments) ?? self::DEFAULT_COMMAND;
6466

65-
return service('commands')->run($command, array_merge($arguments, $this->options));
67+
return service('commands')->run($this->command, array_merge($arguments, $this->options));
6668
}
6769

6870
public function initialize(): static
@@ -73,6 +75,14 @@ public function initialize(): static
7375
return $this;
7476
}
7577

78+
/**
79+
* Returns the command that is being executed.
80+
*/
81+
public function getCommand(): string
82+
{
83+
return $this->command;
84+
}
85+
7686
/**
7787
* Displays basic information about the Console.
7888
*

tests/system/CLI/ConsoleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ public function testHelpArgumentAndHelpOptionCombined(): void
160160
$this->assertStringContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());
161161
}
162162

163+
public function testConsoleReturnsTheLastExecutedCommand(): void
164+
{
165+
$console = new Console();
166+
$this->assertSame('', $console->getCommand());
167+
168+
$this->initializeConsole('help');
169+
$console->run();
170+
$this->assertSame('help', $console->getCommand());
171+
172+
$this->initializeConsole('list');
173+
$console->run();
174+
$this->assertSame('list', $console->getCommand());
175+
}
176+
163177
private function initializeConsole(string ...$tokens): void
164178
{
165179
service('superglobals')

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ Enhancements
172172
Commands
173173
========
174174

175+
- 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.
175176
- ``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.
176177
For example: ``spark my:command -- --myarg`` will pass ``--myarg`` as an argument instead of an option.
177178
- ``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``).

0 commit comments

Comments
 (0)