From c9ebc6793ac33b791905c5180701abe8e5aa2a7b Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 23 May 2015 13:08:53 +0200 Subject: [PATCH] Event listeners can listen to multiple events in same class --- CHANGELOG.md | 1 + .../event/listener/EventListener.class.php | 9 +++++++ .../EventListenerCacheBuilder.class.php | 24 ++++++++++++------- wcfsetup/setup/db/install.sql | 5 ++-- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cdbe6fe6bb..064cb5d3ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/wcfsetup/install/files/lib/data/event/listener/EventListener.class.php b/wcfsetup/install/files/lib/data/event/listener/EventListener.class.php index 49d91cae7a..0632cebb9e 100644 --- a/wcfsetup/install/files/lib/data/event/listener/EventListener.class.php +++ b/wcfsetup/install/files/lib/data/event/listener/EventListener.class.php @@ -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 + */ + public function getEventNames() { + return explode(',', $this->eventName); + } } 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 7f80b0b3d7..1bc23a7794 100644 --- a/wcfsetup/install/files/lib/system/cache/builder/EventListenerCacheBuilder.class.php +++ b/wcfsetup/install/files/lib/system/cache/builder/EventListenerCacheBuilder.class.php @@ -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; } } diff --git a/wcfsetup/setup/db/install.sql b/wcfsetup/setup/db/install.sql index ebd39536f5..3c298dc5ac 100644 --- a/wcfsetup/setup/db/install.sql +++ b/wcfsetup/setup/db/install.sql @@ -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; -- 2.20.1