1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Mvc\Modules; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class ModulesManager |
7
|
|
|
* @package Nip\Mvc\Modules |
8
|
|
|
*/ |
9
|
|
|
class ModulesManager |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var ModulesCollection |
14
|
|
|
*/ |
15
|
|
|
protected $modules = null; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
1 |
|
public function getNames() |
21
|
|
|
{ |
22
|
1 |
|
return $this->getModules()->values(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param $name |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
1 |
|
public function hasModule($name) |
30
|
|
|
{ |
31
|
1 |
|
return $this->getModules()->has($name); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $name |
36
|
|
|
*/ |
37
|
|
|
public function addModule($name) |
38
|
|
|
{ |
39
|
|
|
if (!$this->getModules()->offsetExists($name)) { |
40
|
|
|
$this->getModules()->set($name, $name); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $name |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getViewPath($name) |
50
|
|
|
{ |
51
|
|
|
return $this->getModuleDirectory($name) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $name |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getModuleDirectory($name) |
60
|
|
|
{ |
61
|
|
|
return $this->getModulesBaseDirectory() . $name; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getModulesBaseDirectory() |
68
|
|
|
{ |
69
|
|
|
return defined('MODULES_PATH') ? |
70
|
|
|
MODULES_PATH |
|
|
|
|
71
|
|
|
: app('path') . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return ModulesCollection |
76
|
|
|
*/ |
77
|
3 |
|
public function getModules(): ModulesCollection |
78
|
|
|
{ |
79
|
3 |
|
if ($this->modules === null) { |
80
|
3 |
|
$this->modules = new ModulesCollection(); |
81
|
3 |
|
$this->loadFromConfig(); |
82
|
|
|
} |
83
|
3 |
|
return $this->modules; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param ModulesCollection $sections |
88
|
|
|
*/ |
89
|
|
|
public function setModules(ModulesCollection $sections): void |
90
|
|
|
{ |
91
|
|
|
$this->modules = $sections; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
protected function loadFromConfig() |
95
|
|
|
{ |
96
|
3 |
|
if (function_exists('config')) { |
97
|
3 |
|
$data = config('mvc.modules', ['admin', 'frontend']); |
98
|
3 |
|
foreach ($data as $key => $row) { |
99
|
3 |
|
$this->modules->set($row, $row); |
100
|
|
|
} |
101
|
|
|
} |
102
|
3 |
|
} |
103
|
|
|
} |
104
|
|
|
|