1
|
|
|
<?php namespace Arcanedev\LaravelActive; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelActive\Contracts\Active as ActiveContract; |
4
|
|
|
use Illuminate\Contracts\Config\Repository; |
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Active |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\LaravelActive |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Active implements ActiveContract |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Illuminate Config instance. |
24
|
|
|
* |
25
|
|
|
* @var \Illuminate\Contracts\Config\Repository |
26
|
|
|
*/ |
27
|
|
|
protected $config; |
28
|
|
|
|
29
|
|
|
/* ----------------------------------------------------------------- |
30
|
|
|
| Constructor |
31
|
|
|
| ----------------------------------------------------------------- |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Active constructor. |
36
|
|
|
* |
37
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config |
38
|
|
|
*/ |
39
|
76 |
|
public function __construct(Repository $config) |
40
|
|
|
{ |
41
|
76 |
|
$this->config = $config; |
42
|
76 |
|
} |
43
|
|
|
|
44
|
|
|
/* ----------------------------------------------------------------- |
45
|
|
|
| Getters & Setters |
46
|
|
|
| ----------------------------------------------------------------- |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the active CSS class. |
51
|
|
|
* |
52
|
|
|
* @param string|null $class |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
16 |
|
protected function getActiveClass($class = null) |
57
|
|
|
{ |
58
|
16 |
|
return $class ?: $this->config->get('active.class', 'active'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the fallback (inactive) class. |
63
|
|
|
* |
64
|
|
|
* @param string|null $fallback |
65
|
|
|
* |
66
|
|
|
* @return string|null |
67
|
|
|
*/ |
68
|
24 |
|
protected function getFallbackClass($fallback = null) |
69
|
|
|
{ |
70
|
24 |
|
return $fallback ?: $this->config->get('active.fallback-class'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/* ----------------------------------------------------------------- |
74
|
|
|
| Main Methods |
75
|
|
|
| ----------------------------------------------------------------- |
76
|
|
|
*/ |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Check if any given routes/paths are active. |
80
|
|
|
* |
81
|
|
|
* @param string|array $routes |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
68 |
|
public function isActive($routes) |
86
|
|
|
{ |
87
|
68 |
|
return $this->isPath($routes) |
88
|
68 |
|
|| $this->isRoute($routes); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the active class if the current path/route is active. |
93
|
|
|
* |
94
|
|
|
* @param string|array $routes |
95
|
|
|
* @param string|null $class |
96
|
|
|
* @param string|null $fallback |
97
|
|
|
* |
98
|
|
|
* @return string|null |
99
|
|
|
*/ |
100
|
24 |
|
public function active($routes, $class = null, $fallback = null) |
101
|
|
|
{ |
102
|
24 |
|
return $this->getCssClass( |
103
|
24 |
|
$this->isActive($routes), $class, $fallback |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the active class if the current route is in haystack routes. |
109
|
|
|
* |
110
|
|
|
* @param string|array $routes |
111
|
|
|
* @param string|null $class |
112
|
|
|
* @param string|null $fallback |
113
|
|
|
* |
114
|
|
|
* @return string|null |
115
|
|
|
*/ |
116
|
16 |
|
public function route($routes, $class = null, $fallback = null) |
117
|
|
|
{ |
118
|
16 |
|
return $this->getCssClass( |
119
|
16 |
|
$this->isRoute($routes), $class, $fallback |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the active class if the current path is in haystack paths. |
125
|
|
|
* |
126
|
|
|
* @param string|array $routes |
127
|
|
|
* @param string|null $class |
128
|
|
|
* @param string|null $fallback |
129
|
|
|
* |
130
|
|
|
* @return string|null |
131
|
|
|
*/ |
132
|
16 |
|
public function path($routes, $class = null, $fallback = null) |
133
|
|
|
{ |
134
|
16 |
|
return $this->getCssClass( |
135
|
16 |
|
$this->isPath($routes), $class, $fallback |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Check if the current route is one of the given routes. |
141
|
|
|
* |
142
|
|
|
* @param string|array $routes |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
68 |
|
public function isRoute($routes) |
147
|
|
|
{ |
148
|
68 |
|
return $this->is(app('router'), $routes); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Check if the current path is active. |
153
|
|
|
* |
154
|
|
|
* @param string|array $routes |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
68 |
|
public function isPath($routes) |
159
|
|
|
{ |
160
|
68 |
|
return $this->is(app('request'), $routes); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/* ----------------------------------------------------------------- |
164
|
|
|
| Other Methods |
165
|
|
|
| ----------------------------------------------------------------- |
166
|
|
|
*/ |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the css class based on the given condition. |
170
|
|
|
* |
171
|
|
|
* @param bool $condition |
172
|
|
|
* @param string|null $class |
173
|
|
|
* @param string|null $fallback |
174
|
|
|
* |
175
|
|
|
* @return string|null |
176
|
|
|
*/ |
177
|
24 |
|
protected function getCssClass($condition, $class, $fallback) |
178
|
|
|
{ |
179
|
24 |
|
return $condition |
180
|
16 |
|
? $this->getActiveClass($class) |
181
|
24 |
|
: $this->getFallbackClass($fallback); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Check if one the given routes is active. |
186
|
|
|
* |
187
|
|
|
* @param mixed $object |
188
|
|
|
* @param array $routes |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
68 |
|
protected function is($object, $routes) |
193
|
|
|
{ |
194
|
68 |
|
list($routes, $ignored) = $this->parseRoutes(Arr::wrap($routes)); |
195
|
|
|
|
196
|
68 |
|
return $this->isIgnored($ignored) |
197
|
8 |
|
? false |
198
|
68 |
|
: call_user_func_array([$object, 'is'], $routes); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Check if the given routes/paths are ignored. |
203
|
|
|
* |
204
|
|
|
* @param array $ignored |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
68 |
|
protected function isIgnored(array $ignored) |
209
|
|
|
{ |
210
|
68 |
|
return count($ignored) |
211
|
68 |
|
&& ($this->isPath($ignored) || $this->isRoute($ignored)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Separate ignored routes from the whitelist routes. |
216
|
|
|
* |
217
|
|
|
* @param array $allRoutes |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
68 |
|
protected function parseRoutes(array $allRoutes) |
222
|
|
|
{ |
223
|
68 |
|
return Collection::make($allRoutes) |
224
|
|
|
->partition(function ($route) { |
225
|
68 |
|
return ! Str::startsWith($route, ['not:']); |
226
|
68 |
|
}) |
227
|
|
|
->transform(function (Collection $routes, $index) { |
228
|
68 |
|
return $index === 0 |
229
|
34 |
|
? $routes |
230
|
|
|
: $routes->transform(function ($route) { return substr($route, 4); }); |
231
|
68 |
|
}) |
232
|
68 |
|
->toArray(); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.