Files
kiri-event/EventDispatch.php
T
2023-04-16 02:46:54 +08:00

61 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Kiri\Events;
use Exception;
use Kiri;
use Kiri\Abstracts\Component;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\StoppableEventInterface;
/**
*
*/
class EventDispatch extends Component implements EventDispatcherInterface
{
/**
* @param EventProvider $eventProvider
* @throws Exception
*/
public function __construct(public EventProvider $eventProvider)
{
parent::__construct();
}
/**
* @param object $event
* @return object
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function dispatch(object $event): object
{
$lists = $this->eventProvider->getListenersForEvent($event);
if (!$lists->valid()) {
return $event;
}
$lists->top();
while ($lists->valid()) {
try {
call_user_func($lists->current(), $event);
} catch (\Throwable $exception) {
error($exception->getMessage(), [$exception]);
}
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
break;
}
$lists->next();
}
return $event;
}
}