|
1
|
|
|
<?php |
|
2
|
|
|
namespace Malezha\Menu\Entity; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Contracts\Config\Repository; |
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
|
6
|
|
|
use Illuminate\Contracts\View\Factory as ViewFactory; |
|
7
|
|
|
use Malezha\Menu\Contracts\Attributes as AttributesContract; |
|
8
|
|
|
use Malezha\Menu\Contracts\Builder as BuilderContract; |
|
9
|
|
|
use Malezha\Menu\Contracts\Group; |
|
|
|
|
|
|
10
|
|
|
use Malezha\Menu\Contracts\HasAttributes as HasAttributesContract; |
|
11
|
|
|
use Malezha\Menu\Contracts\Item; |
|
|
|
|
|
|
12
|
|
|
use Malezha\Menu\Contracts\Link; |
|
|
|
|
|
|
13
|
|
|
use Malezha\Menu\Traits\HasAttributes; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Builder |
|
17
|
|
|
* @package Malezha\Menu\Entity |
|
18
|
|
|
*/ |
|
19
|
|
|
class Builder implements BuilderContract |
|
20
|
|
|
{ |
|
21
|
|
|
use HasAttributes; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Container |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $container; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $items; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $name; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $type; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var AttributesContract |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $activeAttributes; |
|
47
|
14 |
|
|
|
48
|
|
|
/** |
|
49
|
14 |
|
* @param Container $container |
|
50
|
14 |
|
* @param string $name |
|
51
|
14 |
|
* @param string $type |
|
52
|
14 |
|
* @param array $attributes |
|
53
|
14 |
|
* @param array $activeAttributes |
|
54
|
14 |
|
*/ |
|
55
|
14 |
|
public function __construct(Container $container, $name, $type = self::UL, $attributes = [], $activeAttributes = []) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->container = $container; |
|
58
|
|
|
$this->name = $name; |
|
59
|
|
|
$this->type = $type; |
|
60
|
|
|
$this->attributes = $this->container->make(AttributesContract::class, ['attributes' => $attributes]); |
|
61
|
|
|
$this->items = []; |
|
62
|
|
|
$this->activeAttributes = $this->container->make(AttributesContract::class, ['attributes' => $activeAttributes]); |
|
63
|
2 |
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
/** |
|
66
|
2 |
|
* @param string $name |
|
67
|
|
|
* @param \Closure $itemCallable |
|
68
|
2 |
|
* @param \Closure $menuCallable |
|
69
|
2 |
|
* @return Group |
|
70
|
2 |
|
*/ |
|
71
|
2 |
|
public function group($name, \Closure $itemCallable, \Closure $menuCallable) |
|
72
|
2 |
|
{ |
|
73
|
2 |
|
$item = $this->container->make(Item::class, [ |
|
74
|
|
|
'builder' => $this, |
|
75
|
2 |
|
'attributes' => $this->container->make(AttributesContract::class, ['attributes' => []]), |
|
76
|
2 |
|
'link' => $this->container->make(Link::class, [ |
|
77
|
|
|
'title' => $name, |
|
78
|
2 |
|
'attributes' => $this->container->make(AttributesContract::class, ['attributes' => []]), |
|
79
|
|
|
]), |
|
80
|
|
|
'request' => $this->container->make('request'), |
|
81
|
|
|
]); |
|
82
|
|
|
call_user_func($itemCallable, $item); |
|
83
|
|
|
|
|
84
|
|
|
$menu = $this->container->make(BuilderContract::class, [ |
|
85
|
|
|
'container' => $this->container, |
|
86
|
|
|
'name' => $name, |
|
87
|
|
|
'activeAttributes' => $this->activeAttributes()->all(), |
|
88
|
|
|
]); |
|
89
|
|
|
call_user_func($menuCallable, $menu); |
|
90
|
6 |
|
|
|
91
|
|
|
$group = $this->container->make(Group::class, [ |
|
92
|
6 |
|
'menu' => $menu, |
|
93
|
|
|
'item' => $item, |
|
94
|
6 |
|
]); |
|
95
|
2 |
|
$this->items[$name] = $group; |
|
96
|
2 |
|
|
|
97
|
|
|
return $group; |
|
98
|
6 |
|
} |
|
99
|
|
|
|
|
100
|
6 |
|
/** |
|
101
|
|
|
* @param string $name |
|
102
|
|
|
* @param string $title |
|
103
|
|
|
* @param string $url |
|
104
|
|
|
* @param array $attributes |
|
105
|
|
|
* @param array $linkAttributes |
|
106
|
|
|
* @param \Closure|null $callback |
|
107
|
4 |
|
* @return Item |
|
108
|
|
|
*/ |
|
109
|
4 |
|
public function add($name, $title, $url, $attributes = [], $linkAttributes = [], $callback = null) |
|
110
|
|
|
{ |
|
111
|
|
|
$link = $this->container->make(Link::class, [ |
|
112
|
|
|
'title' => $title, |
|
113
|
|
|
'url' => $url, |
|
114
|
|
|
'attributes' => $this->container->make(AttributesContract::class, ['attributes' => $linkAttributes]), |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
2 |
|
$item = $this->container->make(Item::class, [ |
|
118
|
|
|
'builder' => $this, |
|
119
|
2 |
|
'attributes' => $this->container->make(AttributesContract::class, ['attributes' => $attributes]), |
|
120
|
2 |
|
'link' => $link, |
|
121
|
|
|
'request' => $this->container->make('request'), |
|
122
|
|
|
]); |
|
123
|
|
|
|
|
124
|
|
|
if (is_callable($callback)) { |
|
125
|
|
|
call_user_func($callback, $item); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
3 |
|
$this->items[$name] = $item; |
|
129
|
|
|
|
|
130
|
3 |
|
return $item; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $name |
|
135
|
|
|
* @return bool |
|
136
|
1 |
|
*/ |
|
137
|
|
|
public function has($name) |
|
138
|
1 |
|
{ |
|
139
|
1 |
|
return array_key_exists($name, $this->items); |
|
140
|
1 |
|
} |
|
141
|
1 |
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $name |
|
144
|
|
|
* @param mixed|null $default |
|
145
|
|
|
* @return Item|Group|null |
|
146
|
3 |
|
*/ |
|
147
|
|
|
public function get($name, $default = null) |
|
148
|
3 |
|
{ |
|
149
|
|
|
if ($this->has($name)) { |
|
150
|
|
|
return $this->items[$name]; |
|
151
|
|
|
} |
|
152
|
|
|
return $default; |
|
153
|
|
|
} |
|
154
|
1 |
|
|
|
155
|
|
|
/** |
|
156
|
1 |
|
* @return array |
|
157
|
1 |
|
*/ |
|
158
|
|
|
public function all() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->items; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
2 |
|
/** |
|
164
|
|
|
* @param string $name |
|
165
|
2 |
|
*/ |
|
166
|
2 |
|
public function forget($name) |
|
167
|
|
|
{ |
|
168
|
|
|
if ($this->has($name)) { |
|
169
|
2 |
|
unset($this->items[$name]); |
|
170
|
|
|
} |
|
171
|
2 |
|
} |
|
172
|
2 |
|
|
|
173
|
2 |
|
/** |
|
174
|
2 |
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
2 |
|
public function getType() |
|
177
|
2 |
|
{ |
|
178
|
2 |
|
return $this->type; |
|
179
|
|
|
} |
|
180
|
2 |
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param string $type |
|
183
|
|
|
*/ |
|
184
|
|
|
public function setType($type) |
|
185
|
|
|
{ |
|
186
|
|
|
$this->type = (string)$type; |
|
187
|
5 |
|
} |
|
188
|
|
|
|
|
189
|
5 |
|
/** |
|
190
|
1 |
|
* @param string|null $view |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
5 |
|
public function render($view = null) |
|
194
|
|
|
{ |
|
195
|
|
|
/** @var Repository $config */ |
|
196
|
2 |
|
$config = $this->container->make('config'); |
|
197
|
|
|
|
|
198
|
|
|
$view = (empty($view)) ? $config->get('menu.view') : $view; |
|
199
|
2 |
|
$minify = $config->get('menu.minify', false); |
|
200
|
2 |
|
|
|
201
|
|
|
/* @var ViewFactory $viewFactory */ |
|
202
|
2 |
|
$viewFactory = $this->container->make(ViewFactory::class); |
|
203
|
|
|
|
|
204
|
|
|
$rendered = $viewFactory->make($view, [ |
|
205
|
2 |
|
'menu' => $this, |
|
206
|
2 |
|
'renderView' => $view, |
|
207
|
|
|
])->render(); |
|
208
|
2 |
|
|
|
209
|
|
|
if ($minify) { |
|
210
|
2 |
|
$rendered = $this->minifyHtmlOutput($rendered); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return $rendered; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param \Closure|null $callback |
|
218
|
|
|
* @return Attributes|mixed |
|
219
|
|
|
*/ |
|
220
|
|
|
public function activeAttributes($callback = null) |
|
221
|
|
|
{ |
|
222
|
|
|
if (is_callable($callback)) { |
|
223
|
|
|
return call_user_func($callback, $this->activeAttributes); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return $this->activeAttributes; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param $html |
|
231
|
|
|
* @return mixed |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function minifyHtmlOutput($html) |
|
234
|
|
|
{ |
|
235
|
|
|
$search = array( |
|
236
|
|
|
'/\>[^\S]+/s', // strip whitespaces after tags, except space |
|
237
|
|
|
'/[^\S]+\</s', // strip whitespaces before tags, except space |
|
238
|
|
|
'/(\s)+/s' // shorten multiple whitespace sequences |
|
239
|
|
|
); |
|
240
|
|
|
|
|
241
|
|
|
$replace = array( |
|
242
|
|
|
'>', |
|
243
|
|
|
'<', |
|
244
|
|
|
'\\1' |
|
245
|
|
|
); |
|
246
|
|
|
|
|
247
|
|
|
return preg_replace($search, $replace, $html); |
|
248
|
|
|
} |
|
249
|
|
|
} |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: