diff options
author | Christopher Smith <chris@jalakai.co.uk> | 2014-02-16 20:19:09 +0000 |
---|---|---|
committer | Christopher Smith <chris@jalakai.co.uk> | 2014-02-16 20:19:09 +0000 |
commit | cf0a922758503003100c988bf25eeaaa8e5b287c (patch) | |
tree | 78626c9921b44de69ee05c86ab42a3a9e7746448 /inc | |
parent | 33416b823f72c23623d441e6341f564c41cd8f8f (diff) | |
download | rpg-cf0a922758503003100c988bf25eeaaa8e5b287c.tar.gz rpg-cf0a922758503003100c988bf25eeaaa8e5b287c.tar.bz2 |
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.
Diffstat (limited to 'inc')
-rw-r--r-- | inc/events.php | 40 |
1 files changed, 17 insertions, 23 deletions
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; - } - } /** |