Skip to content

Commit

Permalink
Merge pull request #552 from splitbrain/event_handler_sequencing
Browse files Browse the repository at this point in the history
Add ordering to event handlers
  • Loading branch information
splitbrain committed Feb 16, 2014
2 parents 7e9cad7 + b95f73d commit db7110f
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions inc/events.php
Expand Up @@ -148,18 +148,19 @@ function Doku_Event_Handler() {
* if NULL, method is assumed to be a globally available function
* @param $method (function) event handler function
* @param $param (mixed) data passed to the event handler
* @param $seq (int) sequence number for ordering hook execution (ascending)
*/
function register_hook($event, $advise, $obj, $method, $param=null) {
$this->_hooks[$event.'_'.$advise][] = array($obj, $method, $param);
function register_hook($event, $advise, $obj, $method, $param=null, $seq=0) {
$this->_hooks[$event.'_'.$advise][] = array($obj, $method, $param, (int)$seq);
}

function process_event(&$event,$advise='') {

$evt_name = $event->name . ($advise ? '_'.$advise : '_BEFORE');

if (!empty($this->_hooks[$evt_name])) {
foreach ($this->_hooks[$evt_name] as $hook) {
// list($obj, $method, $param) = $hook;
foreach ($this->sort_hooks($this->_hooks[$evt_name]) as $hook) {
// list($obj, $method, $param, $seq) = $hook;
$obj =& $hook[0];
$method = $hook[1];
$param = $hook[2];
Expand All @@ -174,6 +175,20 @@ function process_event(&$event,$advise='') {
}
}
}

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 db7110f

Please sign in to comment.