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;
|
|
|
|
|
|
2023-04-16 02:46:54 +08:00
|
|
|
use Exception;
|
2022-01-12 14:10:33 +08:00
|
|
|
use Kiri;
|
2022-03-03 18:30:59 +08:00
|
|
|
use Kiri\Abstracts\Component;
|
2022-02-23 16:32:08 +08:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2022-01-09 13:57:10 +08:00
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
|
|
|
|
use Psr\EventDispatcher\StoppableEventInterface;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
class EventDispatch extends Component implements EventDispatcherInterface
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param object $event
|
|
|
|
|
* @return object
|
2022-02-23 16:32:08 +08:00
|
|
|
* @throws ContainerExceptionInterface
|
2023-04-17 01:39:32 +08:00
|
|
|
* @throws NotFoundExceptionInterface|\ReflectionException
|
2022-01-09 13:57:10 +08:00
|
|
|
*/
|
|
|
|
|
public function dispatch(object $event): object
|
|
|
|
|
{
|
2023-04-17 01:39:32 +08:00
|
|
|
$lists = Kiri::getDi()->get(EventProvider::class)->getListenersForEvent($event);
|
2022-06-22 23:57:29 +08:00
|
|
|
if (!$lists->valid()) {
|
|
|
|
|
return $event;
|
|
|
|
|
}
|
2022-06-22 19:16:08 +08:00
|
|
|
$lists->top();
|
|
|
|
|
while ($lists->valid()) {
|
|
|
|
|
try {
|
2022-06-23 01:09:47 +08:00
|
|
|
call_user_func($lists->current(), $event);
|
2022-06-22 19:16:08 +08:00
|
|
|
} catch (\Throwable $exception) {
|
2023-04-16 02:46:54 +08:00
|
|
|
error($exception->getMessage(), [$exception]);
|
2022-06-22 19:16:08 +08:00
|
|
|
}
|
2022-01-09 13:57:10 +08:00
|
|
|
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-06-22 19:16:08 +08:00
|
|
|
$lists->next();
|
2022-01-09 13:57:10 +08:00
|
|
|
}
|
|
|
|
|
return $event;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|