-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathConsoleApplication.php
More file actions
63 lines (51 loc) · 2.11 KB
/
Copy pathConsoleApplication.php
File metadata and controls
63 lines (51 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace Tempest\Console;
use Tempest\Console\Actions\ExecuteConsoleCommand;
use Tempest\Console\Input\ConsoleArgumentBag;
use Tempest\Container\Container;
use Tempest\Core\Application;
use Tempest\Core\Kernel;
use Tempest\Core\Tempest;
use Tempest\Support\Str;
final readonly class ConsoleApplication implements Application
{
public function __construct(
private Container $container,
private ConsoleArgumentBag $argumentBag,
) {}
/**
* Boots the console application.
*
* @param string|null $root The root directory of the application. By default, the current working directory.
* @param \Tempest\Discovery\DiscoveryLocation[] $discoveryLocations The locations to use for class discovery.
* @param string|null $internalStorage The *absolute* internal storage directory for Tempest.
* @param string $name The name of the console application.
* @param bool $loadBuiltInCommands Whether to load built-in Tempest console commands.
*/
public static function boot(
?string $root = null,
array $discoveryLocations = [],
?string $internalStorage = null,
?string $name = null,
?bool $loadBuiltInCommands = true,
): self {
if (! $internalStorage && $name) {
$internalStorage = sprintf('.%s', Str\to_kebab_case($name));
}
$container = Tempest::boot($root, $discoveryLocations, $internalStorage);
$consoleConfig = $container->get(ConsoleConfig::class);
$consoleConfig->name ??= $name;
$consoleConfig->loadBuiltInCommands = $loadBuiltInCommands ?? $consoleConfig->loadBuiltInCommands;
return $container->get(ConsoleApplication::class);
}
public function run(): void
{
$exitCode = $this->container->get(ExecuteConsoleCommand::class)($this->argumentBag->getCommandName());
$exitCode = is_int($exitCode) ? $exitCode : $exitCode->value;
if ($exitCode < 0 || $exitCode > 255) {
throw new ExitCodeWasInvalid($exitCode);
}
$this->container->get(Kernel::class)->shutdown($exitCode);
}
}