|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JeroenNoten\LaravelAdminLte\Menu; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
class ActiveChecker |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The request instance. |
|
13
|
|
|
* |
|
14
|
|
|
* @var Request |
|
15
|
|
|
*/ |
|
16
|
|
|
private $request; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The url generator instance. |
|
20
|
|
|
* |
|
21
|
|
|
* @var UrlGenerator |
|
22
|
|
|
*/ |
|
23
|
|
|
private $url; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param Request $request |
|
29
|
|
|
* @param UrlGenerator $url |
|
30
|
|
|
*/ |
|
31
|
56 |
|
public function __construct(Request $request, UrlGenerator $url) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
56 |
|
$this->request = $request; |
|
34
|
56 |
|
$this->url = $url; |
|
35
|
56 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Checks if a menu item is currently active. Active items will be |
|
39
|
|
|
* highlighted. |
|
40
|
|
|
* |
|
41
|
|
|
* @param mixed $item The menu item to check |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
55 |
|
public function isActive($item) |
|
45
|
|
|
{ |
|
46
|
55 |
|
if (isset($item['submenu'])) { |
|
47
|
18 |
|
return $this->containsActive($item['submenu']); |
|
48
|
52 |
|
} elseif (isset($item['active'])) { |
|
49
|
19 |
|
return $this->isExplicitActive($item['active']); |
|
50
|
48 |
|
} elseif (isset($item['href'])) { |
|
51
|
36 |
|
return $this->checkPattern($item['href']); |
|
52
|
12 |
|
} elseif (isset($item['url'])) { |
|
53
|
|
|
// Support URL for backwards compatibility. |
|
54
|
11 |
|
return $this->checkPattern($item['url']); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Checks if an array of items contains an active item. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $items The items to check |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
18 |
|
protected function containsActive($items) |
|
67
|
|
|
{ |
|
68
|
18 |
|
foreach ($items as $item) { |
|
69
|
15 |
|
if ($this->isActive($item)) { |
|
70
|
3 |
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
15 |
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Checks if an item is active by explicit definition of 'active' state. |
|
79
|
|
|
* |
|
80
|
|
|
* @param bool|array $activeDef |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
19 |
|
protected function isExplicitActive($activeDef) |
|
84
|
|
|
{ |
|
85
|
|
|
// If the active definition is a bool, return it. |
|
86
|
|
|
|
|
87
|
19 |
|
if (is_bool($activeDef)) { |
|
88
|
13 |
|
return $activeDef; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Otherwise, check if any of the url patterns that defines the active |
|
92
|
|
|
// state matches the requested url. |
|
93
|
|
|
|
|
94
|
6 |
|
foreach ($activeDef as $pattern) { |
|
95
|
6 |
|
if ($this->checkPattern($pattern)) { |
|
96
|
5 |
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
4 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Checks if an url pattern matches the requested url. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $pattern |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
51 |
|
protected function checkPattern($pattern) |
|
110
|
|
|
{ |
|
111
|
|
|
// First, check if the pattern is a regular expression. |
|
112
|
|
|
|
|
113
|
51 |
|
if (Str::startsWith($pattern, 'regex:')) { |
|
114
|
1 |
|
$regex = Str::substr($pattern, 6); |
|
115
|
1 |
|
return (bool) preg_match($regex, $this->request->path()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// If pattern is not a regex, check if the requested url matches the |
|
119
|
|
|
// absolute path to the given pattern. |
|
120
|
|
|
|
|
121
|
50 |
|
return Str::is($this->url->to($pattern), $this->request->url()); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|