This commit is contained in:
xl
2023-05-25 16:59:18 +08:00
parent 30e9b3e51d
commit a6e5c46844
+14 -15
View File
@@ -10,6 +10,7 @@ use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Psr\EventDispatcher\EventDispatcherInterface; use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\StoppableEventInterface; use Psr\EventDispatcher\StoppableEventInterface;
use ReflectionException;
/** /**
@@ -23,26 +24,24 @@ class EventDispatch extends Component implements EventDispatcherInterface
* @param object $event * @param object $event
* @return object * @return object
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|\ReflectionException * @throws NotFoundExceptionInterface|ReflectionException
*/ */
public function dispatch(object $event): object public function dispatch(object $event): object
{ {
$lists = Kiri::getDi()->get(EventProvider::class)->getListenersForEvent($event); $lists = make(EventProvider::class)->getListenersForEvent($event);
if (!$lists->valid()) { if ($lists->isEmpty()) {
return $event; return $event;
} }
$lists->top(); foreach ($lists as $item) {
while ($lists->valid()) { try {
try { call_user_func($item, $event);
call_user_func($lists->current(), $event); } catch (\Throwable $exception) {
} catch (\Throwable $exception) { error($exception);
error($exception); }
} if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) { break;
break; }
} }
$lists->next();
}
return $event; return $event;
} }