From 7f8946f14e0e9e11e8ed8bec650f941cb8afdb1b Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 15 Aug 2019 15:34:09 +0300 Subject: pluginhost: implement priority-based system for running hooks --- classes/pluginhost.php | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'classes/pluginhost.php') 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) { -- cgit v1.2.3