-
Notifications
You must be signed in to change notification settings - Fork 11.2k
/
Copy pathRouteListCommand.php
497 lines (431 loc) · 14.7 KB
/
RouteListCommand.php
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
namespace Illuminate\Foundation\Console;
use Closure;
use Illuminate\Console\Command;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Routing\ViewController;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionFunction;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Terminal;
#[AsCommand(name: 'route:list')]
class RouteListCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'route:list';
/**
* The console command description.
*
* @var string
*/
protected $description = 'List all registered routes';
/**
* The router instance.
*
* @var \Illuminate\Routing\Router
*/
protected $router;
/**
* The table headers for the command.
*
* @var string[]
*/
protected $headers = ['Domain', 'Method', 'URI', 'Name', 'Action', 'Middleware'];
/**
* The terminal width resolver callback.
*
* @var \Closure|null
*/
protected static $terminalWidthResolver;
/**
* The verb colors for the command.
*
* @var array
*/
protected $verbColors = [
'ANY' => 'red',
'GET' => 'blue',
'HEAD' => '#6C7280',
'OPTIONS' => '#6C7280',
'POST' => 'yellow',
'PUT' => 'yellow',
'PATCH' => 'yellow',
'DELETE' => 'red',
];
/**
* Create a new route command instance.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function __construct(Router $router)
{
parent::__construct();
$this->router = $router;
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->router->flushMiddlewareGroups();
if (! $this->router->getRoutes()->count()) {
return $this->components->error("Your application doesn't have any routes.");
}
if (empty($routes = $this->getRoutes())) {
return $this->components->error("Your application doesn't have any routes matching the given criteria.");
}
$this->displayRoutes($routes);
}
/**
* Compile the routes into a displayable format.
*
* @return array
*/
protected function getRoutes()
{
$routes = collect($this->router->getRoutes())->map(function ($route) {
return $this->getRouteInformation($route);
})->filter()->all();
if (($sort = $this->option('sort')) !== null) {
$routes = $this->sortRoutes($sort, $routes);
} else {
$routes = $this->sortRoutes('uri', $routes);
}
if ($this->option('reverse')) {
$routes = array_reverse($routes);
}
return $this->pluckColumns($routes);
}
/**
* Get the route information for a given route.
*
* @param \Illuminate\Routing\Route $route
* @return array
*/
protected function getRouteInformation(Route $route)
{
return $this->filterRoute([
'domain' => $route->domain(),
'method' => implode('|', $route->methods()),
'uri' => $route->uri(),
'name' => $route->getName(),
'action' => ltrim($route->getActionName(), '\\'),
'middleware' => $this->getMiddleware($route),
'vendor' => $this->isVendorRoute($route),
]);
}
/**
* Sort the routes by a given element.
*
* @param string $sort
* @param array $routes
* @return array
*/
protected function sortRoutes($sort, array $routes)
{
return Arr::sort($routes, function ($route) use ($sort) {
return $route[$sort];
});
}
/**
* Remove unnecessary columns from the routes.
*
* @param array $routes
* @return array
*/
protected function pluckColumns(array $routes)
{
return array_map(function ($route) {
return Arr::only($route, $this->getColumns());
}, $routes);
}
/**
* Display the route information on the console.
*
* @param array $routes
* @return void
*/
protected function displayRoutes(array $routes)
{
$routes = collect($routes);
$this->output->writeln(
$this->option('json') ? $this->asJson($routes) : $this->forCli($routes)
);
}
/**
* Get the middleware for the route.
*
* @param \Illuminate\Routing\Route $route
* @return string
*/
protected function getMiddleware($route)
{
return collect($this->router->gatherRouteMiddleware($route))->map(function ($middleware) {
return $middleware instanceof Closure ? 'Closure' : $middleware;
})->implode("\n");
}
/**
* Determine if the route has been defined outside of the application.
*
* @param \Illuminate\Routing\Route $route
* @return bool
*/
protected function isVendorRoute(Route $route)
{
if ($route->action['uses'] instanceof Closure) {
$path = (new ReflectionFunction($route->action['uses']))
->getFileName();
} elseif (is_string($route->action['uses']) &&
str_contains($route->action['uses'], 'SerializableClosure')) {
return false;
} elseif (is_string($route->action['uses'])) {
if ($this->isFrameworkController($route)) {
return false;
}
$path = (new ReflectionClass($route->getControllerClass()))
->getFileName();
} else {
return false;
}
return str_starts_with($path, base_path('vendor'));
}
/**
* Determine if the route uses a framework controller.
*
* @param \Illuminate\Routing\Route $route
* @return bool
*/
protected function isFrameworkController(Route $route)
{
return in_array($route->getControllerClass(), [
'\Illuminate\Routing\RedirectController',
'\Illuminate\Routing\ViewController',
], true);
}
/**
* Filter the route by URI and / or name.
*
* @param array $route
* @return array|null
*/
protected function filterRoute(array $route)
{
if (($this->option('name') && ! Str::contains((string) $route['name'], $this->option('name'))) ||
($this->option('path') && ! Str::contains($route['uri'], $this->option('path'))) ||
($this->option('method') && ! Str::contains($route['method'], strtoupper($this->option('method')))) ||
($this->option('domain') && ! Str::contains((string) $route['domain'], $this->option('domain'))) ||
($this->option('except-vendor') && $route['vendor']) ||
($this->option('only-vendor') && ! $route['vendor'])) {
return;
}
if ($this->option('except-path')) {
foreach (explode(',', $this->option('except-path')) as $path) {
if (str_contains($route['uri'], $path)) {
return;
}
}
}
return $route;
}
/**
* Get the table headers for the visible columns.
*
* @return array
*/
protected function getHeaders()
{
return Arr::only($this->headers, array_keys($this->getColumns()));
}
/**
* Get the column names to show (lowercase table headers).
*
* @return array
*/
protected function getColumns()
{
return array_map('strtolower', $this->headers);
}
/**
* Parse the column list.
*
* @param array $columns
* @return array
*/
protected function parseColumns(array $columns)
{
$results = [];
foreach ($columns as $column) {
if (str_contains($column, ',')) {
$results = array_merge($results, explode(',', $column));
} else {
$results[] = $column;
}
}
return array_map('strtolower', $results);
}
/**
* Convert the given routes to JSON.
*
* @param \Illuminate\Support\Collection $routes
* @return string
*/
protected function asJson($routes)
{
return $routes
->map(function ($route) {
$route['middleware'] = empty($route['middleware']) ? [] : explode("\n", $route['middleware']);
return $route;
})
->values()
->toJson();
}
/**
* Convert the given routes to regular CLI output.
*
* @param \Illuminate\Support\Collection $routes
* @return array
*/
protected function forCli($routes)
{
$routes = $routes->map(
fn ($route) => array_merge($route, [
'action' => $this->formatActionForCli($route),
'method' => $route['method'] == 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS' ? 'ANY' : $route['method'],
'uri' => $route['domain'] ? ($route['domain'].'/'.ltrim($route['uri'], '/')) : $route['uri'],
]),
);
$maxMethod = mb_strlen($routes->max('method'));
$terminalWidth = $this->getTerminalWidth();
$routeCount = $this->determineRouteCountOutput($routes, $terminalWidth);
return $routes->map(function ($route) use ($maxMethod, $terminalWidth) {
[
'action' => $action,
'domain' => $domain,
'method' => $method,
'middleware' => $middleware,
'uri' => $uri,
] = $route;
$middleware = Str::of($middleware)->explode("\n")->filter()->whenNotEmpty(
fn ($collection) => $collection->map(
fn ($middleware) => sprintf(' %s⇂ %s', str_repeat(' ', $maxMethod), $middleware)
)
)->implode("\n");
$spaces = str_repeat(' ', max($maxMethod + 6 - mb_strlen($method), 0));
$dots = str_repeat('.', max(
$terminalWidth - mb_strlen($method.$spaces.$uri.$action) - 6 - ($action ? 1 : 0), 0
));
$dots = empty($dots) ? $dots : " $dots";
if ($action && ! $this->output->isVerbose() && mb_strlen($method.$spaces.$uri.$action.$dots) > ($terminalWidth - 6)) {
$action = substr($action, 0, $terminalWidth - 7 - mb_strlen($method.$spaces.$uri.$dots)).'…';
}
$method = Str::of($method)->explode('|')->map(
fn ($method) => sprintf('<fg=%s>%s</>', $this->verbColors[$method] ?? 'default', $method),
)->implode('<fg=#6C7280>|</>');
return [sprintf(
' <fg=white;options=bold>%s</> %s<fg=white>%s</><fg=#6C7280>%s %s</>',
$method,
$spaces,
preg_replace('#({[^}]+})#', '<fg=yellow>$1</>', $uri),
$dots,
str_replace(' ', ' › ', $action ?? ''),
), $this->output->isVerbose() && ! empty($middleware) ? "<fg=#6C7280>$middleware</>" : null];
})
->flatten()
->filter()
->prepend('')
->push('')->push($routeCount)->push('')
->toArray();
}
/**
* Get the formatted action for display on the CLI.
*
* @param array $route
* @return string
*/
protected function formatActionForCli($route)
{
['action' => $action, 'name' => $name] = $route;
if ($action === 'Closure' || $action === ViewController::class) {
return $name;
}
$name = $name ? "$name " : null;
$rootControllerNamespace = $this->laravel[UrlGenerator::class]->getRootControllerNamespace()
?? ($this->laravel->getNamespace().'Http\\Controllers');
if (str_starts_with($action, $rootControllerNamespace)) {
return $name.substr($action, mb_strlen($rootControllerNamespace) + 1);
}
$actionClass = explode('@', $action)[0];
if (class_exists($actionClass) && str_starts_with((new ReflectionClass($actionClass))->getFilename(), base_path('vendor'))) {
$actionCollection = collect(explode('\\', $action));
return $name.$actionCollection->take(2)->implode('\\').' '.$actionCollection->last();
}
return $name.$action;
}
/**
* Determine and return the output for displaying the number of routes in the CLI output.
*
* @param \Illuminate\Support\Collection $routes
* @param int $terminalWidth
* @return string
*/
protected function determineRouteCountOutput($routes, $terminalWidth)
{
$routeCountText = 'Showing ['.$routes->count().'] routes';
$offset = $terminalWidth - mb_strlen($routeCountText) - 2;
$spaces = str_repeat(' ', $offset);
return $spaces.'<fg=blue;options=bold>Showing ['.$routes->count().'] routes</>';
}
/**
* Get the terminal width.
*
* @return int
*/
public static function getTerminalWidth()
{
return is_null(static::$terminalWidthResolver)
? (new Terminal)->getWidth()
: call_user_func(static::$terminalWidthResolver);
}
/**
* Set a callback that should be used when resolving the terminal width.
*
* @param \Closure|null $resolver
* @return void
*/
public static function resolveTerminalWidthUsing($resolver)
{
static::$terminalWidthResolver = $resolver;
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['json', null, InputOption::VALUE_NONE, 'Output the route list as JSON'],
['method', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by method'],
['name', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by name'],
['domain', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by domain'],
['path', null, InputOption::VALUE_OPTIONAL, 'Only show routes matching the given path pattern'],
['except-path', null, InputOption::VALUE_OPTIONAL, 'Do not display the routes matching the given path pattern'],
['reverse', 'r', InputOption::VALUE_NONE, 'Reverse the ordering of the routes'],
['sort', null, InputOption::VALUE_OPTIONAL, 'The column (domain, method, uri, name, action, middleware) to sort by', 'uri'],
['except-vendor', null, InputOption::VALUE_NONE, 'Do not display routes defined by vendor packages'],
['only-vendor', null, InputOption::VALUE_NONE, 'Only display routes defined by vendor packages'],
];
}
}