From adb80b0288b9e3c7c4576056d5e1d0dc77bcb842 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 16 Feb 2014 16:35:34 +0000 Subject: An event object used as a string will return its name - the event name --- inc/events.php | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'inc') diff --git a/inc/events.php b/inc/events.php index 9c3169b34..44ccdb8a6 100644 --- a/inc/events.php +++ b/inc/events.php @@ -32,6 +32,10 @@ class Doku_Event { } + function __toString() { + return $this->name; + } + /** * advise functions * -- cgit v1.2.3 From d668eb9fab52028922a60c337d370b956602a533 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 16 Feb 2014 16:36:33 +0000 Subject: since php 5, no need to access objects by reference --- inc/events.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'inc') diff --git a/inc/events.php b/inc/events.php index 44ccdb8a6..ed60c95fe 100644 --- a/inc/events.php +++ b/inc/events.php @@ -164,10 +164,7 @@ class Doku_Event_Handler { 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]; + list($obj, $method, $param, $seq) = $hook; if (is_null($obj)) { $method($event, $param); -- cgit v1.2.3 From b9bd3ecff7347bc96d59368f3f4ba4a271ecf0bd Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 16 Feb 2014 20:15:07 +0000 Subject: add appropriate visibility keywords to event properties --- inc/events.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'inc') diff --git a/inc/events.php b/inc/events.php index ed60c95fe..e9ffa5a92 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 @@ -121,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 -- cgit v1.2.3 From 33416b823f72c23623d441e6341f564c41cd8f8f Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 16 Feb 2014 20:18:30 +0000 Subject: remove reference operator from object, no longer required --- inc/events.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc') diff --git a/inc/events.php b/inc/events.php index e9ffa5a92..91b0d181a 100644 --- a/inc/events.php +++ b/inc/events.php @@ -158,7 +158,7 @@ class Doku_Event_Handler { $this->_hooks[$event.'_'.$advise][] = array($obj, $method, $param, (int)$seq); } - function process_event(&$event,$advise='') { + function process_event($event,$advise='') { $evt_name = $event->name . ($advise ? '_'.$advise : '_BEFORE'); -- cgit v1.2.3 From cf0a922758503003100c988bf25eeaaa8e5b287c Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 16 Feb 2014 20:19:09 +0000 Subject: 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. --- inc/events.php | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) (limited to 'inc') 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; - } - } /** -- cgit v1.2.3