Add `EventHandler::executeListeners()`
authorMatthias Schmidt <gravatronics@live.com>
Thu, 17 Jun 2021 05:03:59 +0000 (07:03 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Thu, 17 Jun 2021 05:03:59 +0000 (07:03 +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 685df35398a4b3c239db8554123d011c1e3dbbe3..003d89aa055d89fc4a8e72c727a6214536b5da37 100644 (file)
@@ -124,18 +124,13 @@ class EventHandler extends SingletonFactory
             }
         }
 
-        // execute actions
-        foreach ($this->inheritedActionsObjects[$name] as $actionObj) {
-            if ($actionObj instanceof IParameterizedEventListener) {
-                $actionObj->execute($eventObj, $className, $eventName, $parameters);
-
-                if (!\is_array($parameters)) {
-                    throw new SystemException("'" . \get_class($actionObj) . "' breaks the '\$parameters' array!");
-                }
-            } elseif ($actionObj instanceof ILegacyEventListener) {
-                $actionObj->execute($eventObj, $className, $eventName);
-            }
-        }
+        $this->executeListeners(
+            $this->inheritedActionsObjects[$name],
+            $eventObj,
+            $className,
+            $eventName,
+            $parameters
+        );
     }
 
     /**
@@ -166,6 +161,30 @@ class EventHandler extends SingletonFactory
         return $object;
     }
 
+    /**
+     * @param   EventListener[]     $eventListeners
+     * @since   5.5
+     */
+    protected function executeListeners(
+        array $eventListeners,
+        $eventObj,
+        string $className,
+        string $eventName,
+        array &$parameters
+    ): void {
+        foreach ($eventListeners as $actionObj) {
+            if ($actionObj instanceof IParameterizedEventListener) {
+                $actionObj->execute($eventObj, $className, $eventName, $parameters);
+
+                if (!\is_array($parameters)) {
+                    throw new SystemException("'" . \get_class($actionObj) . "' breaks the '\$parameters' array!");
+                }
+            } elseif ($actionObj instanceof ILegacyEventListener) {
+                $actionObj->execute($eventObj, $className, $eventName);
+            }
+        }
+    }
+
     /**
      * Executes all registered listeners for the given event.
      *
@@ -221,18 +240,13 @@ class EventHandler extends SingletonFactory
             }
         }
 
-        // execute actions
-        foreach ($this->actionsObjects[$name] as $actionObj) {
-            if ($actionObj instanceof IParameterizedEventListener) {
-                $actionObj->execute($eventObj, $className, $eventName, $parameters);
-
-                if (!\is_array($parameters)) {
-                    throw new SystemException("'" . \get_class($actionObj) . "' breaks the '\$parameters' array!");
-                }
-            } elseif ($actionObj instanceof ILegacyEventListener) {
-                $actionObj->execute($eventObj, $className, $eventName);
-            }
-        }
+        $this->executeListeners(
+            $this->actionsObjects[$name],
+            $eventObj,
+            $className,
+            $eventName,
+            $parameters
+        );
     }
 
     /**