|
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\Utilities; |
|
13
|
|
|
|
|
14
|
|
|
use BlitzPHP\Cli\Commands\Utilities\Routes\AutoRouteCollector; |
|
15
|
|
|
use BlitzPHP\Cli\Commands\Utilities\Routes\MiddlewareCollector; |
|
16
|
|
|
use BlitzPHP\Cli\Commands\Utilities\Routes\SampleURIGenerator; |
|
17
|
|
|
use BlitzPHP\Cli\Console\Command; |
|
18
|
|
|
use BlitzPHP\Loader\Services; |
|
19
|
|
|
use BlitzPHP\Utilities\Helpers; |
|
20
|
|
|
use Closure; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Répertorie toutes les routes. |
|
24
|
|
|
* Cela inclura tous les fichiers Routes qui peuvent être découverts, et inclura les routes qui ne sont pas définies |
|
25
|
|
|
* dans les fichiers de routes, mais sont plutôt découverts via le routage automatique. |
|
26
|
|
|
*/ |
|
27
|
|
|
class Routes extends Command |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string Groupe |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $group = 'BlitzPHP'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string Nom |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $name = 'routes'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string Description |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $description = 'Affiche toutes les routes.'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $service = 'Service de routing'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Les options de la commande. |
|
51
|
|
|
* |
|
52
|
|
|
* @var array<string, string> |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $options = [ |
|
55
|
|
|
'-h' => 'Trier par gestionnaire..', |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritDoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function execute(array $params) |
|
62
|
|
|
{ |
|
63
|
|
|
$sortByHandler = $this->option('h', false); |
|
64
|
|
|
|
|
65
|
|
|
$collection = Services::routes()->loadRoutes(); |
|
66
|
|
|
$methods = [ |
|
67
|
|
|
'get', |
|
68
|
|
|
'head', |
|
69
|
|
|
'post', |
|
70
|
|
|
'patch', |
|
71
|
|
|
'put', |
|
72
|
|
|
'delete', |
|
73
|
|
|
'options', |
|
74
|
|
|
'trace', |
|
75
|
|
|
'connect', |
|
76
|
|
|
'cli', |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
$tbody = []; |
|
80
|
|
|
$uriGenerator = new SampleURIGenerator($collection); |
|
81
|
|
|
$middlewareCollector = new MiddlewareCollector(); |
|
82
|
|
|
|
|
83
|
|
|
foreach ($methods as $method) { |
|
84
|
|
|
$routes = $collection->getRoutes($method); |
|
85
|
|
|
|
|
86
|
|
|
foreach ($routes as $route => $handler) { |
|
87
|
|
|
if (is_string($handler) || $handler instanceof Closure) { |
|
88
|
|
|
$sampleUri = $uriGenerator->get($route); |
|
89
|
|
|
$filters = $middlewareCollector->get($method, $sampleUri); |
|
90
|
|
|
|
|
91
|
|
|
if ($handler instanceof Closure) { |
|
92
|
|
|
$handler = '(Closure)'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$routeName = $collection->getRoutesOptions($route)['as'] ?? '»'; |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$tbody[] = [ |
|
99
|
|
|
strtoupper($method), |
|
100
|
|
|
$route, |
|
101
|
|
|
$routeName, |
|
102
|
|
|
$handler, |
|
103
|
|
|
implode(' ', array_map([Helpers::class, 'classBasename'], $filters)), |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($collection->shouldAutoRoute()) { |
|
110
|
|
|
$autoRouteCollector = new AutoRouteCollector( |
|
111
|
|
|
$collection->getDefaultNamespace(), |
|
112
|
|
|
$collection->getDefaultController(), |
|
113
|
|
|
$collection->getDefaultMethod() |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$autoRoutes = $autoRouteCollector->get(); |
|
117
|
|
|
|
|
118
|
|
|
foreach ($autoRoutes as &$routes) { |
|
119
|
|
|
// Il n'y a pas de méthode "auto", mais il est intentionnel de ne pas obtenir de middlewares de route. |
|
120
|
|
|
$filters = $middlewareCollector->get('auto', $uriGenerator->get($routes[1])); |
|
121
|
|
|
|
|
122
|
|
|
$routes[] = implode(' ', array_map([Helpers::class, 'classBasename'], $filters)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$tbody = [...$tbody, ...$autoRoutes]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// Trier par gestionnaire. |
|
129
|
|
|
if ($sortByHandler) { |
|
130
|
|
|
usort($tbody, static fn ($handler1, $handler2) => strcmp($handler1[3], $handler2[3])); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$table = []; |
|
134
|
|
|
foreach ($tbody as $route) { |
|
135
|
|
|
$table[] = [ |
|
136
|
|
|
'Methode' => $route[0], |
|
137
|
|
|
'Route' => $route[1], |
|
138
|
|
|
'Nom' => $route[2], |
|
139
|
|
|
$sortByHandler ? 'Gestionnaire ↓' : 'Gestionnaire' => $route[3], |
|
140
|
|
|
'Middlewares' => $route[4], |
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$this->table($table); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|