1
|
|
|
<?php namespace Modules\Menu\Providers; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Config; |
4
|
|
|
use Illuminate\Support\ServiceProvider; |
5
|
|
|
use Modules\Menu\Entities\Menu; |
6
|
|
|
use Modules\Menu\Entities\Menuitem; |
7
|
|
|
use Modules\Menu\Repositories\Cache\CacheMenuDecorator; |
8
|
|
|
use Modules\Menu\Repositories\Cache\CacheMenuItemDecorator; |
9
|
|
|
use Modules\Menu\Repositories\Eloquent\EloquentMenuItemRepository; |
10
|
|
|
use Modules\Menu\Repositories\Eloquent\EloquentMenuRepository; |
11
|
|
|
use Modules\Page\Entities\Page; |
12
|
|
|
use Pingpong\Menus\MenuBuilder as Builder; |
13
|
|
|
use Pingpong\Menus\MenuFacade; |
14
|
|
|
use Pingpong\Menus\MenuItem as PingpongMenuItem; |
15
|
|
|
|
16
|
|
|
class MenuServiceProvider extends ServiceProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Indicates if loading of the provider is deferred. |
20
|
|
|
* |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $defer = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Register the service provider. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function register() |
31
|
|
|
{ |
32
|
|
|
$this->registerBindings(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Register all online menus on the Pingpong/Menu package |
37
|
|
|
*/ |
38
|
|
|
public function boot() |
39
|
|
|
{ |
40
|
|
|
$this->registerMenus(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the services provided by the provider. |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function provides() |
49
|
|
|
{ |
50
|
|
|
return array(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Register class binding |
55
|
|
|
*/ |
56
|
|
|
private function registerBindings() |
57
|
|
|
{ |
58
|
|
|
$this->app->bind( |
59
|
|
|
'Modules\Menu\Repositories\MenuRepository', |
60
|
|
|
function () { |
61
|
|
|
$repository = new EloquentMenuRepository(new Menu()); |
|
|
|
|
62
|
|
|
|
63
|
|
|
if (! config('app.cache')) { |
64
|
|
|
return $repository; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return new CacheMenuDecorator($repository); |
68
|
|
|
} |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$this->app->bind( |
72
|
|
|
'Modules\Menu\Repositories\MenuItemRepository', |
73
|
|
|
function () { |
74
|
|
|
$repository = new EloquentMenuItemRepository(new Menuitem()); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if (! config('app.cache')) { |
77
|
|
|
return $repository; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return new CacheMenuItemDecorator($repository); |
81
|
|
|
} |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add a menu item to the menu |
87
|
|
|
* @param Menuitem $item |
88
|
|
|
* @param Builder $menu |
89
|
|
|
*/ |
90
|
|
|
public function addItemToMenu(Menuitem $item, Builder $menu) |
91
|
|
|
{ |
92
|
|
|
if ($this->hasChildren($item)) { |
93
|
|
|
$this->addChildrenToMenu($item->title, $item->items, $menu, ['icon' => $item->icon, 'target' => $item->target]); |
|
|
|
|
94
|
|
View Code Duplication |
} else { |
|
|
|
|
95
|
|
|
$target = $this->getTarget($item); |
96
|
|
|
$menu->url( |
97
|
|
|
$target, |
98
|
|
|
$item->title, |
|
|
|
|
99
|
|
|
['target' => $item->target, |
|
|
|
|
100
|
|
|
'icon' => $item->icon] |
|
|
|
|
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Add children to menu under the give name |
107
|
|
|
* |
108
|
|
|
* @param string $name |
109
|
|
|
* @param object $children |
110
|
|
|
* @param Builder|MenuItem $menu |
111
|
|
|
*/ |
112
|
|
|
private function addChildrenToMenu($name, $children, $menu, $attribs = []) |
113
|
|
|
{ |
114
|
|
|
$menu->dropdown($name, function (PingpongMenuItem $subMenu) use ($children) { |
115
|
|
|
foreach ($children as $child) { |
116
|
|
|
$this->addSubItemToMenu($child, $subMenu); |
117
|
|
|
} |
118
|
|
|
}, 0, $attribs); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Add children to the given menu recursively |
123
|
|
|
* @param Menuitem $child |
124
|
|
|
* @param PingpongMenuItem $sub |
125
|
|
|
*/ |
126
|
|
|
private function addSubItemToMenu(Menuitem $child, PingpongMenuItem $sub) |
127
|
|
|
{ |
128
|
|
|
if ($this->hasChildren($child)) { |
129
|
|
|
$this->addChildrenToMenu($child->title, $child->items, $sub); |
|
|
|
|
130
|
|
View Code Duplication |
} else { |
|
|
|
|
131
|
|
|
$target = $this->getTarget($child); |
132
|
|
|
$sub->url($target, $child->title, 0, ['icon' => $child->icon, 'target' => $target]); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Check if the given menu item has children |
138
|
|
|
* |
139
|
|
|
* @param object $item |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
private function hasChildren($item) |
143
|
|
|
{ |
144
|
|
|
return $item->items->count() > 0; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Register the active menus |
149
|
|
|
*/ |
150
|
|
|
private function registerMenus() |
151
|
|
|
{ |
152
|
|
|
if (! $this->app['asgard.isInstalled']) { |
153
|
|
|
return; |
154
|
|
|
} |
155
|
|
|
$menu = $this->app->make('Modules\Menu\Repositories\MenuRepository'); |
156
|
|
|
$menuItem = $this->app->make('Modules\Menu\Repositories\MenuItemRepository'); |
157
|
|
|
foreach ($menu->allOnline() as $menu) { |
158
|
|
|
$menuTree = $menuItem->getTreeForMenu($menu->id); |
159
|
|
|
MenuFacade::create($menu->name, function (Builder $menu) use ($menuTree) { |
|
|
|
|
160
|
|
|
foreach ($menuTree as $menuItem) { |
161
|
|
|
$this->addItemToMenu($menuItem, $menu); |
162
|
|
|
} |
163
|
|
|
}); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get link target |
169
|
|
|
* |
170
|
|
|
* @param $item |
171
|
|
|
* @return mixed |
172
|
|
|
*/ |
173
|
|
|
private function getTarget($item) |
174
|
|
|
{ |
175
|
|
|
if (empty($item->url)) { |
176
|
|
|
$linkPathArray = array(); |
177
|
|
|
$parentItem = $item; |
178
|
|
|
|
179
|
|
|
$hasParentItem = !is_null($item->parent_id) ? true : false; |
180
|
|
|
|
181
|
|
|
while ($hasParentItem) { |
182
|
|
|
$parentItem = Menuitem::where('id', '=', $parentItem->parent_id)->first(); |
183
|
|
|
|
184
|
|
|
if ($parentItem->is_root != true) { |
185
|
|
|
if (!empty($parentItem->page_id)) { |
186
|
|
|
$page = Page::where('id', '=', $parentItem->page_id)->first()->translate($this->app->getLocale()); |
187
|
|
|
array_push($linkPathArray, $page->slug); |
188
|
|
|
} else { |
189
|
|
|
$parentUri = !is_null($parentItem->uri) ? $parentItem->uri . '/' . $linkPathArray : $linkPathArray; |
190
|
|
|
array_push($linkPathArray, $parentUri); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
$hasParentItem = !is_null($parentItem->parent_id) ? true : false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$linkPathArray = array_reverse($linkPathArray); |
197
|
|
|
if (!empty($item->page_id)) { |
198
|
|
|
$page = Page::where('id', '=', $item->page_id)->first()->translate($this->app->getLocale()); |
199
|
|
|
|
200
|
|
|
array_push($linkPathArray, $page->slug); |
201
|
|
|
} else { |
202
|
|
|
array_push($linkPathArray, $item->uri); |
203
|
|
|
} |
204
|
|
|
$parentLinkPath = implode('/', $linkPathArray); |
205
|
|
|
} else { |
206
|
|
|
$parentLinkPath = $item->url; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $parentLinkPath; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get parent item |
214
|
|
|
* |
215
|
|
|
* @param $item |
216
|
|
|
* @return mixed |
217
|
|
|
*/ |
218
|
|
|
/*private function getParentItem($item){ |
219
|
|
|
// \Debugbar::info($item->parent_id); |
220
|
|
|
//Menuitem::where('id','=',$item->parent_id); |
221
|
|
|
|
222
|
|
|
return $target; |
223
|
|
|
}*/ |
224
|
|
|
} |
225
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: