simplify EventDispatcher@trigger

pull/722/head
fadrian06 3 days ago
parent 3723228c3d
commit f7b2736736

@ -27,27 +27,24 @@ class EventDispatcher
} }
/** /**
* Trigger an event with optional arguments. * @param mixed ...$args Arguments to pass to the listeners.
*
* @param string $event Event name
* @param mixed ...$args Arguments to pass to the callbacks
*
* @return mixed * @return mixed
*/ */
public function trigger(string $event, ...$args) public function trigger(string $event, ...$args)
{ {
$result = null; $listenerReturnValue = null;
if (isset($this->listeners[$event]) === true) {
foreach ($this->listeners[$event] as $callback) { if (isset($this->listeners[$event])) {
$result = call_user_func_array($callback, $args); foreach ($this->listeners[$event] as $listener) {
$listenerReturnValue = $listener(...$args);
// If you return false, it will break the loop and stop the other event listeners. if ($listenerReturnValue === false) {
if ($result === false) { break;
break; // Stop executing further listeners
} }
} }
} }
return $result;
return $listenerReturnValue;
} }
/** /**

Loading…
Cancel
Save