Skip to content

Commit b014f40

Browse files
authored
refactor(console): exit codes as enum + int (#741)
1 parent f0db5c8 commit b014f40

18 files changed

+71
-17
lines changed

src/Tempest/Console/src/Actions/ExecuteConsoleCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(
2121
) {
2222
}
2323

24-
public function __invoke(string $commandName): ExitCode
24+
public function __invoke(string $commandName): ExitCode|int
2525
{
2626
$callable = $this->getCallable($this->resolveCommandMiddleware($commandName));
2727

src/Tempest/Console/src/Console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
interface Console
1111
{
12-
public function call(string $command): ExitCode;
12+
public function call(string $command): ExitCode|int;
1313

1414
public function readln(): string;
1515

src/Tempest/Console/src/ConsoleApplication.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ public function run(): void
5757
try {
5858
$exitCode = ($this->container->get(ExecuteConsoleCommand::class))($this->argumentBag->getCommandName());
5959

60-
$this->container->get(Kernel::class)->shutdown($exitCode->value);
60+
$exitCode = is_int($exitCode) ? $exitCode : $exitCode->value;
61+
62+
if ($exitCode < 0 || $exitCode > 255) {
63+
throw new InvalidExitCode($exitCode);
64+
}
65+
66+
$this->container->get(Kernel::class)->shutdown($exitCode);
6167
} catch (Throwable $throwable) {
6268
foreach ($this->appConfig->errorHandlers as $exceptionHandler) {
6369
$exceptionHandler->handleException($throwable);

src/Tempest/Console/src/ConsoleMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88

99
interface ConsoleMiddleware
1010
{
11-
public function __invoke(Invocation $invocation, ConsoleMiddlewareCallable $next): ExitCode;
11+
public function __invoke(Invocation $invocation, ConsoleMiddlewareCallable $next): ExitCode|int;
1212
}

src/Tempest/Console/src/ConsoleMiddlewareCallable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct(
1414
) {
1515
}
1616

17-
public function __invoke(Invocation $invocation): ExitCode
17+
public function __invoke(Invocation $invocation): ExitCode|int
1818
{
1919
return ($this->closure)($invocation);
2020
}

src/Tempest/Console/src/Exceptions/ConsoleErrorHandler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
namespace Tempest\Console\Exceptions;
66

77
use Tempest\Console\Console;
8+
use Tempest\Console\ExitCode;
9+
use Tempest\Console\HasExitCode;
810
use Tempest\Console\Input\ConsoleArgumentBag;
911
use Tempest\Container\Tag;
1012
use Tempest\Core\ErrorHandler;
13+
use Tempest\Core\Kernel;
1114
use Tempest\Highlight\Escape;
1215
use Tempest\Highlight\Highlighter;
1316
use Throwable;
1417

1518
final readonly class ConsoleErrorHandler implements ErrorHandler
1619
{
1720
public function __construct(
21+
private Kernel $kernel,
1822
#[Tag('console')]
1923
private Highlighter $highlighter,
2024
private Console $console,
@@ -53,6 +57,10 @@ public function handleException(Throwable $throwable): void
5357
->writeln('<em>-v</em> show more')
5458
->writeln();
5559
}
60+
61+
$exitCode = $throwable instanceof HasExitCode ? $throwable->getExitCode() : ExitCode::ERROR;
62+
63+
$this->kernel->shutdown($exitCode->value);
5664
}
5765

5866
public function handleError(int $errNo, string $errstr, string $errFile, int $errLine): void

src/Tempest/Console/src/ExitCode.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@
44

55
namespace Tempest\Console;
66

7+
/**
8+
* You're free to return any int between 0 and 255 from a console command as exit code.
9+
* The meaning of these integer values will be determined by your application.
10+
* Alternatively, Tempest provides a closed set of predefined exit codes via this enum.
11+
* The exit codes listed here are used by Tempest and assigned a fixed meaning.
12+
*/
713
enum ExitCode: int
814
{
915
case SUCCESS = 0;
1016
case ERROR = 1;
1117
case INVALID = 2;
1218
case CANCELLED = 25;
19+
case CANNOT_EXECUTE = 126;
20+
case COMMAND_NOT_FOUND = 127;
21+
case INVALID_EXIT_CODE = 128;
22+
case TERMINATED = 130;
1323
}

src/Tempest/Console/src/GenericConsole.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(
4343
) {
4444
}
4545

46-
public function call(string $command): ExitCode
46+
public function call(string $command): ExitCode|int
4747
{
4848
return ($this->executeConsoleCommand)($command);
4949
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Console;
6+
7+
interface HasExitCode
8+
{
9+
public function getExitCode(): ExitCode;
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Console;
6+
7+
use Exception;
8+
9+
final class InvalidExitCode extends Exception implements HasExitCode
10+
{
11+
public function __construct(int $original)
12+
{
13+
parent::__construct("An exit code should be between 0 and 255. Instead got {$original}");
14+
}
15+
16+
public function getExitCode(): ExitCode
17+
{
18+
return ExitCode::INVALID_EXIT_CODE;
19+
}
20+
}

0 commit comments

Comments
 (0)