mirror of
https://github.com/slawkens/myaac.git
synced 2025-04-26 17:29:21 +02:00
feat: Hooks priority
This commit is contained in:
parent
d30811404b
commit
dc17b701da
@ -25,7 +25,8 @@
|
||||
"hooks": {
|
||||
"Example Hook": {
|
||||
"type": "BEFORE_PAGE",
|
||||
"file": "plugins/example/before.php"
|
||||
"file": "plugins/example/before.php",
|
||||
"priority": 1000
|
||||
}
|
||||
},
|
||||
"routes": {
|
||||
@ -33,7 +34,7 @@
|
||||
"pattern": "/YourAwesomePage/{name:string}/{page:int}",
|
||||
"file": "plugins/your-plugin/your-awesome-page.php",
|
||||
"method": "GET",
|
||||
"priority": "130"
|
||||
"priority": 130
|
||||
},
|
||||
"Redirect Example": {
|
||||
"redirect_from": "/redirectExample",
|
||||
|
@ -218,14 +218,20 @@ class Plugins {
|
||||
$hooks = [];
|
||||
foreach(self::getAllPluginsJson() as $plugin) {
|
||||
if (isset($plugin['hooks'])) {
|
||||
$priority = 1000;
|
||||
|
||||
foreach ($plugin['hooks'] as $_name => $info) {
|
||||
if (str_contains($info['type'], 'HOOK_')) {
|
||||
$info['type'] = str_replace('HOOK_', '', $info['type']);
|
||||
}
|
||||
|
||||
if (isset($info['priority'])) {
|
||||
$priority = (int)$info['priority'];
|
||||
}
|
||||
|
||||
if (defined('HOOK_'. $info['type'])) {
|
||||
$hook = constant('HOOK_'. $info['type']);
|
||||
$hooks[] = ['name' => $_name, 'type' => $hook, 'file' => $info['file']];
|
||||
$hooks[] = ['name' => $_name, 'type' => $hook, 'file' => $info['file'], 'priority' => $priority];
|
||||
} else {
|
||||
self::$warnings[] = 'Plugin: ' . $plugin['name'] . '. Unknown event type: ' . $info['type'];
|
||||
}
|
||||
@ -233,6 +239,15 @@ class Plugins {
|
||||
}
|
||||
}
|
||||
|
||||
usort($hooks, function ($a, $b)
|
||||
{
|
||||
if ($a['priority'] == $b['priority']) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ($a['priority'] < $b['priority']) ? -1 : 1;
|
||||
});
|
||||
|
||||
if ($cache->enabled()) {
|
||||
$cache->set('plugins_hooks', serialize($hooks), 600);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user