-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathOverviewMiddleware.php
More file actions
100 lines (78 loc) · 3.26 KB
/
OverviewMiddleware.php
File metadata and controls
100 lines (78 loc) · 3.26 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
namespace Tempest\Console\Middleware;
use Tempest\Console\Actions\RenderConsoleCommand;
use Tempest\Console\Console;
use Tempest\Console\ConsoleCommand;
use Tempest\Console\ConsoleConfig;
use Tempest\Console\ConsoleMiddleware;
use Tempest\Console\ConsoleMiddlewareCallable;
use Tempest\Console\ExitCode;
use Tempest\Console\Initializers\Invocation;
use Tempest\Core\DiscoveryCache;
use Tempest\Core\Priority;
use function Tempest\Support\arr;
use function Tempest\Support\str;
#[Priority(Priority::FRAMEWORK - 10)]
final readonly class OverviewMiddleware implements ConsoleMiddleware
{
public function __construct(
private Console $console,
private ConsoleConfig $consoleConfig,
private DiscoveryCache $discoveryCache,
) {}
public function __invoke(Invocation $invocation, ConsoleMiddlewareCallable $next): ExitCode|int
{
if (! $invocation->argumentBag->getCommandName()) {
$this->renderOverview(showHidden: $invocation->argumentBag->has('--all', '-a'));
return ExitCode::SUCCESS;
}
return $next($invocation);
}
private function renderOverview(bool $showHidden = false): void
{
$this->console->header(
header: $this->consoleConfig->name,
subheader: 'This is an overview of available commands.' . PHP_EOL . 'Type <em><command> --help</em> to get more help about a specific command.',
);
if ($this->discoveryCache->enabled) {
$this->console->writeln();
$this->console->error('<style="bold">Caution</style>: discovery cache is enabled');
}
$this->console->writeln();
/** @var \Tempest\Console\ConsoleCommand[][] $commands */
$commands = [];
foreach ($this->consoleConfig->commands as $consoleCommand) {
if ($showHidden === false && $consoleCommand->hidden) {
continue;
}
$parts = explode(':', $consoleCommand->getName());
$group = count($parts) > 1 ? $parts[0] : 'General';
$commands[$group][$consoleCommand->getName()] = $consoleCommand;
}
ksort($commands);
$longestCommandName = max(
arr($commands)
->flatMap(fn (array $group) => $group)
->map(fn (ConsoleCommand $command) => mb_strlen($command->getName()))
->toArray(),
) + 4;
foreach ($commands as $group => $commandsForGroup) {
$title = str(mb_strtoupper($group))
->alignRight($longestCommandName, padding: 5)
->replaceRegex('/^( *)(.*?)( *)$/', "$1<style='dim fg-blue'>//</style> <style='bold fg-blue'>$2</style>$3")
->toString();
$this->console
->writeln()
->writeln($title);
foreach ($commandsForGroup as $consoleCommand) {
(new RenderConsoleCommand($this->console, $longestCommandName))($consoleCommand);
}
}
$this->console
->unless(
condition: $this->discoveryCache->valid,
callback: fn (Console $console) => $console->writeln()->error('Discovery cache invalid. Run discovery:generate to enable discovery caching.'),
);
}
}