diff options
author | Andreas Gohr <andi@splitbrain.org> | 2014-02-16 21:34:34 +0100 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2014-02-16 21:34:34 +0100 |
commit | 7abbd3bc441b7932081591f6fe113b4c501e2488 (patch) | |
tree | e298e3c10da5b1b8cc706fdb5199af7f460035b3 | |
parent | a218121f7a6086eac0469b13dedbaa04fea769ca (diff) | |
parent | cf0a922758503003100c988bf25eeaaa8e5b287c (diff) | |
download | rpg-7abbd3bc441b7932081591f6fe113b4c501e2488.tar.gz rpg-7abbd3bc441b7932081591f6fe113b4c501e2488.tar.bz2 |
Merge pull request #555 from splitbrain/event_handler_improvements
Event handler improvements (cont.)
-rw-r--r-- | inc/events.php | 61 |
1 files changed, 28 insertions, 33 deletions
diff --git a/inc/events.php b/inc/events.php index 9c3169b34..888b968b5 100644 --- a/inc/events.php +++ b/inc/events.php @@ -11,12 +11,12 @@ if(!defined('DOKU_INC')) die('meh.'); 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 @@ -32,6 +32,10 @@ class Doku_Event { } + function __toString() { + return $this->name; + } + /** * advise functions * @@ -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 @@ -151,44 +155,35 @@ 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='') { + 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; } - } /** |