|
1
|
|
|
<?php |
|
2
|
|
|
namespace Malezha\Menu; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Contracts\Config\Repository; |
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
|
6
|
|
|
use Malezha\Menu\Contracts\Attributes as AttributesContract; |
|
7
|
|
|
use Malezha\Menu\Contracts\Builder as BuilderContract; |
|
8
|
|
|
use Malezha\Menu\Contracts\Element; |
|
9
|
|
|
use Malezha\Menu\Contracts\ElementFactory; |
|
10
|
|
|
use Malezha\Menu\Contracts\HasActiveAttributes; |
|
11
|
|
|
use Malezha\Menu\Contracts\MenuRender; |
|
12
|
|
|
use Malezha\Menu\Support\FromArrayBuilder; |
|
13
|
|
|
use Malezha\Menu\Traits\HasActiveAttributes as TraitHasActiveAttributes; |
|
14
|
|
|
use Malezha\Menu\Traits\HasAttributes; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Builder |
|
18
|
|
|
* @package Malezha\Menu |
|
19
|
|
|
*/ |
|
20
|
|
|
class Builder implements BuilderContract |
|
21
|
|
|
{ |
|
22
|
|
|
use HasAttributes, TraitHasActiveAttributes; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Container |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $app; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $elements; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $indexes = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $type; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var MenuRender |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $viewFactory; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $config; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $view = null; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritDoc |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(Container $container, AttributesContract $attributes, |
|
63
|
27 |
|
AttributesContract $activeAttributes, |
|
64
|
|
|
$type = self::UL, $view = null) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->app = $container; |
|
67
|
27 |
|
$this->type = $type; |
|
68
|
27 |
|
$this->attributes = $attributes; |
|
69
|
27 |
|
$this->elements = []; |
|
70
|
27 |
|
$this->activeAttributes = $activeAttributes; |
|
71
|
27 |
|
$this->viewFactory = $this->app->make(MenuRender::class); |
|
72
|
27 |
|
$this->config = $this->app->make(Repository::class)->get('menu'); |
|
73
|
27 |
|
try { |
|
74
|
|
|
$this->setView($view); |
|
75
|
27 |
|
} catch (\Exception $e) {} |
|
|
|
|
|
|
76
|
27 |
|
} |
|
77
|
27 |
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @inheritDoc |
|
80
|
|
|
*/ |
|
81
|
|
|
public function create($name, $type, callable $callback = null) |
|
82
|
21 |
|
{ |
|
83
|
|
|
if ($this->has($name)) { |
|
84
|
21 |
|
throw new \RuntimeException("Duplicate menu key \"${name}\""); |
|
85
|
1 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
$factory = $this->getFactory($type); |
|
88
|
|
|
|
|
89
|
|
|
if ($this->hasActiveAttributes($type)) { |
|
90
|
|
|
$factory->activeAttributes = clone $this->getActiveAttributes(); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$result = call_if_callable($callback, $factory); |
|
94
|
|
|
|
|
95
|
|
|
if (! $result instanceof Element) { |
|
96
|
|
|
throw new \RuntimeException("Result of callback must be [" . Element::class . "]"); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->saveItem($name, $result); |
|
100
|
|
|
|
|
101
|
|
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @inheritDoc |
|
106
|
|
|
*/ |
|
107
|
|
|
public function insertBefore($name, callable $callback) |
|
108
|
|
|
{ |
|
109
|
|
|
$prepared = $this->prepareInsert($name, $callback); |
|
|
|
|
|
|
110
|
|
|
$this->insert($this->indexes[$name], $prepared); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
/** |
|
114
|
1 |
|
* @inheritDoc |
|
115
|
1 |
|
*/ |
|
116
|
|
|
public function insertAfter($name, callable $callback) |
|
117
|
|
|
{ |
|
118
|
|
|
$prepared = $this->prepareInsert($name, $callback); |
|
|
|
|
|
|
119
|
|
|
$this->insert($this->indexes[$name] + 1, $prepared); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
/** |
|
123
|
1 |
|
* @inheritDoc |
|
124
|
1 |
|
*/ |
|
125
|
|
|
public function has($name) |
|
126
|
|
|
{ |
|
127
|
|
|
return array_key_exists($name, $this->elements); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
24 |
|
* @inheritDoc |
|
132
|
|
|
*/ |
|
133
|
|
|
public function get($name, $default = null) |
|
134
|
|
|
{ |
|
135
|
|
|
if ($this->has($name)) { |
|
136
|
|
|
return $this->elements[$name]; |
|
137
|
|
|
} |
|
138
|
|
|
return $default; |
|
139
|
3 |
|
} |
|
140
|
3 |
|
|
|
141
|
|
|
/** |
|
142
|
1 |
|
* @inheritDoc |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getByIndex($index, $default = null) |
|
145
|
|
|
{ |
|
146
|
|
|
$key = array_search($index, $this->indexes); |
|
147
|
|
|
|
|
148
|
|
|
return $key === false ? $default : $this->get($key, $default); |
|
149
|
|
|
} |
|
150
|
1 |
|
|
|
151
|
|
|
/** |
|
152
|
1 |
|
* @inheritDoc |
|
153
|
|
|
*/ |
|
154
|
|
|
public function all() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->elements; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
11 |
|
* @inheritDoc |
|
161
|
|
|
*/ |
|
162
|
|
|
public function forget($name) |
|
163
|
|
|
{ |
|
164
|
|
|
if ($this->has($name)) { |
|
165
|
|
|
unset($this->elements[$name]); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
1 |
|
|
|
169
|
1 |
|
/** |
|
170
|
|
|
* @inheritDoc |
|
171
|
1 |
|
*/ |
|
172
|
|
|
public function getType() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->type; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
10 |
|
* @inheritDoc |
|
179
|
|
|
*/ |
|
180
|
|
|
public function setType($type) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->type = (string) $type; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
1 |
|
* @inheritDoc |
|
187
|
1 |
|
*/ |
|
188
|
|
|
public function render($renderView = null) |
|
189
|
|
|
{ |
|
190
|
|
|
$view = $this->getRenderView($renderView); |
|
191
|
|
|
|
|
192
|
|
|
$minify = $this->config['minify']; |
|
193
|
|
|
|
|
194
|
9 |
|
$rendered = $this->viewFactory->make($view)->with([ |
|
195
|
|
|
'menu' => $this, |
|
196
|
9 |
|
'renderView' => $renderView, |
|
197
|
|
|
])->render(); |
|
198
|
9 |
|
|
|
199
|
9 |
|
if ($minify) { |
|
200
|
9 |
|
$rendered = $this->minifyHtmlOutput($rendered); |
|
201
|
9 |
|
} |
|
202
|
|
|
|
|
203
|
9 |
|
return $rendered; |
|
204
|
9 |
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
9 |
|
* @inheritDoc |
|
208
|
|
|
*/ |
|
209
|
|
|
public function getView() |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->view; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
2 |
|
* @inheritDoc |
|
216
|
|
|
*/ |
|
217
|
|
|
public function setView($view) |
|
218
|
|
|
{ |
|
219
|
|
|
if (!$this->viewFactory->exists($view)) { |
|
220
|
|
|
throw new \Exception('View not found'); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
27 |
|
$this->view = $view; |
|
224
|
27 |
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
3 |
|
* Minify html |
|
228
|
3 |
|
* |
|
229
|
|
|
* @param string $html |
|
230
|
|
|
* @return string |
|
231
|
|
|
*/ |
|
232
|
|
|
protected function minifyHtmlOutput($html) |
|
233
|
|
|
{ |
|
234
|
|
|
$search = array( |
|
235
|
|
|
'/\>[^\S]+/s', // strip whitespaces after tags, except space |
|
236
|
|
|
'/[^\S]+\</s', // strip whitespaces before tags, except space |
|
237
|
|
|
'/(\s)+/s' // shorten multiple whitespace sequences |
|
238
|
|
|
); |
|
239
|
9 |
|
|
|
240
|
|
|
$replace = array( |
|
241
|
|
|
'>', |
|
242
|
|
|
'<', |
|
243
|
|
|
'\\1' |
|
244
|
|
|
); |
|
245
|
9 |
|
|
|
246
|
|
|
return preg_replace($search, $replace, $html); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
9 |
|
* Get view for render |
|
251
|
|
|
* |
|
252
|
|
|
* @param string $view |
|
253
|
|
|
* @return string |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function getRenderView($view = null) |
|
256
|
|
|
{ |
|
257
|
|
|
$renderView = $this->config['view']; |
|
258
|
|
|
|
|
259
|
|
|
if (!empty($this->view)) { |
|
260
|
|
|
$renderView = $this->view; |
|
261
|
9 |
|
} |
|
262
|
|
|
|
|
263
|
9 |
|
if (!empty($view) && $this->viewFactory->exists($view)) { |
|
264
|
3 |
|
$renderView = $view; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
9 |
|
return $renderView; |
|
268
|
3 |
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
9 |
|
* @param string $name |
|
272
|
|
|
* @param Element $item |
|
273
|
|
|
*/ |
|
274
|
|
|
protected function saveItem($name, $item) |
|
275
|
|
|
{ |
|
276
|
|
|
$this->elements[$name] = $item; |
|
277
|
|
|
$this->indexes[$name] = count($this->elements) - 1; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
20 |
|
/** |
|
281
|
20 |
|
* @param string $name |
|
282
|
20 |
|
* @param array $attributes |
|
283
|
|
|
* @param array $activeAttributes |
|
284
|
|
|
* @param \Closure|null $callback |
|
285
|
|
|
* @return BuilderContract |
|
286
|
|
|
*/ |
|
287
|
|
|
protected function builderFactory($name, $attributes = [], $activeAttributes = [], $callback = null) |
|
288
|
|
|
{ |
|
289
|
|
|
$menu = $this->app->makeWith(BuilderContract::class, [ |
|
|
|
|
|
|
290
|
|
|
'container' => $this->app, |
|
291
|
|
|
'name' => $name, |
|
292
|
|
|
'activeAttributes' => $this->app->makeWith(AttributesContract::class, |
|
|
|
|
|
|
293
|
2 |
|
['attributes' => $activeAttributes]), |
|
294
|
2 |
|
'attributes' => $this->app->makeWith(AttributesContract::class, |
|
|
|
|
|
|
295
|
2 |
|
['attributes' => $attributes]), |
|
296
|
2 |
|
'view' => $this->getView(), |
|
297
|
2 |
|
]); |
|
298
|
2 |
|
|
|
299
|
|
|
if (is_callable($callback)) { |
|
300
|
|
|
call_user_func($callback, $menu); |
|
301
|
2 |
|
} |
|
302
|
2 |
|
|
|
303
|
|
|
return $menu; |
|
304
|
|
|
} |
|
305
|
2 |
|
|
|
306
|
|
|
protected function rebuildIndexesArray() |
|
307
|
|
|
{ |
|
308
|
|
|
$this->indexes = []; |
|
309
|
|
|
$iterator = 0; |
|
310
|
2 |
|
|
|
311
|
2 |
|
foreach ($this->elements as $key => $value) { |
|
312
|
|
|
$this->indexes[$key] = $iterator++; |
|
313
|
2 |
|
} |
|
314
|
2 |
|
} |
|
315
|
|
|
|
|
316
|
2 |
|
/** |
|
317
|
|
|
* @param string $name |
|
318
|
|
|
* @param \Closure $callback |
|
319
|
|
|
* @return array |
|
320
|
|
|
*/ |
|
321
|
|
|
protected function prepareInsert($name, $callback) |
|
322
|
|
|
{ |
|
323
|
|
|
if (!$this->has($name)) { |
|
324
|
|
|
throw new \RuntimeException("Menu item \"${name}\" must be exists"); |
|
325
|
3 |
|
} |
|
326
|
1 |
|
|
|
327
|
|
|
$forInsert = $this->builderFactory('tmp', [], [], $callback)->all(); |
|
328
|
|
|
$diff = array_intersect_key($this->elements, $forInsert); |
|
329
|
2 |
|
|
|
330
|
2 |
|
if (count($diff) > 0) { |
|
331
|
|
|
throw new \RuntimeException('Duplicated keys: ' . implode(', ', array_keys($diff))); |
|
332
|
2 |
|
} |
|
333
|
1 |
|
|
|
334
|
|
|
// foreach ($forInsert as &$item) { |
|
|
|
|
|
|
335
|
|
|
// if ($item instanceof ElementFactory) { |
|
336
|
1 |
|
// $item = $item->build(); |
|
337
|
1 |
|
// } |
|
338
|
1 |
|
// } |
|
339
|
|
|
|
|
340
|
|
|
return $forInsert; |
|
341
|
|
|
} |
|
342
|
1 |
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* @param int $position |
|
345
|
|
|
* @param array $values |
|
346
|
|
|
*/ |
|
347
|
|
|
protected function insert($position, $values) |
|
348
|
|
|
{ |
|
349
|
|
|
$firstArray = array_splice($this->elements, 0, $position); |
|
350
|
|
|
$this->elements = array_merge($firstArray, $values, $this->elements); |
|
351
|
1 |
|
$this->rebuildIndexesArray(); |
|
352
|
1 |
|
} |
|
353
|
1 |
|
|
|
354
|
1 |
|
/** |
|
355
|
|
|
* @param $element |
|
356
|
|
|
* @return ElementFactory |
|
357
|
|
|
* @throws \RuntimeException |
|
358
|
|
|
*/ |
|
359
|
|
|
protected function getFactory($element) |
|
360
|
|
|
{ |
|
361
|
|
|
if (!array_key_exists($element, $this->config['elements'])) { |
|
362
|
21 |
|
throw new \RuntimeException('Not found factory for element:' . $element); |
|
363
|
|
|
} |
|
364
|
21 |
|
|
|
365
|
|
|
$factoryClass = $this->config['elements'][$element]['factory']; |
|
366
|
|
|
|
|
367
|
|
|
return $this->app->make($factoryClass); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* @inheritDoc |
|
372
|
2 |
|
*/ |
|
373
|
2 |
|
public function toArray() |
|
374
|
|
|
{ |
|
375
|
2 |
|
$this->view = $this->getRenderView($this->view); |
|
376
|
2 |
|
$elements = []; |
|
377
|
2 |
|
|
|
378
|
|
|
foreach ($this->elements as $key => $element) { |
|
379
|
|
|
// if ($element instanceof ElementFactory) { |
|
|
|
|
|
|
380
|
2 |
|
// $element = $element->build(); |
|
381
|
2 |
|
// } |
|
382
|
|
|
|
|
383
|
|
|
$elements[$key] = $element->toArray(); |
|
384
|
|
|
$elements[$key]['type'] = array_search(get_class($element), $this->config['alias']); |
|
385
|
2 |
|
} |
|
386
|
2 |
|
|
|
387
|
2 |
|
return [ |
|
388
|
2 |
|
'type' => $this->type, |
|
389
|
2 |
|
'view' => $this->view, |
|
390
|
|
|
'attributes' => $this->attributes->toArray(), |
|
391
|
|
|
'activeAttributes' => $this->activeAttributes->toArray(), |
|
392
|
|
|
'elements' => $elements, |
|
393
|
|
|
]; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* @inheritDoc |
|
398
|
1 |
|
*/ |
|
399
|
|
|
public function offsetExists($offset) |
|
400
|
|
|
{ |
|
401
|
1 |
|
if (is_int($offset)) { |
|
402
|
|
|
return (bool) array_search($offset, $this->indexes, true); |
|
403
|
|
|
} |
|
404
|
|
|
return $this->has($offset); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* @inheritDoc |
|
409
|
2 |
|
*/ |
|
410
|
1 |
|
public function offsetGet($offset) |
|
411
|
1 |
|
{ |
|
412
|
1 |
|
if (is_int($offset)) { |
|
413
|
|
|
$offset = array_search($offset, $this->indexes, true); |
|
414
|
|
|
if ($offset === false) { |
|
415
|
2 |
|
return null; |
|
416
|
|
|
} |
|
417
|
|
|
} |
|
418
|
|
|
return $this->get($offset); |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* @inheritDoc |
|
423
|
1 |
|
*/ |
|
424
|
1 |
|
public function offsetSet($offset, $value) |
|
425
|
|
|
{ |
|
426
|
1 |
|
if ($value instanceof Element) { |
|
427
|
|
|
$this->elements[$offset] = $value; |
|
428
|
|
|
} |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
/** |
|
432
|
|
|
* @inheritDoc |
|
433
|
1 |
|
*/ |
|
434
|
1 |
|
public function offsetUnset($offset) |
|
435
|
|
|
{ |
|
436
|
|
|
$this->forget($offset); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* @inheritDoc |
|
441
|
1 |
|
*/ |
|
442
|
1 |
|
static public function fromArray(array $builder) |
|
|
|
|
|
|
443
|
1 |
|
{ |
|
444
|
1 |
|
return FromArrayBuilder::getInstance()->build($builder); |
|
445
|
1 |
|
} |
|
446
|
1 |
|
|
|
447
|
|
|
/** |
|
448
|
|
|
* @param string $type |
|
449
|
|
|
* @return bool |
|
450
|
|
|
*/ |
|
451
|
|
|
protected function hasActiveAttributes($type) |
|
452
|
|
|
{ |
|
453
|
|
|
$reflection = new \ReflectionClass($type); |
|
454
|
|
|
|
|
455
|
1 |
|
return $reflection->implementsInterface(HasActiveAttributes::class); |
|
456
|
|
|
} |
|
457
|
|
|
} |