|
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 Illuminate\Contracts\View\Factory as ViewFactory; |
|
13
|
|
|
|
|
14
|
|
|
class MenuManager implements Countable |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* The menus collection. |
|
18
|
|
|
* |
|
19
|
|
|
* @var \Illuminate\Support\Collection |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $menus; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The view factory. |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Illuminate\Contracts\View\Factory |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $viewFactory; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The router instance. |
|
32
|
|
|
* |
|
33
|
|
|
* @var \Illuminate\Routing\Router |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $router; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The menu factory. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \Rinvex\Menus\Models\MenuGenerator |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $generator; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The menu callbacks. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $callbacks = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \Illuminate\Contracts\View\Factory $viewFactory |
|
55
|
|
|
* @param \Illuminate\Routing\Router $router |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(ViewFactory $viewFactory, Router $router) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->viewFactory = $viewFactory; |
|
60
|
|
|
$this->callbacks = collect(); |
|
|
|
|
|
|
61
|
|
|
$this->menus = collect(); |
|
62
|
|
|
$this->router = $router; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Check if the menu exists. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $name |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function has($name): bool |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->menus->has($name); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get instance of the given menu if exists. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $name |
|
81
|
|
|
* |
|
82
|
|
|
* @return \Rinvex\Menus\Models\MenuGenerator|null |
|
83
|
|
|
*/ |
|
84
|
|
|
public function instance($name): ?MenuGenerator |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->menus->get($name); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Register a menu. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* @param \Closure $callback |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
public function register($name, Closure $callback): void |
|
98
|
|
|
{ |
|
99
|
|
|
if ($this->has($name)) { |
|
100
|
|
|
$this->callbacks = $this->callbacks->put($name, $this->callbacks->get($name, collect())->push($callback)); |
|
|
|
|
|
|
101
|
|
|
} else { |
|
102
|
|
|
$builder = new MenuGenerator(); |
|
103
|
|
|
|
|
104
|
|
|
$builder->setViewFactory($this->viewFactory); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$this->menus->put($name, $builder); |
|
107
|
|
|
|
|
108
|
|
|
$this->callbacks = $this->callbacks->put($name, $this->callbacks->get($name, collect())->push($callback)); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Render the menu tag by given name. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $name |
|
116
|
|
|
* @param string $presenter |
|
|
|
|
|
|
117
|
|
|
* @param array $bindings |
|
118
|
|
|
* @param bool $specialSidebar |
|
119
|
|
|
* |
|
120
|
|
|
* @return string|null |
|
121
|
|
|
*/ |
|
122
|
|
|
public function render(string $name, string $presenter = null, array $bindings = [], bool $specialSidebar = false): ?string |
|
123
|
|
|
{ |
|
124
|
|
|
if ($this->has($name)) { |
|
125
|
|
|
$instance = $this->instance($name); |
|
126
|
|
|
|
|
127
|
|
|
$this->callbacks->get($name)->each(function ($callback) use ($instance) { |
|
|
|
|
|
|
128
|
|
|
$reflectionParams = collect((new ReflectionFunction($callback))->getParameters()); |
|
129
|
|
|
$reflectionParams->shift(); |
|
130
|
|
|
|
|
131
|
|
|
collect($reflectionParams)->each(function ($param) use (&$params) { |
|
132
|
|
|
$name = $param->getType()->getName(); |
|
133
|
|
|
$params[] = Route::current()->hasParameter($param->getName()) ? Route::current()->parameter($param->getName()) |
|
134
|
|
|
: ((app()->bound($name) ?: class_exists($name) ?: interface_exists($name)) ? app($name) : 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..