1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Blitz PHP framework. |
5
|
|
|
* |
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\Cli\Commands\Routes; |
13
|
|
|
|
14
|
|
|
use BlitzPHP\Cli\Console\Command; |
15
|
|
|
use BlitzPHP\Container\Services; |
16
|
|
|
use BlitzPHP\Router\DefinedRouteCollector; |
17
|
|
|
use BlitzPHP\Router\Router; |
18
|
|
|
use BlitzPHP\Utilities\Helpers; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Répertorie toutes les routes. |
22
|
|
|
* Cela inclura tous les fichiers Routes qui peuvent être découverts, et inclura les routes qui ne sont pas définies |
23
|
|
|
* dans les fichiers de routes, mais sont plutôt découverts via le routage automatique. |
24
|
|
|
*/ |
25
|
|
|
class Routes extends Command |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string Groupe |
29
|
|
|
*/ |
30
|
|
|
protected $group = 'BlitzPHP'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string Nom |
34
|
|
|
*/ |
35
|
|
|
protected $name = 'route:list'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string Description |
39
|
|
|
*/ |
40
|
|
|
protected $description = 'Affiche toutes les routes.'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $service = 'Service de routing'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Les options de la commande. |
49
|
|
|
* |
50
|
|
|
* @var array<string, string> |
51
|
|
|
*/ |
52
|
|
|
protected $options = [ |
53
|
|
|
'-h' => 'Trier par gestionnaire.', |
54
|
|
|
'--host' => 'Spécifiez nom d\'hôte dans la demande URI.', |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function execute(array $params) |
61
|
|
|
{ |
62
|
|
|
$sortByHandler = $this->option('h', false); |
63
|
|
|
$host = $this->option('host'); |
64
|
|
|
|
65
|
|
|
if ($host) { |
66
|
|
|
putenv('HTTP_HOST=' . $host); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$collection = Services::routes()->loadRoutes(); |
70
|
|
|
$methods = Router::HTTP_METHODS; |
71
|
|
|
|
72
|
|
|
$tbody = []; |
73
|
|
|
$uriGenerator = new SampleURIGenerator($collection); |
74
|
|
|
$middlewareCollector = new MiddlewareCollector(); |
75
|
|
|
$definedRouteCollector = new DefinedRouteCollector($collection); |
76
|
|
|
|
77
|
|
|
foreach ($definedRouteCollector->collect() as $route) { |
78
|
|
|
$sampleUri = $uriGenerator->get($route['route']); |
79
|
|
|
$filters = $middlewareCollector->get($route['method'], $sampleUri); |
80
|
|
|
|
81
|
|
|
$routeName = ($route['route'] === $route['name']) ? '»' : $route['name']; |
82
|
|
|
|
83
|
|
|
$tbody[] = [ |
84
|
|
|
strtoupper($route['method']), |
85
|
|
|
$route['route'], |
86
|
|
|
$routeName, |
87
|
|
|
$route['handler'], |
88
|
|
|
implode(' ', array_map(Helpers::classBasename(...), $filters)), |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($collection->shouldAutoRoute()) { |
93
|
|
|
$autoRouteCollector = new AutoRouteCollector( |
94
|
|
|
$collection->getDefaultNamespace(), |
95
|
|
|
$collection->getDefaultController(), |
96
|
|
|
$collection->getDefaultMethod(), |
97
|
|
|
$methods, |
98
|
|
|
$collection->getRegisteredControllers('*') |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$autoRoutes = $autoRouteCollector->get(); |
102
|
|
|
|
103
|
|
|
// Verification des routes de modules |
104
|
|
|
if ([] !== $routingConfig = config('routing')) { |
|
|
|
|
105
|
|
|
foreach ($routingConfig['module_routes'] as $uri => $namespace) { |
106
|
|
|
$autoRouteCollector = new AutoRouteCollector( |
107
|
|
|
$namespace, |
108
|
|
|
$collection->getDefaultController(), |
109
|
|
|
$collection->getDefaultMethod(), |
110
|
|
|
$methods, |
111
|
|
|
$collection->getRegisteredControllers('*'), |
112
|
|
|
$uri |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$autoRoutes = [...$autoRoutes, ...$autoRouteCollector->get()]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$tbody = [...$tbody, ...$autoRoutes]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Trier par gestionnaire. |
123
|
|
|
if ($sortByHandler) { |
124
|
|
|
usort($tbody, static fn ($handler1, $handler2) => strcmp($handler1[3], $handler2[3])); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$table = []; |
128
|
|
|
|
129
|
|
|
foreach ($tbody as $route) { |
130
|
|
|
$table[] = [ |
131
|
|
|
'Méthode' => $route[0], |
132
|
|
|
'Route' => $route[1], |
133
|
|
|
'Nom' => $route[2], |
134
|
|
|
$sortByHandler ? 'Gestionnaire ↓' : 'Gestionnaire' => $route[3], |
135
|
|
|
'Middlewares' => $route[4], |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($host) { |
140
|
|
|
$this->write('Hôte: ' . $host); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->table($table); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|