1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bmatovu\RouteList\View\Components; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
6
|
|
|
use Illuminate\View\Component; |
7
|
|
|
|
8
|
|
|
class RouteList extends Component |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Router. |
12
|
|
|
* |
13
|
|
|
* @var \Illuminate\Routing\Router |
14
|
|
|
*/ |
15
|
|
|
protected $router; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Table ID. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $id; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new component instance. |
26
|
|
|
* |
27
|
|
|
* @param string $id |
28
|
|
|
* @param \Illuminate\Routing\Router $router |
29
|
|
|
* |
30
|
|
|
* @return void |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
public function __construct(Router $router, $id = 'routes') |
33
|
|
|
{ |
34
|
|
|
$this->router = $router; |
35
|
|
|
$this->id = $id; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the view / contents that represent the component. |
40
|
|
|
* |
41
|
|
|
* @return \Illuminate\Contracts\View\View|string |
42
|
|
|
*/ |
43
|
|
|
public function render() |
44
|
|
|
{ |
45
|
|
|
$routes = $this->getRoutes(); |
46
|
|
|
|
47
|
|
|
return view('route-list::components.list', [ |
|
|
|
|
48
|
|
|
'id' => $this->id, |
49
|
|
|
'routes' => $routes, |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function getRoutes() |
54
|
|
|
{ |
55
|
|
|
return collect($this->router->getRoutes()) |
56
|
|
|
->filter(function ($route) { |
57
|
|
|
return ! $this->matches(config('route-list.excluded'), $route->uri); |
58
|
|
View Code Duplication |
})->map(function ($route) { |
|
|
|
|
59
|
|
|
$name = $route->action['as'] ?? ''; |
60
|
|
|
|
61
|
|
|
if ($name && $this->matches(['/^generated::/'], $name)) { |
62
|
|
|
$name = ''; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return [ |
66
|
|
|
// 'host' => $route->action['where'], |
67
|
|
|
'uri' => $route->uri, |
68
|
|
|
'name' => $name, |
69
|
|
|
'methods' => $route->methods, |
70
|
|
|
'action' => $route->action['controller'] ?? 'Closure', |
71
|
|
|
'middleware' => $this->getRouteMiddleware($route), |
72
|
|
|
]; |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get route middleware. |
78
|
|
|
* |
79
|
|
|
* @param \Illuminate\Routing\Route $route |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected function getRouteMiddleware($route) |
84
|
|
|
{ |
85
|
|
|
return collect($route->gatherMiddleware())->map(function ($middleware) { |
86
|
|
|
return $middleware instanceof \Closure ? 'Closure' : $middleware; |
87
|
|
|
})->implode(', '); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Perform a regular expression match. |
92
|
|
|
* |
93
|
|
|
* @param array $patterns |
94
|
|
|
* @param string $subject |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
protected function matches($patterns, $subject) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$isMatched = false; |
101
|
|
|
|
102
|
|
|
foreach ($patterns as $pattern) { |
103
|
|
|
if (preg_match($pattern, $subject)) { |
104
|
|
|
$isMatched = true; |
105
|
|
|
|
106
|
|
|
break; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $isMatched; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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.