Files
kiri-event/EventDispatch.php
T

56 lines
1.1 KiB
PHP
Raw Permalink Normal View History

2022-01-09 13:57:10 +08:00
<?php
2023-04-16 01:45:34 +08:00
declare(strict_types=1);
2022-01-09 13:57:10 +08:00
namespace Kiri\Events;
2022-03-03 18:30:59 +08:00
use Kiri\Abstracts\Component;
2022-01-09 13:57:10 +08:00
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\StoppableEventInterface;
2023-10-17 21:18:52 +08:00
use SplPriorityQueue;
2022-01-09 13:57:10 +08:00
/**
*
*/
class EventDispatch extends Component implements EventDispatcherInterface
{
/**
* @param object $event
* @return object
2023-10-17 21:18:52 +08:00
*/
2022-01-09 13:57:10 +08:00
public function dispatch(object $event): object
{
2023-05-25 16:59:18 +08:00
$lists = make(EventProvider::class)->getListenersForEvent($event);
2023-10-17 21:18:52 +08:00
return $this->execute($lists, $event);
}
/**
* @param SplPriorityQueue $lists
* @param object $event
* @return object
*/
public function execute(SplPriorityQueue $lists, object $event): object
{
2023-05-25 16:59:18 +08:00
if ($lists->isEmpty()) {
2022-06-22 23:57:29 +08:00
return $event;
}
2023-05-25 16:59:18 +08:00
foreach ($lists as $item) {
try {
call_user_func($item, $event);
} catch (\Throwable $exception) {
error($exception);
}
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
break;
}
}
2022-01-09 13:57:10 +08:00
return $event;
}
}