Event listeners can listen to multiple events in same class
authorMatthias Schmidt <gravatronics@live.com>
Sat, 23 May 2015 11:08:53 +0000 (13:08 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 23 May 2015 11:09:44 +0000 (13:09 +0200)
CHANGELOG.md
wcfsetup/install/files/lib/data/event/listener/EventListener.class.php
wcfsetup/install/files/lib/system/cache/builder/EventListenerCacheBuilder.class.php
wcfsetup/setup/db/install.sql

index cdbe6fe6bb961cb24a045542e92996f937b05546..064cb5d3cee3c4344bf50bfdf44f7e9f3bf36d9e 100644 (file)
@@ -7,6 +7,7 @@
 * instruction file name for most PIPs has default value provided by `wcf\system\package\plugin\IPackageInstallationPlugin::getDefaultFilename()`.
 * `options` support for cronjobs.
 * `name` attribute for cronjob PIP (`cronjobName` for cronjob objects).
+* `eventName` of event listener PIP supports multiple events.
 * `permissions` and `options` support for event listeners.
 * `name` attribute for event listener PIP (`listenerName` for event listener objects).
 * `permissions` and `options` support for template listeners.
index 49d91cae7a0a89b16cef3a33efe3171ffddf4dc8..0632cebb9e15425bb14360c87c8809f3c1cff1e2 100644 (file)
@@ -34,4 +34,13 @@ class EventListener extends DatabaseObject {
         * @deprecated  will be removed once listener names are mandatory
         */
        const AUTOMATIC_NAME_PREFIX = 'com.woltlab.wcf.eventListener';
+       
+       /**
+        * Returns the names of all events listened to.
+        * 
+        * @return      array<string>
+        */
+       public function getEventNames() {
+               return explode(',', $this->eventName);
+       }
 }
index 7f80b0b3d779916ade5fd25f217db4eb2838609a..1bc23a77941d856f801f46243fbf2f4baffc4727 100644 (file)
@@ -37,28 +37,34 @@ class EventListenerCacheBuilder extends AbstractCacheBuilder {
                $statement = WCF::getDB()->prepareStatement($sql);
                $statement->execute();
                while ($eventListener = $statement->fetchObject('wcf\data\event\listener\EventListener')) {
+                       $eventNames = $eventListener->getEventNames();
+                       
                        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();
+                               foreach ($eventNames as $eventName) {
+                                       $key = EventHandler::generateKey($eventListener->eventClassName, $eventName);
+                                       if (!isset($actions[$eventListener->environment][$key])) {
+                                               $actions[$eventListener->environment][$key] = array();
+                                       }
+                                       
+                                       $actions[$eventListener->environment][$key][] = $eventListener;
                                }
-                               
-                               $actions[$eventListener->environment][$key][] = $eventListener;
                        }
                        else {
                                if (!isset($inheritedActions[$eventListener->environment])) {
                                        $inheritedActions[$eventListener->environment] = array();
                                }
                                
-                               if (!isset($inheritedActions[$eventListener->environment][$eventListener->eventClassName])) {
-                                       $inheritedActions[$eventListener->environment][$eventListener->eventClassName] = array();
+                               foreach ($eventNames as $eventName) {
+                                       if (!isset($inheritedActions[$eventListener->environment][$eventListener->eventClassName])) {
+                                               $inheritedActions[$eventListener->environment][$eventListener->eventClassName] = array();
+                                       }
+                                       
+                                       $inheritedActions[$eventListener->environment][$eventListener->eventClassName][$eventName][] = $eventListener;
                                }
-                               
-                               $inheritedActions[$eventListener->environment][$eventListener->eventClassName][$eventListener->eventName][] = $eventListener;
                        }
                }
                
index ebd39536f5b1678a4a00a418c0313c154e0c6459..3c298dc5ac1994a1ae5b602d609c405ff2d4f2bd 100644 (file)
@@ -378,15 +378,14 @@ CREATE TABLE wcf1_event_listener (
        environment ENUM('user', 'admin') NOT NULL DEFAULT 'user',
        listenerName VARCHAR(255) NOT NULL,
        eventClassName VARCHAR(80) NOT NULL DEFAULT '',
-       eventName VARCHAR(50) NOT NULL DEFAULT '',
+       eventName TEXT,
        listenerClassName VARCHAR(200) NOT NULL DEFAULT '',
        inherit TINYINT(1) NOT NULL DEFAULT 0,
        niceValue TINYINT(3) NOT NULL DEFAULT 0,
        permissions TEXT,
        options TEXT,
        
-       UNIQUE KEY listenerName (listenerName, packageID),
-       UNIQUE KEY packageID (packageID, environment, eventClassName, eventName, listenerClassName)
+       UNIQUE KEY listenerName (listenerName, packageID)
 );
 
 DROP TABLE IF EXISTS wcf1_import_mapping;