2022-01-09 13:57:10 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Events;
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2022-03-03 18:30:59 +08:00
|
|
|
/**
|
|
|
|
|
* @param EventProvider $eventProvider
|
|
|
|
|
* @param array $config
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(public EventProvider $eventProvider, array $config = [])
|
|
|
|
|
{
|
|
|
|
|
parent::__construct($config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 13:57:10 +08:00
|
|
|
/**
|
|
|
|
|
* @param object $event
|
|
|
|
|
* @return object
|
2022-02-23 16:32:08 +08:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
2022-01-09 13:57:10 +08:00
|
|
|
*/
|
|
|
|
|
public function dispatch(object $event): object
|
|
|
|
|
{
|
2022-03-03 18:30:59 +08:00
|
|
|
$lists = $this->eventProvider->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 00:11:06 +08:00
|
|
|
$current = $lists->current();
|
2022-06-23 00:36:23 +08:00
|
|
|
if (is_array($current)) {
|
|
|
|
|
var_dump($current[0]::class, $current[1]);
|
|
|
|
|
} else if (is_string($current)) {
|
|
|
|
|
var_dump($current);
|
|
|
|
|
}
|
2022-06-23 00:11:06 +08:00
|
|
|
call_user_func($current, $event);
|
2022-06-22 19:16:08 +08:00
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
$this->logger->error($exception->getMessage(), [$exception]);
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|