1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Menus\Models; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Countable; |
9
|
|
|
use ReflectionFunction; |
10
|
|
|
use Illuminate\Routing\Router; |
11
|
|
|
use Illuminate\Support\Facades\Route; |
12
|
|
|
use Rinvex\Menus\Models\MenuGenerator; |
13
|
|
|
use Illuminate\Contracts\View\Factory as ViewFactory; |
14
|
|
|
|
15
|
|
|
class MenuManager implements Countable |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The menus collection. |
19
|
|
|
* |
20
|
|
|
* @var \Illuminate\Support\Collection |
21
|
|
|
*/ |
22
|
|
|
protected $menus; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The view factory. |
26
|
|
|
* |
27
|
|
|
* @var \Illuminate\Contracts\View\Factory |
28
|
|
|
*/ |
29
|
|
|
protected $viewFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The router instance. |
33
|
|
|
* |
34
|
|
|
* @var \Illuminate\Routing\Router |
35
|
|
|
*/ |
36
|
|
|
protected $router; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The menu factory. |
40
|
|
|
* |
41
|
|
|
* @var \Rinvex\Menus\Models\MenuGenerator |
42
|
|
|
*/ |
43
|
|
|
protected $generator; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The menu callbacks. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $callbacks = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The constructor. |
54
|
|
|
* |
55
|
|
|
* @param \Illuminate\Contracts\View\Factory $viewFactory |
56
|
|
|
* @param \Illuminate\Routing\Router $router |
57
|
|
|
*/ |
58
|
|
|
public function __construct(ViewFactory $viewFactory, Router $router) |
59
|
|
|
{ |
60
|
|
|
$this->viewFactory = $viewFactory; |
61
|
|
|
$this->callbacks = collect(); |
|
|
|
|
62
|
|
|
$this->menus = collect(); |
63
|
|
|
$this->router = $router; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check if the menu exists. |
68
|
|
|
* |
69
|
|
|
* @param string $name |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function has($name): bool |
74
|
|
|
{ |
75
|
|
|
return $this->menus->has($name); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get instance of the given menu if exists. |
80
|
|
|
* |
81
|
|
|
* @param string $name |
82
|
|
|
* |
83
|
|
|
* @return \Rinvex\Menus\Models\MenuGenerator|null |
84
|
|
|
*/ |
85
|
|
|
public function instance($name): ?MenuGenerator |
86
|
|
|
{ |
87
|
|
|
return $this->menus->get($name); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Register a menu. |
92
|
|
|
* |
93
|
|
|
* @param string $name |
94
|
|
|
* @param \Closure $callback |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function register($name, Closure $callback): void |
99
|
|
|
{ |
100
|
|
|
if ($this->has($name)) { |
101
|
|
|
$this->callbacks = $this->callbacks->put($name, $this->callbacks->get($name, collect())->push($callback)); |
|
|
|
|
102
|
|
|
} else { |
103
|
|
|
$builder = new MenuGenerator(); |
104
|
|
|
|
105
|
|
|
$builder->setViewFactory($this->viewFactory); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$this->menus->put($name, $builder); |
108
|
|
|
|
109
|
|
|
$this->callbacks = $this->callbacks->put($name, $this->callbacks->get($name, collect())->push($callback)); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Render the menu tag by given name. |
115
|
|
|
* |
116
|
|
|
* @param string $name |
117
|
|
|
* @param string $presenter |
|
|
|
|
118
|
|
|
* @param array $bindings |
119
|
|
|
* @param bool $specialSidebar |
120
|
|
|
* |
121
|
|
|
* @return string|null |
122
|
|
|
*/ |
123
|
|
|
public function render(string $name, string $presenter = null, array $bindings = [], bool $specialSidebar = false): ?string |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
if ($this->has($name)) { |
126
|
|
|
$instance = $this->instance($name); |
127
|
|
|
|
128
|
|
|
$this->callbacks->get($name)->each(function ($callback) use ($instance) { |
|
|
|
|
129
|
|
|
$reflectionParams = collect((new ReflectionFunction($callback))->getParameters()); |
130
|
|
|
$reflectionParams->shift(); |
131
|
|
|
|
132
|
|
|
collect($reflectionParams)->each(function ($param) use (&$params) { |
133
|
|
|
$params[] = Route::current()->hasParameter($param->getName()) ? Route::current()->parameter($param->getName()) |
|
|
|
|
134
|
|
|
: (class_exists($param->getClass()->getName()) ? app($param->getClass()->getName()) : null); |
135
|
|
|
}); |
136
|
|
|
|
137
|
|
|
$params ? $callback($instance, ...$params) : $callback($instance); |
138
|
|
|
}); |
139
|
|
|
|
140
|
|
|
return $instance->setBindings($bindings)->render($presenter, $specialSidebar); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get all menus. |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function all(): array |
152
|
|
|
{ |
153
|
|
|
return $this->menus->toArray(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get count from all menus. |
158
|
|
|
* |
159
|
|
|
* @return int |
160
|
|
|
*/ |
161
|
|
|
public function count(): int |
162
|
|
|
{ |
163
|
|
|
return $this->menus->count(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Empty the current menus. |
168
|
|
|
*/ |
169
|
|
|
public function destroy() |
170
|
|
|
{ |
171
|
|
|
$this->menus = collect(); |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..