Files
kiri-event/src/EventDispatch.php
T

47 lines
850 B
PHP
Raw Normal View History

2021-08-25 16:07:04 +08:00
<?php
namespace Kiri\Events;
2021-12-03 15:44:06 +08:00
use Kiri\Abstracts\Component;
2021-12-08 11:32:32 +08:00
use Kiri\Kiri;
2021-08-25 16:07:04 +08:00
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\StoppableEventInterface;
/**
*
*/
2021-12-03 15:44:06 +08:00
class EventDispatch extends Component implements EventDispatcherInterface
2021-08-25 16:07:04 +08:00
{
/**
* @param object $event
* @return object
2021-12-08 11:32:32 +08:00
* @throws \ReflectionException
2021-08-25 16:07:04 +08:00
*/
public function dispatch(object $event): object
{
2021-12-08 11:32:32 +08:00
$lists = $this->provider()->getListenersForEvent($event);
2021-08-25 16:07:04 +08:00
foreach ($lists as $listener) {
/** @var Struct $list */
$listener($event);
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
break;
}
}
return $event;
}
2021-12-08 11:32:32 +08:00
/**
* @return EventProvider
* @throws \ReflectionException
*/
private function provider(): EventProvider
{
return Kiri::getDi()->get(EventProvider::class);
}
2021-08-25 16:07:04 +08:00
}