1
|
|
|
<?php namespace Arcanedev\RouteViewer\Entities; |
2
|
|
|
|
3
|
|
|
use Illuminate\Routing\Route as IlluminateRoute; |
4
|
|
|
use Illuminate\Support\Arr; |
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class RouteCollection |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\RouteViewer\Entities |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class RouteCollection extends Collection |
15
|
|
|
{ |
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
17
|
|
|
| Main Functions |
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
19
|
|
|
*/ |
20
|
|
|
/** |
21
|
|
|
* Load the routes. |
22
|
|
|
* |
23
|
|
|
* @param array $routes |
24
|
|
|
* @param array $excludedUris |
25
|
|
|
* @param array $excludedMethods |
26
|
|
|
* @param array $methodColors |
27
|
|
|
* |
28
|
|
|
* @return self |
29
|
|
|
*/ |
30
|
27 |
|
public static function load( |
31
|
|
|
array $routes, array $excludedUris = [], array $excludedMethods = [], array $methodColors = [] |
32
|
|
|
) { |
33
|
27 |
|
return static::make($routes) |
34
|
|
|
->filter(function (IlluminateRoute $route) use ($excludedUris) { |
35
|
27 |
|
return ! Str::startsWith($route->uri(), $excludedUris); |
36
|
27 |
|
}) |
37
|
|
|
->transform(function (IlluminateRoute $route) use ($excludedMethods, $methodColors) { |
38
|
27 |
|
return new Route( |
39
|
27 |
|
static::prepareMethods($route, $excludedMethods, $methodColors), |
|
|
|
|
40
|
27 |
|
$route->uri(), |
41
|
27 |
|
$route->getActionName(), |
42
|
27 |
|
$route->getName(), |
43
|
27 |
|
static::prepareMiddleware($route), |
|
|
|
|
44
|
27 |
|
$route->domain() |
45
|
9 |
|
); |
46
|
27 |
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Prepare route methods. |
51
|
|
|
* |
52
|
|
|
* @param \Illuminate\Routing\Route $route |
53
|
|
|
* @param array $excluded |
54
|
|
|
* @param array $colors |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
27 |
|
private static function prepareMethods(IlluminateRoute $route, array $excluded, array $colors) |
59
|
|
|
{ |
60
|
|
|
return array_map(function ($method) use ($colors) { |
61
|
|
|
return [ |
62
|
27 |
|
'name' => $method, |
63
|
27 |
|
'color' => Arr::get($colors, $method), |
64
|
9 |
|
]; |
65
|
27 |
|
}, array_diff($route->getMethods(), $excluded)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Prepare route middleware. |
70
|
|
|
* |
71
|
|
|
* @param \Illuminate\Routing\Route $route |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
27 |
|
private static function prepareMiddleware(IlluminateRoute $route) |
76
|
|
|
{ |
77
|
|
|
return array_map(function ($value) { |
78
|
27 |
|
return $value instanceof \Closure ? 'Closure' : $value; |
79
|
27 |
|
}, self::gatherMiddleware($route)); |
80
|
15 |
|
} |
81
|
27 |
|
|
82
|
|
|
/** |
83
|
27 |
|
* Gather all the route middleware. |
84
|
27 |
|
* |
85
|
27 |
|
* @param \Illuminate\Routing\Route $route |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
private static function gatherMiddleware(IlluminateRoute $route) |
90
|
|
|
{ |
91
|
|
|
/** @var array $middleware */ |
92
|
|
|
$middleware = $route->middleware(); |
93
|
|
|
|
94
|
|
|
return is_callable([$route, 'controllerMiddleware']) |
95
|
|
|
? array_unique(array_merge($middleware, $route->controllerMiddleware()), SORT_REGULAR) |
96
|
|
|
: $middleware; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: