1
|
|
|
<?php namespace Modules\Core\Sidebar; |
2
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Container\Container; |
4
|
|
|
use Maatwebsite\Sidebar\Menu; |
5
|
|
|
use Maatwebsite\Sidebar\ShouldCache; |
6
|
|
|
use Maatwebsite\Sidebar\Sidebar; |
7
|
|
|
use Maatwebsite\Sidebar\Traits\CacheableTrait; |
8
|
|
|
use Pingpong\Modules\Contracts\RepositoryInterface; |
9
|
|
|
|
10
|
|
|
class AdminSidebar implements Sidebar, ShouldCache |
11
|
|
|
{ |
12
|
|
|
use CacheableTrait; |
13
|
|
|
/** |
14
|
|
|
* @var Menu |
15
|
|
|
*/ |
16
|
|
|
protected $menu; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var RepositoryInterface |
20
|
|
|
*/ |
21
|
|
|
protected $modules; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Container |
25
|
|
|
*/ |
26
|
|
|
protected $container; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Menu $menu |
30
|
|
|
* @param RepositoryInterface $modules |
31
|
|
|
* @param Container $container |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Menu $menu, RepositoryInterface $modules, Container $container) |
34
|
|
|
{ |
35
|
|
|
$this->menu = $menu; |
36
|
|
|
$this->modules = $modules; |
37
|
|
|
$this->container = $container; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Build your sidebar implementation here |
42
|
|
|
*/ |
43
|
|
|
public function build() |
44
|
|
|
{ |
45
|
|
|
foreach ($this->modules->enabled() as $module) { |
46
|
|
|
$lowercaseModule = strtolower($module->get('name')); |
47
|
|
|
if ($this->hasCustomSidebar($lowercaseModule) === true) { |
48
|
|
|
$class = config("asgard.{$lowercaseModule}.config.custom-sidebar"); |
49
|
|
|
$this->addToSidebar($class); |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$name = studly_case($module->get('name')); |
54
|
|
|
$class = 'Modules\\' . $name . '\\Sidebar\\SidebarExtender'; |
55
|
|
|
$this->addToSidebar($class); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Add the given class to the sidebar collection |
61
|
|
|
* @param string $class |
62
|
|
|
*/ |
63
|
|
|
private function addToSidebar($class) |
64
|
|
|
{ |
65
|
|
|
if (class_exists($class) === false) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
$extender = $this->container->make($class); |
69
|
|
|
|
70
|
|
|
$this->menu->add($extender->extendWith($this->menu)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Menu |
75
|
|
|
*/ |
76
|
|
|
public function getMenu() |
77
|
|
|
{ |
78
|
|
|
$this->build(); |
79
|
|
|
return $this->menu; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Check if the module has a custom sidebar class configured |
84
|
|
|
* @param string $module |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
private function hasCustomSidebar($module) |
88
|
|
|
{ |
89
|
|
|
$config = config("asgard.{$module}.config.custom-sidebar"); |
90
|
|
|
|
91
|
|
|
return $config !== null; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|