1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Malezha\Menu; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
6
|
|
|
use Malezha\Menu\Contracts\Attributes; |
7
|
|
|
use Malezha\Menu\Contracts\Menu as MenuContract; |
8
|
|
|
use Malezha\Menu\Contracts\Builder; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Menu |
12
|
|
|
* @package Malezha\Menu |
13
|
|
|
*/ |
14
|
|
|
class Menu implements MenuContract |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $menuList = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Container |
23
|
|
|
*/ |
24
|
|
|
protected $container; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Menu constructor. |
28
|
|
|
* |
29
|
|
|
* @param Container $container |
30
|
|
|
*/ |
31
|
6 |
|
public function __construct(Container $container) |
32
|
|
|
{ |
33
|
6 |
|
$this->container = $container; |
34
|
6 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Make new global menu |
38
|
|
|
* |
39
|
|
|
* @param string $name |
40
|
|
|
* @param \Closure $callback |
41
|
|
|
* @param string $type |
42
|
|
|
* @param array $attributes |
43
|
|
|
* @param array $activeAttributes |
44
|
|
|
* @return Builder |
45
|
|
|
*/ |
46
|
4 |
|
public function make($name, \Closure $callback, $type = Builder::UL, $attributes = [], $activeAttributes = []) |
47
|
|
|
{ |
48
|
4 |
|
$menu = $this->container->make(Builder::class, [ |
49
|
4 |
|
'container' => $this->container, |
50
|
4 |
|
'name' => $name, |
51
|
4 |
|
'type' => $type, |
52
|
4 |
|
'attributes' => $this->container->make(Attributes::class, ['attributes' => $attributes]), |
53
|
4 |
|
'activeAttributes' => $this->container->make(Attributes::class, ['attributes' => $activeAttributes]), |
54
|
4 |
|
]); |
55
|
4 |
|
call_user_func($callback, $menu); |
56
|
4 |
|
$this->menuList[$name] = $menu; |
57
|
|
|
|
58
|
4 |
|
return $menu; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get global menu |
63
|
|
|
* |
64
|
|
|
* @param string $name |
65
|
|
|
* @return Builder |
66
|
|
|
* @throws \RuntimeException |
67
|
|
|
*/ |
68
|
3 |
|
public function get($name) |
69
|
|
|
{ |
70
|
3 |
|
if (array_key_exists($name, $this->menuList) && ($menu = $this->menuList[$name]) instanceof Builder) { |
71
|
2 |
|
return $menu; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
throw new \RuntimeException('Menu not found'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Render global menu to html |
79
|
|
|
* |
80
|
|
|
* @param string $name |
81
|
|
|
* @param null|string $view |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
1 |
|
public function render($name, $view = null) |
85
|
|
|
{ |
86
|
1 |
|
return $this->get($name)->render($view); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check exits global menu by name |
91
|
|
|
* |
92
|
|
|
* @param string $name |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
2 |
|
public function has($name) |
96
|
|
|
{ |
97
|
2 |
|
return array_key_exists($name, $this->menuList); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Delete menu from global list |
102
|
|
|
* |
103
|
|
|
* @param string $name |
104
|
|
|
*/ |
105
|
1 |
|
public function forget($name) |
106
|
|
|
{ |
107
|
1 |
|
if ($this->has($name)) { |
108
|
1 |
|
unset($this->menuList[$name]); |
109
|
1 |
|
} |
110
|
|
|
} |
111
|
|
|
} |