Skip to content

Commit 004c5d9

Browse files
authored
feat: get the last executed spark command name (#10085)
* feat: get the last executed spark command name * add test for DEFAULT_COMMAND
1 parent d6e3f7b commit 004c5d9

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
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: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
*/
2323
class Console
2424
{
25-
private const DEFAULT_COMMAND = 'list';
25+
/**
26+
* @internal
27+
*/
28+
public const DEFAULT_COMMAND = 'list';
29+
30+
private string $command = '';
2631

2732
/**
2833
* @var array<string, string|null>
@@ -60,9 +65,9 @@ public function run(array $tokens = [])
6065
}
6166
}
6267

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

65-
return service('commands')->run($command, array_merge($arguments, $this->options));
70+
return service('commands')->run($this->command, array_merge($arguments, $this->options));
6671
}
6772

6873
public function initialize(): static
@@ -73,6 +78,14 @@ public function initialize(): static
7378
return $this;
7479
}
7580

81+
/**
82+
* Returns the command that is being executed.
83+
*/
84+
public function getCommand(): string
85+
{
86+
return $this->command;
87+
}
88+
7689
/**
7790
* Displays basic information about the Console.
7891
*

tests/system/CLI/ConsoleTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ 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();
169+
$console->run();
170+
$this->assertSame(Console::DEFAULT_COMMAND, $console->getCommand());
171+
172+
$this->initializeConsole('help');
173+
$console->run();
174+
$this->assertSame('help', $console->getCommand());
175+
176+
$this->initializeConsole('list');
177+
$console->run();
178+
$this->assertSame('list', $console->getCommand());
179+
}
180+
163181
private function initializeConsole(string ...$tokens): void
164182
{
165183
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)