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 \Illuminate\Routing\Router $router |
24
|
|
|
* |
25
|
|
|
* @return void |
|
|
|
|
26
|
|
|
*/ |
27
|
1 |
|
public function __construct(Router $router) |
28
|
|
|
{ |
29
|
1 |
|
$this->router = $router; |
30
|
1 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Render routes. |
34
|
|
|
* |
35
|
|
|
* @return \Illuminate\View\View |
36
|
|
|
*/ |
37
|
1 |
|
public function __invoke() |
38
|
|
|
{ |
39
|
1 |
|
$routes = collect($this->router->getRoutes()) |
40
|
1 |
|
->filter(function ($route) { |
41
|
1 |
|
return ! $this->matches(config('route-list.excluded'), $route->uri); |
42
|
1 |
View Code Duplication |
})->map(function ($route) { |
|
|
|
|
43
|
1 |
|
$name = $route->action['as'] ?? ''; |
44
|
|
|
|
45
|
1 |
|
if ($name && $this->matches(['/^generated::/'], $name)) { |
46
|
|
|
$name = ''; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return [ |
50
|
|
|
// 'host' => $route->action['where'], |
51
|
1 |
|
'uri' => $route->uri, |
52
|
1 |
|
'name' => $name, |
53
|
1 |
|
'methods' => $route->methods, |
54
|
1 |
|
'action' => $route->action['controller'] ?? 'Closure', |
55
|
1 |
|
'middleware' => $this->getRouteMiddleware($route), |
56
|
|
|
]; |
57
|
1 |
|
}); |
58
|
|
|
|
59
|
1 |
|
return view('route-list::index', [ |
|
|
|
|
60
|
1 |
|
'routes' => $routes, |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get route middleware. |
66
|
|
|
* |
67
|
|
|
* @param \Illuminate\Routing\Route $route |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
1 |
|
protected function getRouteMiddleware($route) |
72
|
|
|
{ |
73
|
1 |
|
$middlewares = collect($route->gatherMiddleware())->map(function ($middleware) { |
74
|
1 |
|
return $middleware instanceof \Closure ? 'Closure' : $middleware; |
75
|
1 |
|
}); |
76
|
|
|
|
77
|
1 |
|
return $middlewares->implode(', '); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Perform a regular expression match. |
82
|
|
|
* |
83
|
|
|
* @param array $patterns |
84
|
|
|
* @param string $subject |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
1 |
View Code Duplication |
protected function matches($patterns, $subject) |
|
|
|
|
89
|
|
|
{ |
90
|
1 |
|
$isMatched = false; |
91
|
|
|
|
92
|
1 |
|
foreach ($patterns as $pattern) { |
93
|
1 |
|
if (preg_match($pattern, $subject)) { |
94
|
|
|
$isMatched = true; |
95
|
|
|
|
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return $isMatched; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.