-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSystem.php
More file actions
275 lines (229 loc) · 8.25 KB
/
System.php
File metadata and controls
275 lines (229 loc) · 8.25 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
namespace Bethropolis\PluginSystem;
use Bethropolis\PluginSystem\Autoloader;
class System
{
private static $plugins = array();
private static $pluginsDir;
private static $pluginsLoaded = array();
private static $hooks = array();
private static $events = array();
private static $configFile;
private static $config;
private static function pluginClassAutoloader($className, $pluginsDir, $folder)
{
return Autoloader::pluginClassAutoloader($className, $pluginsDir, $folder);
}
private static function pluginAutoloader($file)
{
return Autoloader::pluginAutoloader($file);
}
/**
* Check if a plugin class exists.
*
* @param string $className The name of the class to check.
* @return bool Returns true if the class exists, false otherwise.
*/
private static function pluginClassExists($className)
{
return class_exists($className);
}
public static function setPluginsDir($dir)
{
self::$pluginsDir = $dir;
}
public static function getPlugins()
{
return self::$pluginsLoaded;
}
public static function getPluginsDir()
{
return self::$pluginsDir;
}
public static function getEvents()
{
return self::$events;
}
public static function getHooks()
{
return self::$hooks;
}
public static function setConfigFile($filePath)
{
self::$configFile = $filePath;
}
/**
* Load plugins from a specified directory.
*
* @param string|null $dir The directory path to load plugins from. If null, uses the default plugins directory.
* @return bool Returns true if the plugins are successfully loaded.
*/
public static function loadPlugins($dir = null)
{
if ($dir) {
self::setPluginsDir($dir);
}
$pluginsDir = self::$pluginsDir;
foreach (new \DirectoryIterator($pluginsDir) as $fileInfo) {
if (!$fileInfo->isDot()) {
if ($fileInfo->isDir()) {
$pluginFile = $fileInfo->getPathname() . '/plugin.php';
} else {
$pluginFile = $fileInfo->getPathname();
}
if (file_exists($pluginFile)) {
$classAutoloader = function ($className) use ($pluginsDir, $fileInfo) {
self::pluginClassAutoloader($className, $pluginsDir, $fileInfo->getFilename());
};
spl_autoload_register($classAutoloader);
self::pluginAutoloader($pluginFile);
if ($fileInfo->isDir()) {
$pluginClass = __NAMESPACE__ . '\\' . $fileInfo->getFilename() . 'Plugin\\Load';
} else {
$pluginClass = pathinfo($fileInfo->getFilename(), PATHINFO_FILENAME);
}
self::$pluginsLoaded[] = $pluginClass;
if (self::pluginClassExists($pluginClass)) {
$pluginInstance = new $pluginClass();
$pluginInstance->initialize();
$pluginInstance->getInfo();
}
spl_autoload_unregister($classAutoloader);
}
}
}
self::initialize();
return true;
}
private static function initialize()
{
if (file_exists(self::$configFile)) {
$config = json_decode(file_get_contents(self::$configFile), true);
} else {
$config = array();
}
self::$config = $config;
}
/**
* Link a plugin to a hook.
*
* @param mixed $hook The hook to link the plugin to.
* @param mixed $callback The callback function to be executed when the hook is triggered.
*
* @return bool
*/
public static function linkPluginToHook(string $hook, callable $callback): bool
{
if (!isset(self::$plugins[$hook])) {
self::$hooks[$hook] = array();
self::$plugins[$hook] = array();
}
self::$plugins[$hook][] = $callback;
return true;
}
/**
* Executes a hook by calling all registered callbacks associated with it.
*
* @param string $hook The name of the hook to execute.
* @param string|null $pluginName The name of the plugin. Default is null.
* @param mixed ...$args The arguments to pass to the callbacks.
* @return array The return values from the callbacks.
*/
public static function executeHook($hook, $pluginName = null, ...$args)
{
$returnValues = array();
$plugin_status = isset(self::$config["activated_plugins"]) ? self::$config["activated_plugins"] : false;
if (isset(self::$plugins[$hook])) {
foreach (self::$plugins[$hook] as $callback) {
// Safely determine if the callback belongs to a plugin class instance
$callbackPluginName = (is_array($callback) && is_object($callback[0]))
? get_class($callback[0])
: 'Closure';
// Only check activation status if it's tied to a specific plugin class
if ($callbackPluginName !== 'Closure') {
$checkName = stripslashes(strtolower($callbackPluginName));
if ($plugin_status && isset(self::$config["activated_plugins"][$checkName]) && $plugin_status[$checkName] === false) {
continue;
}
}
if ($pluginName === null || $pluginName === $callbackPluginName) {
$returnValue = call_user_func($callback, $args);
if ($returnValue !== null && $pluginName === null) {
$returnValues[] = $returnValue;
} else {
$returnValues = $returnValue;
}
}
}
}
return $returnValues;
}
/**
* Executes a series of hooks.
*
* @param array $hooks An array of hooks to execute.
* @param string|null $pluginName The name of the plugin. Defaults to null.
* @param mixed ...$args Additional arguments to pass to the hooks.
* @return array An array of return values from the executed hooks.
*/
public static function executeHooks(array $hooks, $pluginName = null, ...$args)
{
$returnValues = array();
foreach ($hooks as $hook) {
$returnValue = self::executeHook($hook, $pluginName, ...$args);
if (!empty($returnValue)) {
$returnValues[$hook] = $returnValue;
}
}
return $returnValues;
}
/**
* Registers an event.
*
* @param mixed $eventName The name of the event to register.
*/
public static function registerEvent($eventName)
{
if (!isset(self::$events[$eventName])) {
self::$events[$eventName] = array();
}
}
# unlink all events
public static function clearEvents()
{
self::$events = array();
}
/**
* Adds an action to the event specified by $eventName.
*
* @param mixed $eventName The name of the event.
* @param mixed $callback The callback function to be executed when the event is triggered.
* @return void
*/
public static function addAction($eventName, $callback)
{
if (isset(self::$events[$eventName])) {
self::$events[$eventName][] = $callback;
}
}
/**
* Triggers an event and calls all registered callbacks for that event.
*
* @param string $eventName The name of the event to trigger.
* @param mixed ...$args Additional arguments to pass to the callbacks.
* @return array The return values from the callbacks, if any.
*/
public static function triggerEvent($eventName, ...$args)
{
$returnValues = array();
if (isset(self::$events[$eventName])) {
foreach (self::$events[$eventName] as $callback) {
$returnValue = call_user_func_array($callback, $args);
if ($returnValue !== null) {
$returnValues[] = $returnValue;
}
}
}
return $returnValues;
}
}