Skip to content

Commit

Permalink
Merge pull request #555 from splitbrain/event_handler_improvements
Browse files Browse the repository at this point in the history
Event handler improvements (cont.)
  • Loading branch information
splitbrain committed Feb 16, 2014
2 parents a218121 + cf0a922 commit 7abbd3b
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions inc/events.php
Expand Up @@ -11,12 +11,12 @@
class Doku_Event {

// public properties
var $name = ''; // READONLY event name, objects must register against this name to see the event
var $data = null; // READWRITE data relevant to the event, no standardised format (YET!)
var $result = null; // READWRITE the results of the event action, only relevant in "_AFTER" advise
public $name = ''; // READONLY event name, objects must register against this name to see the event
public $data = null; // READWRITE data relevant to the event, no standardised format (YET!)
public $result = null; // READWRITE the results of the event action, only relevant in "_AFTER" advise
// event handlers may modify this if they are preventing the default action
// to provide the after event handlers with event results
var $canPreventDefault = true; // READONLY if true, event handlers can prevent the events default action
public $canPreventDefault = true; // READONLY if true, event handlers can prevent the events default action

// private properties, event handlers can effect these through the provided methods
var $_default = true; // whether or not to carry out the default action associated with the event
Expand All @@ -32,6 +32,10 @@ function Doku_Event($name, &$data) {

}

function __toString() {
return $this->name;
}

/**
* advise functions
*
Expand Down Expand Up @@ -117,7 +121,7 @@ class Doku_Event_Handler {
// public properties: none

// private properties
var $_hooks = array(); // array of events and their registered handlers
protected $_hooks = array(); // array of events and their registered handlers

/**
* event_handler
Expand Down Expand Up @@ -151,44 +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='') {
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;
$obj =& $hook[0];
$method = $hook[1];
$param = $hook[2];

if (is_null($obj)) {
$method($event, $param);
} else {
$obj->$method($event, $param);
}

if (!$event->_continue) break;
}
}
}
foreach ($this->_hooks[$evt_name] as $sequenced_hooks) {
foreach ($sequenced_hooks as $hook) {
list($obj, $method, $param, $seq) = $hook;

protected function sort_hooks($hooks) {
usort($hooks, array('Doku_Event_Handler','cmp_hooks'));
return $hooks;
}
if (is_null($obj)) {
$method($event, $param);
} else {
$obj->$method($event, $param);
}

public static function cmp_hooks($a, $b) {
if ($a[3] == $b[3]) {
return 0;
if (!$event->_continue) return;
}
}
}

return ($a[3] < $b[3]) ? -1 : 1;
}

}

/**
Expand Down

0 comments on commit 7abbd3b

Please sign in to comment.