1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JeroenNoten\LaravelAdminLte\Menu; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
class Builder |
8
|
|
|
{ |
9
|
|
|
public $menu = []; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
21 |
|
private $filters; |
15
|
|
|
|
16
|
21 |
|
public function __construct(array $filters = []) |
17
|
21 |
|
{ |
18
|
|
|
$this->filters = $filters; |
19
|
21 |
|
} |
20
|
|
|
|
21
|
21 |
|
public function add() |
22
|
|
|
{ |
23
|
21 |
|
$items = $this->transformItems(func_get_args()); |
24
|
21 |
|
|
25
|
|
|
foreach ($items as $item) { |
26
|
21 |
|
array_push($this->menu, $item); |
27
|
|
|
} |
28
|
21 |
|
} |
29
|
|
|
|
30
|
21 |
|
public function addAfter($itemKey, ...$newItems) |
31
|
|
|
{ |
32
|
|
|
$this->addItem($itemKey, 'after', ...$newItems); |
33
|
21 |
|
} |
34
|
|
|
|
35
|
21 |
|
public function addBefore($itemKey, ...$newItems) |
36
|
1 |
|
{ |
37
|
|
|
$this->addItem($itemKey, 'before', ...$newItems); |
38
|
|
|
} |
39
|
20 |
|
|
40
|
19 |
|
public function addIn($itemKey, ...$newItems) |
41
|
|
|
{ |
42
|
|
|
$this->addItem($itemKey, 'in', ...$newItems); |
43
|
20 |
|
} |
44
|
2 |
|
|
45
|
|
|
public function remove($itemKey) |
46
|
|
|
{ |
47
|
20 |
|
$completeArrayPath = ''; |
48
|
|
|
$itemPath = $this->findItem($itemKey, $this->menu); |
49
|
|
|
if (is_array($itemPath)) { |
50
|
|
|
foreach ($itemPath as $key => $value) { |
51
|
|
|
if ($completeArrayPath === '') { |
52
|
|
|
$completeArrayPath .= "$value"; |
53
|
|
|
} else { |
54
|
|
|
$completeArrayPath .= ".submenu.$value"; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
$itemPath = $completeArrayPath; |
58
|
|
|
} |
59
|
|
|
Arr::forget($this->menu, $itemPath); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function itemKeyExists($itemKey) |
63
|
|
|
{ |
64
|
|
|
if ($this->findItem($itemKey, $this->menu)) { |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function transformItems($items) |
72
|
|
|
{ |
73
|
|
|
return array_filter(array_map([$this, 'applyFilters'], $items)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function addItem($itemKey, $direction, ...$newItems) |
77
|
|
|
{ |
78
|
|
|
$items = $this->transformItems($newItems); |
79
|
|
|
$position = $this->findItem($itemKey, $this->menu); |
80
|
|
|
|
81
|
|
|
if ($position !== null) { |
82
|
|
|
$completeArrayPath = $lastKey = ''; |
|
|
|
|
83
|
|
|
|
84
|
|
|
if (is_array($position)) { |
85
|
|
|
$completeArrayPath = implode('.submenu.', $position); |
86
|
|
|
$lastKey = end($position); |
87
|
|
|
} else { |
88
|
|
|
$completeArrayPath = $lastKey = $position; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($direction == 'in' || ! is_array($position)) { |
92
|
|
|
$arrayPath = $completeArrayPath; |
93
|
|
|
} elseif (is_array($position)) { |
94
|
|
|
$arrayPath = substr($completeArrayPath, 0, -(strlen(".$lastKey"))); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($position == $lastKey && $direction != 'in') { |
98
|
|
|
array_splice($this->menu, $position + ($direction == 'after' ? 1 : 0), 0, $items); |
99
|
|
|
} else { |
100
|
|
|
$menuItems = Arr::get($this->menu, $arrayPath); |
|
|
|
|
101
|
|
|
|
102
|
|
|
if ($direction == 'in') { |
103
|
|
|
if (! isset($menuItems['submenu'])) { |
104
|
|
|
$menuItems['submenu'] = []; |
105
|
|
|
$menuItems = $this->transformItems([$menuItems]); |
106
|
|
|
$menuItems = $menuItems[0]; |
107
|
|
|
$menuItems['submenu'] = []; |
108
|
|
|
$menuItems['submenu'] = array_merge($menuItems['submenu'], $items); |
109
|
|
|
} else { |
110
|
|
|
$menuItems['submenu'] = array_merge($menuItems['submenu'], $items); |
111
|
|
|
} |
112
|
|
|
} else { |
113
|
|
|
array_splice($menuItems, $lastKey + ($direction == 'after' ? 1 : 0), 0, $items); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
Arr::set($this->menu, $arrayPath, $menuItems); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function findItem($itemKey, $items, $childPositionOld = null) |
122
|
|
|
{ |
123
|
|
|
if (is_array($childPositionOld)) { |
124
|
|
|
$childPositions = $childPositionOld; |
125
|
|
|
} else { |
126
|
|
|
$childPositions = []; |
127
|
|
|
if ($childPositionOld) { |
128
|
|
|
$childPositions[] = $childPositionOld; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
foreach ($items as $key => $item) { |
132
|
|
|
if (isset($item['key']) && $item['key'] == $itemKey) { |
133
|
|
|
if ($childPositionOld) { |
134
|
|
|
$childPositions[] = $key; |
135
|
|
|
|
136
|
|
|
return $childPositions; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $key; |
140
|
|
|
} elseif (isset($item['submenu'])) { |
141
|
|
|
if ($childPositionOld) { |
142
|
|
|
$childPositions[] = $key; |
143
|
|
|
$childPosition = $this->findItem($itemKey, $item['submenu'], $childPositions); |
144
|
|
|
$childPositions[] = $childPosition; |
145
|
|
|
|
146
|
|
|
if (is_array($childPosition)) { |
147
|
|
|
$childPositions = $childPosition; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $childPositions; |
151
|
|
|
} else { |
152
|
|
|
$newKey = $this->findItem($itemKey, $item['submenu']); |
153
|
|
|
|
154
|
|
|
$childPositions[] = $key; |
155
|
|
|
if (! is_array($newKey)) { |
156
|
|
|
$childPositions[] = $newKey; |
157
|
|
|
} else { |
158
|
|
|
$childPositions = array_merge($childPositions, $newKey); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $childPositions; |
162
|
|
|
|
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
protected function applyFilters($item) |
169
|
|
|
{ |
170
|
|
|
if (is_string($item)) { |
171
|
|
|
return $item; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
foreach ($this->filters as $filter) { |
175
|
|
|
$item = $filter->transform($item, $this); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $item; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.