Files
kiri-event/EventDispatch.php
T
2023-10-17 21:18:52 +08:00

61 lines
1.3 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;
use ReflectionException;
use SplPriorityQueue;
/**
*
*/
class EventDispatch extends Component implements EventDispatcherInterface
{
/**
* @param object $event
* @return object
*/
public function dispatch(object $event): object
{
$lists = make(EventProvider::class)->getListenersForEvent($event);
return $this->execute($lists, $event);
}
/**
* @param SplPriorityQueue $lists
* @param object $event
* @return object
*/
public function execute(SplPriorityQueue $lists, object $event): object
{
if ($lists->isEmpty()) {
return $event;
}
foreach ($lists as $item) {
try {
call_user_func($item, $event);
} catch (\Throwable $exception) {
error($exception);
}
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
break;
}
}
return $event;
}
}