From: Matthias Schmidt Date: Thu, 21 May 2015 20:07:44 +0000 (+0200) Subject: Use EventListener objects instead of arrays X-Git-Tag: 3.0.0_Beta_1~2362 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=8b1d73ce12c7f9cfee696c7962466af7da815b6d;p=GitHub%2FWoltLab%2FWCF.git Use EventListener objects instead of arrays ... in EventListenerCacheBuilder and thus in EventHandler. --- diff --git a/wcfsetup/install/files/lib/system/cache/builder/EventListenerCacheBuilder.class.php b/wcfsetup/install/files/lib/system/cache/builder/EventListenerCacheBuilder.class.php index 4f2cb3b905..801e32336b 100644 --- a/wcfsetup/install/files/lib/system/cache/builder/EventListenerCacheBuilder.class.php +++ b/wcfsetup/install/files/lib/system/cache/builder/EventListenerCacheBuilder.class.php @@ -6,7 +6,10 @@ use wcf\system\WCF; /** * Caches the event listeners. * - * @author Marcel Werk + * Important: You cannot use \wcf\data\event\listener\EventListenerLister here as + * \wcf\data\DatabaseObjectList fires an event. + * + * @author Matthias Schmidt, Marcel Werk * @copyright 2001-2015 WoltLab GmbH * @license GNU Lesser General Public License * @package com.woltlab.wcf @@ -18,57 +21,50 @@ class EventListenerCacheBuilder extends AbstractCacheBuilder { * @see \wcf\system\cache\builder\AbstractCacheBuilder::rebuild() */ public function rebuild(array $parameters) { - $data = array( - 'actions' => array('user' => array(), 'admin' => array()), - 'inheritedActions' => array('user' => array(), 'admin' => array()) + $actions = array( + 'admin' => array(), + 'user' => array() + ); + + $inheritedActions = array( + 'admin' => array(), + 'user' => array() ); - // get all listeners and filter options with low priority - $sql = "SELECT event_listener.* - FROM wcf".WCF_N."_event_listener event_listener"; + $sql = "SELECT * + FROM wcf".WCF_N."_event_listener + ORDER BY niceValue ASC, listenerClassName ASC"; $statement = WCF::getDB()->prepareStatement($sql); $statement->execute(); - while ($row = $statement->fetchArray()) { - // distinguish between inherited actions and non-inherited actions - if (!$row['inherit']) { - $data['actions'][$row['environment']][EventHandler::generateKey($row['eventClassName'], $row['eventName'])][] = $row; + while ($eventListener = $statement->fetchObject('wcf\data\event\listener\EventListener')) { + if (!$eventListener->inherit) { + if (!isset($actions[$eventListener->environment])) { + $actions[$eventListener->environment] = array(); + } + + $key = EventHandler::generateKey($eventListener->eventClassName, $eventListener->eventName); + if (!isset($actions[$eventListener->environment][$key])) { + $actions[$eventListener->environment][$key] = array(); + } + + $actions[$eventListener->environment][$key][] = $eventListener; } else { - if (!isset($data['inheritedActions'][$row['environment']][$row['eventClassName']])) $data['inheritedActions'][$row['environment']][$row['eventClassName']] = array(); - $data['inheritedActions'][$row['environment']][$row['eventClassName']][$row['eventName']][] = $row; - } - } - - // sort data by nice value and class name - foreach ($data['actions'] as &$listenerMap) { - foreach ($listenerMap as &$listeners) { - uasort($listeners, array(__CLASS__, 'sortListeners')); - } - } - - foreach ($data['inheritedActions'] as &$listenerMap) { - foreach ($listenerMap as &$listeners) { - foreach ($listeners as &$val) { - uasort($val, array(__CLASS__, 'sortListeners')); + if (!isset($inheritedActions[$eventListener->environment])) { + $inheritedActions[$eventListener->environment] = array(); + } + + if (!isset($inheritedActions[$eventListener->environment][$eventListener->eventClassName])) { + $inheritedActions[$eventListener->environment][$eventListener->eventClassName] = array(); } + + $inheritedActions[$eventListener->environment][$eventListener->eventClassName][$eventListener->eventName][] = $eventListener; } } - return $data; - } - - /** - * Sorts the event listeners by nice value. - */ - public static function sortListeners($listenerA, $listenerB) { - if ($listenerA['niceValue'] < $listenerB['niceValue']) { - return -1; - } - else if ($listenerA['niceValue'] > $listenerB['niceValue']) { - return 1; - } - else { - return strcmp($listenerA['listenerClassName'], $listenerB['listenerClassName']); - } + return array( + 'actions' => $actions, + 'inheritedActions' => $inheritedActions + ); } } diff --git a/wcfsetup/install/files/lib/system/event/EventHandler.class.php b/wcfsetup/install/files/lib/system/event/EventHandler.class.php index 66a5268a71..03e6be53d8 100644 --- a/wcfsetup/install/files/lib/system/event/EventHandler.class.php +++ b/wcfsetup/install/files/lib/system/event/EventHandler.class.php @@ -97,31 +97,31 @@ class EventHandler extends SingletonFactory { if (isset($this->inheritedActions[$member])) { $actions = $this->inheritedActions[$member]; if (isset($actions[$eventName]) && !empty($actions[$eventName])) { - foreach ($actions[$eventName] as $action) { - if (isset($this->inheritedActionsObjects[$name][$action['listenerClassName']])) continue; + foreach ($actions[$eventName] as $eventListener) { + if (isset($this->inheritedActionsObjects[$name][$eventListener->listenerClassName])) continue; // get class object - if (isset($this->listenerObjects[$action['listenerClassName']])) { - $object = $this->listenerObjects[$action['listenerClassName']]; + if (isset($this->listenerObjects[$eventListener->listenerClassName])) { + $object = $this->listenerObjects[$eventListener->listenerClassName]; } else { $object = null; // instance action object - if (!class_exists($action['listenerClassName'])) { - throw new SystemException("Unable to find class '".$action['listenerClassName']."'"); + if (!class_exists($eventListener->listenerClassName)) { + throw new SystemException("Unable to find class '".$eventListener->listenerClassName."'"); } - if (!ClassUtil::isInstanceOf($action['listenerClassName'], 'wcf\system\event\listener\IParameterizedEventListener')) { + if (!ClassUtil::isInstanceOf($eventListener->listenerClassName, 'wcf\system\event\listener\IParameterizedEventListener')) { // legacy event listeners - if (!ClassUtil::isInstanceOf($action['listenerClassName'], 'wcf\system\event\IEventListener')) { - throw new SystemException("'".$action['listenerClassName']."' does not implement 'wcf\system\event\listener\IParameterizedEventListener'"); + if (!ClassUtil::isInstanceOf($eventListener->listenerClassName, 'wcf\system\event\IEventListener')) { + throw new SystemException("'".$eventListener->listenerClassName."' does not implement 'wcf\system\event\listener\IParameterizedEventListener'"); } } - $object = new $action['listenerClassName']; - $this->listenerObjects[$action['listenerClassName']] = $object; + $object = new $eventListener->listenerClassName; + $this->listenerObjects[$eventListener->listenerClassName] = $object; } - if ($object !== null) $this->inheritedActionsObjects[$name][$action['listenerClassName']] = $object; + if ($object !== null) $this->inheritedActionsObjects[$name][$eventListener->listenerClassName] = $object; } } } @@ -181,30 +181,30 @@ class EventHandler extends SingletonFactory { } $this->actionsObjects[$name] = array(); - foreach ($this->actions[$name] as $action) { - if (isset($this->actionsObjects[$name][$action['listenerClassName']])) continue; + foreach ($this->actions[$name] as $eventListener) { + if (isset($this->actionsObjects[$name][$eventListener->listenerClassName])) continue; // get class object - if (isset($this->listenerObjects[$action['listenerClassName']])) { - $object = $this->listenerObjects[$action['listenerClassName']]; + if (isset($this->listenerObjects[$eventListener->listenerClassName])) { + $object = $this->listenerObjects[$eventListener->listenerClassName]; } else { // instance action object - if (!class_exists($action['listenerClassName'])) { - throw new SystemException("Unable to find class '".$action['listenerClassName']."'"); + if (!class_exists($eventListener->listenerClassName)) { + throw new SystemException("Unable to find class '".$eventListener->listenerClassName."'"); } - if (!ClassUtil::isInstanceOf($action['listenerClassName'], 'wcf\system\event\listener\IParameterizedEventListener')) { + if (!ClassUtil::isInstanceOf($eventListener->listenerClassName, 'wcf\system\event\listener\IParameterizedEventListener')) { // legacy event listeners - if (!ClassUtil::isInstanceOf($action['listenerClassName'], 'wcf\system\event\IEventListener')) { - throw new SystemException("'".$action['listenerClassName']."' does not implement 'wcf\system\event\listener\IParameterizedEventListener'"); + if (!ClassUtil::isInstanceOf($eventListener->listenerClassName, 'wcf\system\event\IEventListener')) { + throw new SystemException("'".$eventListener->listenerClassName."' does not implement 'wcf\system\event\listener\IParameterizedEventListener'"); } } - $object = new $action['listenerClassName']; - $this->listenerObjects[$action['listenerClassName']] = $object; + $object = new $eventListener->listenerClassName; + $this->listenerObjects[$eventListener->listenerClassName] = $object; } - $this->actionsObjects[$name][$action['listenerClassName']] = $object; + $this->actionsObjects[$name][$eventListener->listenerClassName] = $object; } }