/** * @param $eventName * @param EventInterface|null $event * @return Event * @throws Exception */ public function dispatch($eventName, EventInterface $event = null) { if ($this->hasListeners($eventName)) { if ($event === null) { $event = new Event(); } foreach ($this->getListeners($eventName) as $listener) { if (is_callable($listener)) { call_user_func_array($listener, [$event, $eventName, $this]); if ($event->isStoped()) { break; } } else { throw new Exception('Listener is not callable'); } } } return $event; }