-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleLoader.php
More file actions
156 lines (128 loc) · 5.4 KB
/
ModuleLoader.php
File metadata and controls
156 lines (128 loc) · 5.4 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
<?php
namespace MerapiPanel\Box\Module {
use Exception;
use MerapiPanel\Box\Module\AbstractLoader;
use MerapiPanel\Box\Container;
use MerapiPanel\Box\Module\Entity\Module;
use MerapiPanel\Box\Module\Entity\Fragment;
use MerapiPanel\Box\Module\Entity\Proxy;
use MerapiPanel\Utility\Http\Request;
use Symfony\Component\Filesystem\Path;
use Throwable;
class ModuleLoader extends AbstractLoader
{
protected string $directory;
protected string $classNamePrefix = "\\MerapiPanel\\Module";
private static array $defaultModules = ["Setting", "Panel", "Ajax", "Auth", "Editor", "FileManager", "Dashboard", "Setting", "User", "Website"];
public function __construct(string $directory)
{
$this->directory = $directory;
}
public final static function getDefaultModules()
{
return self::$defaultModules;
}
private array $scannedListModule = [];
public function getListModule(): array
{
if (empty($this->scannedListModule)) {
foreach (glob(Path::join($this->directory, "*")) as $dirname) {
$moduleName = basename($dirname);
if (in_array($moduleName, self::getDefaultModules()) || file_exists(Path::join($dirname, ".active"))) {
$this->scannedListModule[$moduleName] = $dirname;
}
}
}
return $this->scannedListModule;
}
public function initialize(Container $container): void
{
$this->postLoad($container);
$this->registerController($container);
}
private $initiatedList = [];
public final function postLoad($container)
{
foreach (glob(Path::join($this->directory, "*")) as $dirname) {
$moduleName = basename($dirname);
if (in_array($moduleName, $this->initiatedList)) continue;
if (in_array($moduleName, self::getDefaultModules()) || file_exists(Path::join($dirname, ".active"))) {
try {
$service = $container->$moduleName->Service;
if ($service instanceof Proxy && $service->__method_exists("onInit")) {
$service->onInit();
$this->initiatedList[] = $moduleName;
}
} catch (Throwable $t) {
// silent
}
}
}
}
public final function registerController($container)
{
$access = ['guest'];
if (isset($_ENV['__MP_ADMIN__']['prefix'])) {
if (strpos(Request::getInstance()->getPath(), $_ENV['__MP_ADMIN__']['prefix']) === 0) {
$access[] = "admin";
}
}
foreach (glob(Path::join($this->directory, "*"), GLOB_ONLYDIR) as $dirname) {
$moduleName = basename($dirname);
try {
/**
* @var Fragment $controller
*/
if ($controller = $container->$moduleName->Controller) {
foreach ($access as $accessName) {
$accessName = ucfirst($accessName);
if ($controller->$accessName) {
try {
$controller->$accessName->register();
} catch (Throwable $e) {
error_log("Unable to register controller: $moduleName, " . $e->getMessage());
}
}
}
}
} catch (Exception $e) {
// silent
}
}
}
function loadFragment(string $name, Module|Fragment $parent): Fragment|null
{
if ($parent instanceof Fragment) {
if (!class_exists($parent->resolveClassName($name))) {
if (file_exists($parent->resolvePath($name))) {
return new Fragment($name, $parent);
}
return null;
}
return new Proxy($name, $parent);
} else if ($parent instanceof Module) {
if (!class_exists($parent->namespace . '\\' . $name)) {
if (file_exists(Path::join($parent->path, $name))) {
return new Fragment($name, $parent);
}
return null;
}
return new Proxy($name, $parent);
}
return null;
}
function loadModule(string $name, Container $container): Module | bool
{
$path = Path::join($this->directory, $name);
if (!file_exists($path)) {
return false;
}
if (!is_dir(Path::join($path, "data"))) mkdir(Path::join($path, "data"));
if (!in_array($name, self::$defaultModules) && !file_exists(Path::join($path, ".active"))) throw new Exception("Module {$name} inactive", 500);
return new Module($container, [
"namespace" => $this->classNamePrefix . "\\$name",
"path" => $path,
]);
}
}
}