Use EventListener objects instead of arrays
authorMatthias Schmidt <gravatronics@live.com>
Thu, 21 May 2015 20:07:44 +0000 (22:07 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Thu, 21 May 2015 20:07:44 +0000 (22:07 +0200)
... in EventListenerCacheBuilder and thus in EventHandler.

wcfsetup/install/files/lib/system/cache/builder/EventListenerCacheBuilder.class.php
wcfsetup/install/files/lib/system/event/EventHandler.class.php

index 4f2cb3b905481ca4f085936ccf100543d2182791..801e32336b870e7d299db2400262b7a99dd003cc 100644 (file)
@@ -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 <http://opensource.org/licenses/lgpl-license.php>
  * @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
+               );
        }
 }
index 66a5268a710d6ed3aa88f0f471c6536d078875c1..03e6be53d889377acb39d9c586e76729c3d2e7d5 100644 (file)
@@ -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;
                        }
                }