bytic /
mvc
| 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 | * @var ModulesCollection |
||||
| 13 | */ |
||||
| 14 | protected $modules = null; |
||||
| 15 | |||||
| 16 | /** |
||||
| 17 | * @return array |
||||
| 18 | */ |
||||
| 19 | public function getNames() |
||||
| 20 | 1 | { |
|||
| 21 | return $this->getModules()->values(); |
||||
| 22 | 1 | } |
|||
| 23 | |||||
| 24 | /** |
||||
| 25 | * @param $name |
||||
| 26 | * @return bool |
||||
| 27 | */ |
||||
| 28 | public function hasModule($name) |
||||
| 29 | 1 | { |
|||
| 30 | return $this->getModules()->has($name); |
||||
| 31 | 1 | } |
|||
| 32 | |||||
| 33 | /** |
||||
| 34 | * @param $name |
||||
| 35 | */ |
||||
| 36 | public function addModule($name) |
||||
| 37 | { |
||||
| 38 | if (!$this->getModules()->offsetExists($name)) { |
||||
| 39 | $this->getModules()->set($name, $name); |
||||
| 40 | } |
||||
| 41 | } |
||||
| 42 | |||||
| 43 | /** |
||||
| 44 | * @param $name |
||||
| 45 | * |
||||
| 46 | * @return string |
||||
| 47 | */ |
||||
| 48 | public function getViewPath($name) |
||||
| 49 | { |
||||
| 50 | return $this->getModuleDirectory($name) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR; |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | /** |
||||
| 54 | * @param $name |
||||
| 55 | * |
||||
| 56 | * @return string |
||||
| 57 | */ |
||||
| 58 | public function getModuleDirectory($name) |
||||
| 59 | { |
||||
| 60 | return $this->getModulesBaseDirectory() . $name; |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | /** |
||||
| 64 | * @return string |
||||
| 65 | */ |
||||
| 66 | public function getModulesBaseDirectory() |
||||
| 67 | { |
||||
| 68 | return defined('MODULES_PATH') ? |
||||
| 69 | MODULES_PATH |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 70 | : app('path') . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR; |
||||
|
0 ignored issues
–
show
Are you sure
app('path') of type mixed|object can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 71 | } |
||||
| 72 | |||||
| 73 | /** |
||||
| 74 | * @return ModulesCollection |
||||
| 75 | */ |
||||
| 76 | public function getModules(): ModulesCollection |
||||
| 77 | 3 | { |
|||
| 78 | if ($this->modules === null) { |
||||
| 79 | 3 | $this->modules = new ModulesCollection(); |
|||
| 80 | 3 | $this->loadFromConfig(); |
|||
| 81 | 3 | } |
|||
| 82 | return $this->modules; |
||||
| 83 | 3 | } |
|||
| 84 | |||||
| 85 | /** |
||||
| 86 | * @param ModulesCollection $sections |
||||
| 87 | */ |
||||
| 88 | public function setModules(ModulesCollection $sections): void |
||||
| 89 | { |
||||
| 90 | $this->modules = $sections; |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | protected function loadFromConfig() |
||||
| 94 | 3 | { |
|||
| 95 | if (function_exists('config')) { |
||||
| 96 | 3 | $data = config('mvc.modules', ['admin', 'frontend']); |
|||
| 97 | 3 | foreach ($data as $key => $row) { |
|||
| 98 | 3 | $this->modules->set($row, $row); |
|||
| 99 | 3 | } |
|||
| 100 | } |
||||
| 101 | } |
||||
| 102 | } |
||||
| 103 |