|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelActive; |
|
6
|
|
|
|
|
7
|
|
|
use Arcanedev\LaravelActive\Contracts\Active as ActiveContract; |
|
8
|
|
|
use Illuminate\Support\{ |
|
9
|
|
|
Arr, |
|
10
|
|
|
Collection, |
|
11
|
|
|
Str}; |
|
12
|
|
|
use Illuminate\Http\Request; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Active |
|
16
|
|
|
* |
|
17
|
|
|
* @package Arcanedev\LaravelActive |
|
18
|
|
|
* @author ARCANEDEV <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class Active implements ActiveContract |
|
21
|
|
|
{ |
|
22
|
|
|
/* ----------------------------------------------------------------- |
|
23
|
|
|
| Properties |
|
24
|
|
|
| ----------------------------------------------------------------- |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected $activeClass; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
protected $fallbackClass; |
|
32
|
|
|
|
|
33
|
|
|
/** @var \Illuminate\Http\Request */ |
|
34
|
|
|
protected $request; |
|
35
|
|
|
|
|
36
|
|
|
/* ----------------------------------------------------------------- |
|
37
|
|
|
| Constructor |
|
38
|
|
|
| ----------------------------------------------------------------- |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Active constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $options |
|
45
|
|
|
*/ |
|
46
|
138 |
|
public function __construct(array $options) |
|
47
|
|
|
{ |
|
48
|
138 |
|
$this->setActiveClass(Arr::get($options, 'class', 'active')); |
|
49
|
138 |
|
$this->setFallbackClass(Arr::get($options, 'fallback-class')); |
|
50
|
138 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/* ----------------------------------------------------------------- |
|
53
|
|
|
| Getters & Setters |
|
54
|
|
|
| ----------------------------------------------------------------- |
|
55
|
|
|
*/ |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the `active` CSS class. |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
24 |
|
public function getActiveClass(): string |
|
63
|
|
|
{ |
|
64
|
24 |
|
return $this->activeClass; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set the `active` CSS class. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $class |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
138 |
|
public function setActiveClass(string $class) |
|
75
|
|
|
{ |
|
76
|
138 |
|
$this->activeClass = $class; |
|
77
|
|
|
|
|
78
|
138 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the fallback (inactive) class. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string|null |
|
85
|
|
|
*/ |
|
86
|
36 |
|
public function getFallbackClass(): ?string |
|
87
|
|
|
{ |
|
88
|
36 |
|
return $this->fallbackClass; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Set the fallback (inactive) class. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string|null $class |
|
95
|
|
|
* |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
138 |
|
public function setFallbackClass(?string $class) |
|
99
|
|
|
{ |
|
100
|
138 |
|
$this->fallbackClass = $class; |
|
101
|
|
|
|
|
102
|
138 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get the request. |
|
107
|
|
|
* |
|
108
|
|
|
* @return \Illuminate\Http\Request |
|
109
|
|
|
*/ |
|
110
|
126 |
|
public function getRequest(): Request |
|
111
|
|
|
{ |
|
112
|
126 |
|
return $this->request ?: app('request'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Set the request. |
|
117
|
|
|
* |
|
118
|
|
|
* @param \Illuminate\Http\Request $request |
|
119
|
|
|
* |
|
120
|
|
|
* @return $this |
|
121
|
|
|
*/ |
|
122
|
6 |
|
public function setRequest(Request $request) |
|
123
|
|
|
{ |
|
124
|
6 |
|
$this->request = $request; |
|
125
|
|
|
|
|
126
|
6 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/* ----------------------------------------------------------------- |
|
130
|
|
|
| Main Methods |
|
131
|
|
|
| ----------------------------------------------------------------- |
|
132
|
|
|
*/ |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the active class if the current path/route is active. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string|array $routes |
|
138
|
|
|
* @param string|null $class |
|
139
|
|
|
* @param string|null $fallback |
|
140
|
|
|
* |
|
141
|
|
|
* @return string|null |
|
142
|
|
|
*/ |
|
143
|
48 |
|
public function active($routes, $class = null, $fallback = null) |
|
144
|
|
|
{ |
|
145
|
48 |
|
return $this->getCssClass( |
|
146
|
48 |
|
$this->is($routes), $class, $fallback |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get the active class if the current route is in haystack routes. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string|array $routes |
|
154
|
|
|
* @param string|null $class |
|
155
|
|
|
* @param string|null $fallback |
|
156
|
|
|
* |
|
157
|
|
|
* @return string|null |
|
158
|
|
|
*/ |
|
159
|
36 |
|
public function route($routes, $class = null, $fallback = null) |
|
160
|
|
|
{ |
|
161
|
36 |
|
return $this->getCssClass( |
|
162
|
36 |
|
$this->isRoute($routes), $class, $fallback |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get the active class if the current path is in haystack paths. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string|array $routes |
|
170
|
|
|
* @param string|null $class |
|
171
|
|
|
* @param string|null $fallback |
|
172
|
|
|
* |
|
173
|
|
|
* @return string|null |
|
174
|
|
|
*/ |
|
175
|
36 |
|
public function path($routes, $class = null, $fallback = null) |
|
176
|
|
|
{ |
|
177
|
36 |
|
return $this->getCssClass( |
|
178
|
36 |
|
$this->isPath($routes), $class, $fallback |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Check if any given routes/paths are active. |
|
184
|
|
|
* |
|
185
|
|
|
* @param string|array $routes |
|
186
|
|
|
* |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
126 |
|
public function is($routes): bool |
|
190
|
|
|
{ |
|
191
|
126 |
|
return $this->isPath($routes) |
|
192
|
126 |
|
|| $this->isRoute($routes); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Check if the current route is one of the given routes. |
|
197
|
|
|
* |
|
198
|
|
|
* @param string|array $routes |
|
199
|
|
|
* |
|
200
|
|
|
* @return bool |
|
201
|
|
|
*/ |
|
202
|
120 |
|
public function isRoute($routes): bool |
|
203
|
|
|
{ |
|
204
|
120 |
|
list($patterns, $ignored) = $this->parseRoutes(Arr::wrap($routes)); |
|
205
|
|
|
|
|
206
|
120 |
|
if ($this->isIgnored($ignored)) { |
|
207
|
24 |
|
return false; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
120 |
|
return $this->getRequest()->routeIs($patterns); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Check if the current path is active. |
|
215
|
|
|
* |
|
216
|
|
|
* @param string|array $routes |
|
217
|
|
|
* |
|
218
|
|
|
* @return bool |
|
219
|
|
|
*/ |
|
220
|
126 |
|
public function isPath($routes): bool |
|
221
|
|
|
{ |
|
222
|
126 |
|
list($routes, $ignored) = $this->parseRoutes(Arr::wrap($routes)); |
|
223
|
|
|
|
|
224
|
126 |
|
if ($this->isIgnored($ignored)) { |
|
225
|
24 |
|
return false; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
126 |
|
return $this->getRequest()->is($routes); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/* ----------------------------------------------------------------- |
|
232
|
|
|
| Other Methods |
|
233
|
|
|
| ----------------------------------------------------------------- |
|
234
|
|
|
*/ |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get the css class based on the given active state. |
|
238
|
|
|
* |
|
239
|
|
|
* @param bool $isActive |
|
240
|
|
|
* @param string|null $class |
|
241
|
|
|
* @param string|null $fallback |
|
242
|
|
|
* |
|
243
|
|
|
* @return string|null |
|
244
|
|
|
*/ |
|
245
|
48 |
|
protected function getCssClass(bool $isActive, ?string $class = null, ?string $fallback = null): ?string |
|
246
|
|
|
{ |
|
247
|
48 |
|
return $isActive |
|
248
|
24 |
|
? ($class ?: $this->getActiveClass()) |
|
249
|
48 |
|
: ($fallback ?: $this->getFallbackClass()); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Check if the given routes/paths are ignored. |
|
254
|
|
|
* |
|
255
|
|
|
* @param array $ignored |
|
256
|
|
|
* |
|
257
|
|
|
* @return bool |
|
258
|
|
|
*/ |
|
259
|
126 |
|
protected function isIgnored(array $ignored): bool |
|
260
|
|
|
{ |
|
261
|
126 |
|
return count($ignored) |
|
262
|
126 |
|
&& ($this->isPath($ignored) || $this->isRoute($ignored)); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Separate ignored routes from the whitelist routes. |
|
267
|
|
|
* |
|
268
|
|
|
* @param array $allRoutes |
|
269
|
|
|
* |
|
270
|
|
|
* @return array |
|
271
|
|
|
*/ |
|
272
|
126 |
|
protected function parseRoutes(array $allRoutes): array |
|
273
|
|
|
{ |
|
274
|
126 |
|
return Collection::make($allRoutes) |
|
275
|
|
|
->partition(function ($route) { |
|
276
|
126 |
|
return ! Str::startsWith($route, ['not:']); |
|
277
|
126 |
|
}) |
|
278
|
|
|
->transform(function (Collection $routes, $index) { |
|
279
|
126 |
|
if ($index === 0) { |
|
280
|
126 |
|
return $routes; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return $routes->transform(function ($route) { |
|
284
|
24 |
|
return substr($route, 4); |
|
285
|
126 |
|
}); |
|
286
|
126 |
|
}) |
|
287
|
126 |
|
->toArray(); |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|