feat: Hooks priority

This commit is contained in:
slawkens 2024-06-29 14:05:06 +02:00
parent d30811404b
commit dc17b701da
2 changed files with 19 additions and 3 deletions

View File

@ -25,7 +25,8 @@
"hooks": { "hooks": {
"Example Hook": { "Example Hook": {
"type": "BEFORE_PAGE", "type": "BEFORE_PAGE",
"file": "plugins/example/before.php" "file": "plugins/example/before.php",
"priority": 1000
} }
}, },
"routes": { "routes": {
@ -33,7 +34,7 @@
"pattern": "/YourAwesomePage/{name:string}/{page:int}", "pattern": "/YourAwesomePage/{name:string}/{page:int}",
"file": "plugins/your-plugin/your-awesome-page.php", "file": "plugins/your-plugin/your-awesome-page.php",
"method": "GET", "method": "GET",
"priority": "130" "priority": 130
}, },
"Redirect Example": { "Redirect Example": {
"redirect_from": "/redirectExample", "redirect_from": "/redirectExample",

View File

@ -218,14 +218,20 @@ class Plugins {
$hooks = []; $hooks = [];
foreach(self::getAllPluginsJson() as $plugin) { foreach(self::getAllPluginsJson() as $plugin) {
if (isset($plugin['hooks'])) { if (isset($plugin['hooks'])) {
$priority = 1000;
foreach ($plugin['hooks'] as $_name => $info) { foreach ($plugin['hooks'] as $_name => $info) {
if (str_contains($info['type'], 'HOOK_')) { if (str_contains($info['type'], 'HOOK_')) {
$info['type'] = str_replace('HOOK_', '', $info['type']); $info['type'] = str_replace('HOOK_', '', $info['type']);
} }
if (isset($info['priority'])) {
$priority = (int)$info['priority'];
}
if (defined('HOOK_'. $info['type'])) { if (defined('HOOK_'. $info['type'])) {
$hook = constant('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 { } else {
self::$warnings[] = 'Plugin: ' . $plugin['name'] . '. Unknown event type: ' . $info['type']; 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()) { if ($cache->enabled()) {
$cache->set('plugins_hooks', serialize($hooks), 600); $cache->set('plugins_hooks', serialize($hooks), 600);
} }