|
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 Pingpong\Menus\MenuBuilder as Builder; |
|
12
|
|
|
use Pingpong\Menus\MenuFacade; |
|
13
|
|
|
use Pingpong\Menus\MenuItem as PingpongMenuItem; |
|
14
|
|
|
|
|
15
|
|
|
class MenuServiceProvider extends ServiceProvider |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Indicates if loading of the provider is deferred. |
|
19
|
|
|
* |
|
20
|
|
|
* @var bool |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $defer = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Register the service provider. |
|
26
|
|
|
* |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
|
|
public function register() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->registerBindings(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Register all online menus on the Pingpong/Menu package |
|
36
|
|
|
*/ |
|
37
|
|
|
public function boot() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->registerMenus(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the services provided by the provider. |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function provides() |
|
48
|
|
|
{ |
|
49
|
|
|
return array(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Register class binding |
|
54
|
|
|
*/ |
|
55
|
|
|
private function registerBindings() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->app->bind( |
|
58
|
|
|
'Modules\Menu\Repositories\MenuRepository', |
|
59
|
|
|
function () { |
|
60
|
|
|
$repository = new EloquentMenuRepository(new Menu()); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
if (! config('app.cache')) { |
|
63
|
|
|
return $repository; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return new CacheMenuDecorator($repository); |
|
67
|
|
|
} |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$this->app->bind( |
|
71
|
|
|
'Modules\Menu\Repositories\MenuItemRepository', |
|
72
|
|
|
function () { |
|
73
|
|
|
$repository = new EloquentMenuItemRepository(new Menuitem()); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
if (! config('app.cache')) { |
|
76
|
|
|
return $repository; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return new CacheMenuItemDecorator($repository); |
|
80
|
|
|
} |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Add a menu item to the menu |
|
86
|
|
|
* @param Menuitem $item |
|
87
|
|
|
* @param Builder $menu |
|
88
|
|
|
*/ |
|
89
|
|
|
public function addItemToMenu(Menuitem $item, Builder $menu) |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->hasChildren($item)) { |
|
92
|
|
|
$this->addChildrenToMenu($item->title, $item->items, $menu, ['icon' => $item->icon]); |
|
|
|
|
|
|
93
|
|
|
} else { |
|
94
|
|
|
$target = $item->uri ?: $item->url; |
|
|
|
|
|
|
95
|
|
|
$menu->url( |
|
96
|
|
|
$target, |
|
97
|
|
|
$item->title, |
|
|
|
|
|
|
98
|
|
|
['target' => $item->target, |
|
|
|
|
|
|
99
|
|
|
'icon' => $item->icon] |
|
|
|
|
|
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Add children to menu under the give name |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $name |
|
108
|
|
|
* @param object $children |
|
109
|
|
|
* @param Builder|MenuItem $menu |
|
110
|
|
|
*/ |
|
111
|
|
|
private function addChildrenToMenu($name, $children, $menu, $attribs = []) |
|
112
|
|
|
{ |
|
113
|
|
|
$menu->dropdown($name, function (PingpongMenuItem $subMenu) use ($children) { |
|
114
|
|
|
foreach ($children as $child) { |
|
115
|
|
|
$this->addSubItemToMenu($child, $subMenu); |
|
116
|
|
|
} |
|
117
|
|
|
}, 0, $attribs); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Add children to the given menu recursively |
|
122
|
|
|
* @param Menuitem $child |
|
123
|
|
|
* @param PingpongMenuItem $sub |
|
124
|
|
|
*/ |
|
125
|
|
View Code Duplication |
private function addSubItemToMenu(Menuitem $child, PingpongMenuItem $sub) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
if ($this->hasChildren($child)) { |
|
128
|
|
|
$this->addChildrenToMenu($child->title, $child->items, $sub); |
|
|
|
|
|
|
129
|
|
|
} else { |
|
130
|
|
|
$target = $child->uri ?: $child->url; |
|
|
|
|
|
|
131
|
|
|
$sub->url($target, $child->title, 0, ['icon' => $child->icon]); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Check if the given menu item has children |
|
137
|
|
|
* |
|
138
|
|
|
* @param object $item |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
private function hasChildren($item) |
|
142
|
|
|
{ |
|
143
|
|
|
return $item->items->count() > 0; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Register the active menus |
|
148
|
|
|
*/ |
|
149
|
|
|
private function registerMenus() |
|
150
|
|
|
{ |
|
151
|
|
|
if (! $this->app['asgard.isInstalled']) { |
|
152
|
|
|
return; |
|
153
|
|
|
} |
|
154
|
|
|
$menu = $this->app->make('Modules\Menu\Repositories\MenuRepository'); |
|
155
|
|
|
$menuItem = $this->app->make('Modules\Menu\Repositories\MenuItemRepository'); |
|
156
|
|
|
foreach ($menu->allOnline() as $menu) { |
|
157
|
|
|
$menuTree = $menuItem->getTreeForMenu($menu->id); |
|
158
|
|
|
MenuFacade::create($menu->name, function (Builder $menu) use ($menuTree) { |
|
|
|
|
|
|
159
|
|
|
foreach ($menuTree as $menuItem) { |
|
160
|
|
|
$this->addItemToMenu($menuItem, $menu); |
|
161
|
|
|
} |
|
162
|
|
|
}); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
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: