-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrentRouteInfo.php
183 lines (159 loc) · 3.91 KB
/
CurrentRouteInfo.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
<?php
declare(strict_types=1);
namespace Permafrost\CurrentRoute;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
/**
* Class CurrentRouteInfo.
*
* @property string|null action
* @property string|null name
* @property \Illuminate\Routing\Route|null route
* @property string|null uri
*/
final class CurrentRouteInfo
{
/** @var Illuminate\Routing\Route|null $route */
protected $route;
/** @var Illuminate\Routing\Router $router */
protected $router;
/**
* Returns a new instance of the CurrentRouteInfo class.
*
* @return \Permafrost\CurrentRoute\CurrentRouteInfo
*/
public static function create($router = null): CurrentRouteInfo
{
return new self($router);
}
public function __construct($router = null)
{
$this->router = $router ? $router : \app()->make(Router::class);
$this->route = $this->router->getCurrentRoute();
}
/**
* Dynamically access route properties.
*
* @param $name
*
* @return \Illuminate\Routing\Route|null
*/
public function __get($name)
{
$validNames = [
'action',
'actionMethod',
'name',
'route',
'uri',
];
if (in_array($name, $validNames, true)) {
return $this->$name();
}
}
/**
* Returns the current route's action name.
*
* @return string|null
*/
public function action(): ?string
{
return optional($this->route)->getActionName();
}
/**
* Returns the current route's action method.
*
* @return string|null
*/
public function actionMethod(): ?string
{
$action = explode('@', $this->action() ?? '@');
return !empty(last($action)) ? last($action) : null;
}
/**
* Returns the current route's name.
*
* @return string|null
*/
public function name(): ?string
{
return optional($this->route)->getName();
}
/**
* Returns the current route.
*
* @return \Illuminate\Routing\Route|null
*/
public function route(): ?Route
{
return $this->route;
}
/**
* Returns the current route's uri pattern.
*
* @return string|null
*/
public function uri(): ?string
{
return optional($this->route)->uri();
}
/**
* Checks the current route name against either a string pattern or
* an array of patterns (wildcards permitted).
*
* @param array|string $patterns
*
* @return bool
*/
public function nameMatches($patterns): bool
{
if (is_string($patterns)) {
$patterns = [$patterns];
}
$routeName = $this->name();
if ($routeName === null) {
return false;
}
foreach ($patterns as $pattern) {
$pattern = preg_quote($pattern, '#');
$pattern = str_replace('\*', '.*', $pattern);
if (preg_match('#^'.$pattern.'\z#u', $routeName) === 1) {
return true;
}
}
return false;
//return $this->router->currentRouteNamed($patterns);
}
/**
* Returns true if $name matches the current route name, otherwise false.
*
* @param string $name
*
* @return bool
*/
public function named(string $name): bool
{
return $this->name === $name;
}
/**
* Returns true if the current route uses the specified middleware,
* otherwise false.
*
* @param string $name
*
* @return bool
*/
public function usesMiddleware(string $name): bool
{
return in_array($name, $this->middleware(), true);
}
/**
* Returns all of the middleware used by the current route.
*
* @return array
*/
public function middleware(): array
{
return optional($this->route)->middleware() ?? [];
}
}