Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
RouteListCommand
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 generate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makeHeader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 writeRaw
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Console\Commands;
6
7use Hyde\Hyde;
8use Illuminate\Support\Arr;
9use Hyde\Console\Concerns\Command;
10use Hyde\Support\Internal\RouteListItem;
11
12use function array_keys;
13use function json_encode;
14use function array_values;
15
16/**
17 * Display the list of site routes.
18 */
19class RouteListCommand extends Command
20{
21    /** @var string */
22    protected $signature = 'route:list {--format=txt : The output format (txt or json)}';
23
24    /** @var string */
25    protected $description = 'Display all the registered routes';
26
27    public function handle(): int
28    {
29        $routes = $this->generate();
30
31        return match ($this->option('format')) {
32            'txt' => $this->table($this->makeHeader($routes), $routes) ?? Command::SUCCESS,
33            'json' => $this->writeRaw(json_encode($routes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)) ?? Command::SUCCESS,
34            default => $this->error("Invalid format provided. Only 'txt' and 'json' are supported.") ?? Command::FAILURE,
35        };
36    }
37
38    /** @return array<integer, array<string, string>>  */
39    protected function generate(): array
40    {
41        return Arr::map(array_values(Hyde::routes()->all()), RouteListItem::format(...));
42    }
43
44    /** @param array<integer, array<string, string>> $routes */
45    protected function makeHeader(array $routes): array
46    {
47        return Arr::map(array_keys($routes[0]), Hyde::makeTitle(...));
48    }
49
50    /** Write a message without ANSI formatting */
51    protected function writeRaw(string $message): void
52    {
53        $this->output->setDecorated(false);
54        $this->output->writeln($message);
55    }
56}