From cf0a922758503003100c988bf25eeaaa8e5b287c Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 16 Feb 2014 20:19:09 +0000 Subject: Ensure hook array is always in the correct sequence Changed to sort on add from sort on process for efficiency. Some events (e.g. AUTH_ACL_CHECK) could be trigged many times in a single page request. --- inc/events.php | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) (limited to 'inc/events.php') diff --git a/inc/events.php b/inc/events.php index 91b0d181a..888b968b5 100644 --- a/inc/events.php +++ b/inc/events.php @@ -155,7 +155,13 @@ class Doku_Event_Handler { * @param $seq (int) sequence number for ordering hook execution (ascending) */ function register_hook($event, $advise, $obj, $method, $param=null, $seq=0) { - $this->_hooks[$event.'_'.$advise][] = array($obj, $method, $param, (int)$seq); + $seq = (int)$seq; + $doSort = !isset($this->_hooks[$event.'_'.$advise][$seq]); + $this->_hooks[$event.'_'.$advise][$seq][] = array($obj, $method, $param); + + if ($doSort) { + ksort($this->_hooks[$event.'_'.$advise]); + } } function process_event($event,$advise='') { @@ -163,33 +169,21 @@ class Doku_Event_Handler { $evt_name = $event->name . ($advise ? '_'.$advise : '_BEFORE'); if (!empty($this->_hooks[$evt_name])) { - foreach ($this->sort_hooks($this->_hooks[$evt_name]) as $hook) { - list($obj, $method, $param, $seq) = $hook; + foreach ($this->_hooks[$evt_name] as $sequenced_hooks) { + foreach ($sequenced_hooks as $hook) { + list($obj, $method, $param, $seq) = $hook; - if (is_null($obj)) { - $method($event, $param); - } else { - $obj->$method($event, $param); - } + if (is_null($obj)) { + $method($event, $param); + } else { + $obj->$method($event, $param); + } - if (!$event->_continue) break; + if (!$event->_continue) return; + } } } } - - protected function sort_hooks($hooks) { - usort($hooks, array('Doku_Event_Handler','cmp_hooks')); - return $hooks; - } - - public static function cmp_hooks($a, $b) { - if ($a[3] == $b[3]) { - return 0; - } - - return ($a[3] < $b[3]) ? -1 : 1; - } - } /** -- cgit v1.2.3