1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bmatovu\RouteList\Http; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Controller; |
6
|
|
|
use Illuminate\Routing\Router; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @see \Illuminate\Foundation\Console\RouteListCommand |
10
|
|
|
*/ |
11
|
|
|
class RouteListController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Router. |
15
|
|
|
* |
16
|
|
|
* @var \Illuminate\Routing\Router |
17
|
|
|
*/ |
18
|
|
|
protected $router; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create a controller instance. |
22
|
|
|
* |
23
|
|
|
* @param string $id |
|
|
|
|
24
|
|
|
* @param Router $router |
25
|
|
|
* |
26
|
|
|
* @return void |
|
|
|
|
27
|
|
|
*/ |
28
|
|
|
public function __construct(Router $router) |
29
|
|
|
{ |
30
|
|
|
$this->router = $router; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Render routes. |
35
|
|
|
* |
36
|
|
|
* @return \Illuminate\Http\Response |
37
|
|
|
*/ |
38
|
|
|
public function __invoke() |
39
|
|
|
{ |
40
|
|
|
$routes = collect($this->router->getRoutes()) |
41
|
|
|
->filter(function ($route) { |
42
|
|
|
return ! $this->matches(config('route-list.excluded'), $route->uri); |
43
|
|
View Code Duplication |
})->map(function ($route) { |
|
|
|
|
44
|
|
|
$name = $route->action['as'] ?? ''; |
45
|
|
|
|
46
|
|
|
if ($name && $this->matches(['/^generated::/'], $name)) { |
47
|
|
|
$name = ''; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return [ |
51
|
|
|
// 'host' => $route->action['where'], |
52
|
|
|
'uri' => $route->uri, |
53
|
|
|
'name' => $name, |
54
|
|
|
'methods' => $route->methods, |
55
|
|
|
'action' => $route->action['controller'] ?? 'Closure', |
56
|
|
|
'middleware' => $this->getRouteMiddleware($route), |
57
|
|
|
]; |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
return view('route-list::index', [ |
61
|
|
|
'routes' => $routes, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get route middleware. |
67
|
|
|
* |
68
|
|
|
* @param \Illuminate\Routing\Route $route |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
protected function getRouteMiddleware($route) |
73
|
|
|
{ |
74
|
|
|
return collect($route->gatherMiddleware())->map(function ($middleware) { |
75
|
|
|
return $middleware instanceof \Closure ? 'Closure' : $middleware; |
76
|
|
|
})->implode(', '); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Perform a regular expression match. |
81
|
|
|
* |
82
|
|
|
* @param array $patterns |
83
|
|
|
* @param string $subject |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
protected function matches($patterns, $subject) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$isMatched = false; |
90
|
|
|
|
91
|
|
|
foreach ($patterns as $pattern) { |
92
|
|
|
if (preg_match($pattern, $subject)) { |
93
|
|
|
$isMatched = true; |
94
|
|
|
|
95
|
|
|
break; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $isMatched; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.