summaryrefslogtreecommitdiff
path: root/classes/pluginhost.php
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2019-08-15 15:34:09 +0300
committerAndrew Dolgov <[email protected]>2019-08-15 15:34:09 +0300
commit7f8946f14e0e9e11e8ed8bec650f941cb8afdb1b (patch)
treefbfe59e81d601d1d4303a2d988095cedf887cae4 /classes/pluginhost.php
parent7ab99f0c327ce069b36d349c0abfdc56c58a70b0 (diff)
pluginhost: implement priority-based system for running hooks
Diffstat (limited to 'classes/pluginhost.php')
-rwxr-xr-xclasses/pluginhost.php32
1 files changed, 24 insertions, 8 deletions
diff --git a/classes/pluginhost.php b/classes/pluginhost.php
index d09ecca17..4d5b3252c 100755
--- a/classes/pluginhost.php
+++ b/classes/pluginhost.php
@@ -128,28 +128,44 @@ class PluginHost {
}
}
- function add_hook($type, $sender) {
+ function add_hook($type, $sender, $priority = 50) {
+ $priority = (int) $priority;
+
if (!is_array($this->hooks[$type])) {
- $this->hooks[$type] = array();
+ $this->hooks[$type] = [];
+ }
+
+ if (!is_array($this->hooks[$type][$priority])) {
+ $this->hooks[$type][$priority] = [];
}
- array_push($this->hooks[$type], $sender);
+ array_push($this->hooks[$type][$priority], $sender);
+ ksort($this->hooks[$type]);
}
function del_hook($type, $sender) {
if (is_array($this->hooks[$type])) {
- $key = array_Search($sender, $this->hooks[$type]);
- if ($key !== FALSE) {
- unset($this->hooks[$type][$key]);
+ foreach (array_keys($this->hooks[$type]) as $prio) {
+ $key = array_search($sender, $this->hooks[$type][$prio]);
+
+ if ($key !== FALSE) {
+ unset($this->hooks[$type][$prio][$key]);
+ }
}
}
}
function get_hooks($type) {
if (isset($this->hooks[$type])) {
- return $this->hooks[$type];
+ $tmp = [];
+
+ foreach (array_keys($this->hooks[$type]) as $prio) {
+ $tmp = array_merge($tmp, $this->hooks[$type][$prio]);
+ }
+
+ return $tmp;
} else {
- return array();
+ return [];
}
}
function load_all($kind, $owner_uid = false, $skip_init = false) {