Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion system/Boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 12 additions & 2 deletions system/CLI/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Console
{
private const DEFAULT_COMMAND = 'list';

private string $command = '';

/**
* @var array<string, string|null>
*/
Expand Down Expand Up @@ -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
Expand All @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/system/CLI/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.8.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``).
Expand Down
Loading