From 86f907fb445f4164346e0dccda7bcb9f34674ebb Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Thu, 17 Jun 2021 06:52:22 +0200 Subject: [PATCH] Add `EventHandler::getListenerObject()` Instead of having the same code in `EventHandler::executeInheritedActions()` and `EventHandler::fireAction()`, it is now just in one method. --- .../lib/system/event/EventHandler.class.php | 89 +++++++------------ 1 file changed, 30 insertions(+), 59 deletions(-) diff --git a/wcfsetup/install/files/lib/system/event/EventHandler.class.php b/wcfsetup/install/files/lib/system/event/EventHandler.class.php index b64b40c759..a533771a79 100644 --- a/wcfsetup/install/files/lib/system/event/EventHandler.class.php +++ b/wcfsetup/install/files/lib/system/event/EventHandler.class.php @@ -117,42 +117,7 @@ class EventHandler extends SingletonFactory continue; } - // get class object - if (isset($this->listenerObjects[$eventListener->listenerClassName])) { - $object = $this->listenerObjects[$eventListener->listenerClassName]; - } else { - $object = null; - // instance action object - if (!\class_exists($eventListener->listenerClassName)) { - throw new SystemException("Unable to find class '" . $eventListener->listenerClassName . "'"); - } - if ( - !\is_subclass_of( - $eventListener->listenerClassName, - IParameterizedEventListener::class - ) - ) { - // legacy event listeners - if ( - !\is_subclass_of( - $eventListener->listenerClassName, - IEventListener::class - ) - ) { - throw new ImplementationException( - $eventListener->listenerClassName, - IParameterizedEventListener::class - ); - } - } - - $object = new $eventListener->listenerClassName(); - $this->listenerObjects[$eventListener->listenerClassName] = $object; - } - - if ($object !== null) { - $this->inheritedActionsObjects[$name][$eventListener->listenerClassName] = $object; - } + $this->inheritedActionsObjects[$name][$eventListener->listenerClassName] = $this->getListenerObject($eventListener); } } } @@ -174,6 +139,34 @@ class EventHandler extends SingletonFactory } } + /** + * @since 5.5 + */ + protected function getListenerObject(EventListener $eventListener): object + { + if (isset($this->listenerObjects[$eventListener->listenerClassName])) { + return $this->listenerObjects[$eventListener->listenerClassName]; + } + + if (!\class_exists($eventListener->listenerClassName)) { + throw new SystemException("Unable to find class '" . $eventListener->listenerClassName . "'"); + } + if ( + !\is_subclass_of($eventListener->listenerClassName, IParameterizedEventListener::class) + && !\is_subclass_of($eventListener->listenerClassName, IEventListener::class) + ) { + throw new ImplementationException( + $eventListener->listenerClassName, + IParameterizedEventListener::class + ); + } + + $object = new $eventListener->listenerClassName(); + $this->listenerObjects[$eventListener->listenerClassName] = $object; + + return $object; + } + /** * Executes all registered listeners for the given event. * @@ -224,29 +217,7 @@ class EventHandler extends SingletonFactory continue; } - // get class object - if (isset($this->listenerObjects[$eventListener->listenerClassName])) { - $object = $this->listenerObjects[$eventListener->listenerClassName]; - } else { - // instance action object - if (!\class_exists($eventListener->listenerClassName)) { - throw new SystemException("Unable to find class '" . $eventListener->listenerClassName . "'"); - } - if (!\is_subclass_of($eventListener->listenerClassName, IParameterizedEventListener::class)) { - // legacy event listeners - if (!\is_subclass_of($eventListener->listenerClassName, IEventListener::class)) { - throw new ImplementationException( - $eventListener->listenerClassName, - IParameterizedEventListener::class - ); - } - } - - $object = new $eventListener->listenerClassName(); - $this->listenerObjects[$eventListener->listenerClassName] = $object; - } - - $this->actionsObjects[$name][$eventListener->listenerClassName] = $object; + $this->actionsObjects[$name][$eventListener->listenerClassName] = $this->getListenerObject($eventListener); } } } -- 2.20.1