forked from quantum-php/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleLoader.php
More file actions
185 lines (150 loc) · 4.62 KB
/
Copy pathModuleLoader.php
File metadata and controls
185 lines (150 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
/**
* Quantum PHP Framework
*
* An open source software development framework for PHP
*
* @package Quantum
* @author Arman Ag. <arman@quantumphp.io>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link https://quantumphp.io/
* @since 3.0.0
*/
namespace Quantum\Module;
use Quantum\Storage\Factories\FileSystemFactory;
use Quantum\Config\Exceptions\ConfigException;
use Quantum\Module\Exceptions\ModuleException;
use Quantum\Router\Exceptions\RouteException;
use Quantum\App\Exceptions\BaseException;
use Quantum\Di\Exceptions\DiException;
use Quantum\Storage\FileSystem;
use ReflectionException;
use Quantum\App\App;
use Quantum\Di\Di;
use Closure;
/**
* Class ModuleLoader
* @package Quantum\Module
*/
class ModuleLoader
{
/**
* @var array<string, array<string>>
*/
private array $moduleDependencies = [];
/**
* @var array<string, array<string, mixed>>
*/
private array $moduleConfigs = [];
/** @var array<string, Closure> */
private array $moduleRouteClosures = [];
private FileSystem $fs;
/**
* @throws ModuleException|ConfigException|DiException|BaseException|ReflectionException
*/
public function __construct()
{
$this->fs = FileSystemFactory::get();
Di::registerDependencies($this->loadModulesDependencies());
}
/**
* @return array<string, Closure>
* @throws ModuleException|RouteException
*/
public function loadModulesRoutes(): array
{
if (empty($this->moduleConfigs)) {
$this->loadModuleConfig();
}
$modulesRoutes = [];
foreach ($this->moduleConfigs as $module => $options) {
if (!$this->isModuleEnabled($options)) {
continue;
}
$modulesRoutes[$module] = $this->getModuleRouteDefinitions($module);
}
return $modulesRoutes;
}
/**
* @throws ModuleException|RouteException
*/
private function getModuleRouteDefinitions(string $module): Closure
{
if (isset($this->moduleRouteClosures[$module])) {
return $this->moduleRouteClosures[$module];
}
$moduleRoutesPath = modules_dir() . DS . $module . DS . 'routes' . DS . 'routes.php';
if (!$this->fs->exists($moduleRoutesPath)) {
throw ModuleException::moduleRoutesNotFound($module);
}
$closure = $this->fs->require($moduleRoutesPath);
if (!$closure instanceof Closure) {
throw RouteException::notClosure();
}
return $this->moduleRouteClosures[$module] = $closure;
}
/**
* @return array<string, string>
* @throws ModuleException
*/
public function loadModulesDependencies(): array
{
if (empty($this->moduleConfigs)) {
$this->loadModuleConfig();
}
$modulesDependencies = [];
foreach ($this->moduleConfigs as $module => $options) {
if (!$this->isModuleEnabled($options)) {
continue;
}
$modulesDependencies = array_merge($modulesDependencies, $this->getModuleDependencies($module));
}
return $modulesDependencies;
}
/**
* @return array<string>
*/
public function getModuleDependencies(string $module): array
{
if (!isset($this->moduleDependencies[$module])) {
$file = modules_dir() . DS . $module . DS . 'config' . DS . 'dependencies.php';
if ($this->fs->exists($file)) {
$deps = $this->fs->require($file);
$this->moduleDependencies[$module] = is_array($deps) ? $deps : [];
} else {
$this->moduleDependencies[$module] = [];
}
}
return $this->moduleDependencies[$module];
}
/**
* @return array<string, array<string, mixed>>
* @throws ModuleException
*/
public function getModuleConfigs(): array
{
if (empty($this->moduleConfigs)) {
$this->loadModuleConfig();
}
return $this->moduleConfigs;
}
/**
* @throws ModuleException
*/
private function loadModuleConfig(): void
{
$configPath = App::getBaseDir() . DS . 'shared' . DS . 'config' . DS . 'modules.php';
if (!$this->fs->exists($configPath)) {
throw ModuleException::moduleConfigNotFound();
}
$this->moduleConfigs = $this->fs->require($configPath);
}
/**
* @param array<string, mixed> $options
*/
private function isModuleEnabled(array $options): bool
{
return (bool) ($options['enabled'] ?? false);
}
}