Skip to content

Commit dc17b70

Browse files
committed
feat: Hooks priority
1 parent d308114 commit dc17b70

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

plugins/example.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@
2525
"hooks": {
2626
"Example Hook": {
2727
"type": "BEFORE_PAGE",
28-
"file": "plugins/example/before.php"
28+
"file": "plugins/example/before.php",
29+
"priority": 1000
2930
}
3031
},
3132
"routes": {
3233
"First Route": {
3334
"pattern": "/YourAwesomePage/{name:string}/{page:int}",
3435
"file": "plugins/your-plugin/your-awesome-page.php",
3536
"method": "GET",
36-
"priority": "130"
37+
"priority": 130
3738
},
3839
"Redirect Example": {
3940
"redirect_from": "/redirectExample",

system/src/Plugins.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,36 @@ public static function getHooks()
218218
$hooks = [];
219219
foreach(self::getAllPluginsJson() as $plugin) {
220220
if (isset($plugin['hooks'])) {
221+
$priority = 1000;
222+
221223
foreach ($plugin['hooks'] as $_name => $info) {
222224
if (str_contains($info['type'], 'HOOK_')) {
223225
$info['type'] = str_replace('HOOK_', '', $info['type']);
224226
}
225227

228+
if (isset($info['priority'])) {
229+
$priority = (int)$info['priority'];
230+
}
231+
226232
if (defined('HOOK_'. $info['type'])) {
227233
$hook = constant('HOOK_'. $info['type']);
228-
$hooks[] = ['name' => $_name, 'type' => $hook, 'file' => $info['file']];
234+
$hooks[] = ['name' => $_name, 'type' => $hook, 'file' => $info['file'], 'priority' => $priority];
229235
} else {
230236
self::$warnings[] = 'Plugin: ' . $plugin['name'] . '. Unknown event type: ' . $info['type'];
231237
}
232238
}
233239
}
234240
}
235241

242+
usort($hooks, function ($a, $b)
243+
{
244+
if ($a['priority'] == $b['priority']) {
245+
return 0;
246+
}
247+
248+
return ($a['priority'] < $b['priority']) ? -1 : 1;
249+
});
250+
236251
if ($cache->enabled()) {
237252
$cache->set('plugins_hooks', serialize($hooks), 600);
238253
}

0 commit comments

Comments
 (0)