Add `EventHandler::getListenerObject()`
authorMatthias Schmidt <gravatronics@live.com>
Thu, 17 Jun 2021 04:52:22 +0000 (06:52 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Thu, 17 Jun 2021 04:52:22 +0000 (06:52 +0200)
Instead of having the same code in `EventHandler::executeInheritedActions()` and `EventHandler::fireAction()`, it is now just in one method.

wcfsetup/install/files/lib/system/event/EventHandler.class.php

index b64b40c75905fef167d374b2659dc947ed60203d..a533771a79cfdc9ac72ba01aff7d346624e52e86 100644 (file)
@@ -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);
                 }
             }
         }