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