Skip to content

Commit

Permalink
Ensure hook array is always in the correct sequence
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Chris--S committed Feb 16, 2014
1 parent 33416b8 commit cf0a922
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions inc/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,41 +155,35 @@ function 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='') {

$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;
}

}

/**
Expand Down

0 comments on commit cf0a922

Please sign in to comment.