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 ModulesCollection |
19
|
|
|
*/ |
20
|
1 |
|
public function getNames() |
21
|
|
|
{ |
22
|
1 |
|
return $this->getModules()->all(); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param $name |
27
|
|
|
*/ |
28
|
|
|
public function addModule($name) |
29
|
|
|
{ |
30
|
|
|
if (!$this->getModules()->offsetExists($name)) { |
31
|
|
|
$this->getModules()->set($name, $name); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $name |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getViewPath($name) |
41
|
|
|
{ |
42
|
|
|
return $this->getModuleDirectory($name) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $name |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function getModuleDirectory($name) |
51
|
|
|
{ |
52
|
|
|
return $this->getModulesBaseDirectory() . $name; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getModulesBaseDirectory() |
59
|
|
|
{ |
60
|
|
|
return defined('MODULES_PATH') ? |
61
|
|
|
MODULES_PATH |
|
|
|
|
62
|
|
|
: app('path') . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return ModulesCollection |
67
|
|
|
*/ |
68
|
2 |
|
public function getModules(): ModulesCollection |
69
|
|
|
{ |
70
|
2 |
|
if ($this->modules === null) { |
71
|
2 |
|
$this->modules = new ModulesCollection(); |
72
|
2 |
|
$this->loadFromConfig(); |
73
|
|
|
} |
74
|
2 |
|
return $this->modules; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ModulesCollection $sections |
79
|
|
|
*/ |
80
|
|
|
public function setModules(ModulesCollection $sections): void |
81
|
|
|
{ |
82
|
|
|
$this->modules = $sections; |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
protected function loadFromConfig() |
86
|
|
|
{ |
87
|
2 |
|
if (function_exists('config')) { |
88
|
2 |
|
$data = config('mvc.modules', ['admin', 'frontend']); |
89
|
2 |
|
foreach ($data as $key => $row) { |
90
|
2 |
|
$this->modules->set($key, $row); |
91
|
|
|
} |
92
|
|
|
} |
93
|
2 |
|
} |
94
|
|
|
} |
95
|
|
|
|