-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRouteListCommand.php
More file actions
88 lines (68 loc) · 2.28 KB
/
Copy pathRouteListCommand.php
File metadata and controls
88 lines (68 loc) · 2.28 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
<?php
declare(strict_types=1);
namespace Zero\Lib\Console\Commands;
use Zero\Lib\Console\Command\CommandInterface;
use Zero\Lib\Router;
final class RouteListCommand implements CommandInterface
{
public function getName(): string
{
return 'route:list';
}
public function getDescription(): string
{
return 'Display the registered routes';
}
public function getUsage(): string
{
return 'php zero route:list';
}
public function execute(array $argv): int
{
$this->bootstrapRoutes();
$routes = Router::getRoutes();
if (empty($routes)) {
\Zero\Lib\Log::channel('internal')->info('No routes have been registered.');
return 0;
}
$rows = [];
foreach ($routes as $route) {
$rows[] = [
$route['method'],
$route['uri'],
$route['name'] ?? '',
$route['action'],
implode(', ', array_map('strval', $route['middleware'])),
];
}
$headers = ['METHOD', 'URI', 'NAME', 'ACTION', 'MIDDLEWARE'];
$widths = array_map('strlen', $headers);
foreach ($rows as $row) {
foreach ($row as $index => $value) {
$widths[$index] = max($widths[$index], strlen($value));
}
}
$line = function (array $columns) use ($widths): string {
$segments = [];
foreach ($columns as $index => $value) {
$segments[] = str_pad($value, $widths[$index]);
}
return implode(' ', $segments);
};
\Zero\Lib\Log::channel('internal')->info($line($headers));
\Zero\Lib\Log::channel('internal')->info(str_repeat('-', array_sum($widths) + (count($widths) - 1) * 2));
foreach ($rows as $row) {
\Zero\Lib\Log::channel('internal')->info($line($row));
}
\Zero\Lib\Log::channel('internal')->info(sprintf('Total: %d routes', count($rows)));
return 0;
}
private function bootstrapRoutes(): void
{
$basePath = $_ENV['BASE_PATH'] ?? dirname(__DIR__, 4);
$webRoutes = $basePath . '/routes/web.php';
if (file_exists($webRoutes)) {
require_once $webRoutes;
}
}
}