14 Commits

Author SHA1 Message Date
as2252258 29410876e1 变更 2022-06-23 01:09:47 +08:00
as2252258 f22d8463e3 变更 2022-06-23 00:39:31 +08:00
as2252258 af3ed4a235 变更 2022-06-23 00:38:28 +08:00
as2252258 ffefd79006 变更 2022-06-23 00:36:23 +08:00
as2252258 d96aec90b7 变更 2022-06-23 00:11:06 +08:00
as2252258 9580aebffd 变更 2022-06-23 00:09:14 +08:00
as2252258 745308577d 变更 2022-06-23 00:05:23 +08:00
as2252258 6bc22c92a5 变更 2022-06-23 00:00:48 +08:00
as2252258 3bb1398e31 变更 2022-06-22 23:57:29 +08:00
as2252258 af647afb93 modify plugin name 2022-06-22 19:16:08 +08:00
as2252258 acce084300 modify plugin name 2022-06-16 18:58:26 +08:00
as2252258 38f6830190 modify plugin name 2022-06-16 18:13:27 +08:00
as2252258 a44cfb70b1 modify mysql result 2022-04-29 16:43:54 +08:00
as2252258 100ce506ea modify plugin name 2022-03-03 18:30:59 +08:00
3 changed files with 38 additions and 13 deletions
+24 -5
View File
@@ -2,8 +2,8 @@
namespace Kiri\Events;
use Kiri\Abstracts\Component;
use Kiri;
use Kiri\Abstracts\Component;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
@@ -17,6 +17,17 @@ class EventDispatch extends Component implements EventDispatcherInterface
{
/**
* @param EventProvider $eventProvider
* @param array $config
* @throws \Exception
*/
public function __construct(public EventProvider $eventProvider, array $config = [])
{
parent::__construct($config);
}
/**
* @param object $event
* @return object
@@ -25,13 +36,21 @@ class EventDispatch extends Component implements EventDispatcherInterface
*/
public function dispatch(object $event): object
{
$lists = $this->getEventProvider()->getListenersForEvent($event);
foreach ($lists as $listener) {
/** @var Struct $list */
$listener($event);
$lists = $this->eventProvider->getListenersForEvent($event);
if (!$lists->valid()) {
return $event;
}
$lists->top();
while ($lists->valid()) {
try {
call_user_func($lists->current(), $event);
} catch (\Throwable $exception) {
$this->logger->error($exception->getMessage(), [$exception]);
}
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
break;
}
$lists->next();
}
return $event;
}
+12 -6
View File
@@ -4,6 +4,7 @@ namespace Kiri\Events;
use Psr\EventDispatcher\ListenerProviderInterface;
use SplPriorityQueue;
/**
*
@@ -17,11 +18,12 @@ class EventProvider implements ListenerProviderInterface
/**
* @param object $event
* @return iterable
* @return SplPriorityQueue
*/
public function getListenersForEvent(object $event): iterable
public function getListenersForEvent(object $event): SplPriorityQueue
{
$queue = new \SplPriorityQueue();
$queue = new SplPriorityQueue();
$queue->setExtractFlags(SplPriorityQueue::EXTR_DATA);
// TODO: Implement getListenersForEvent() method.
foreach ($this->_listeners[get_class($event)] ?? [] as $listener) {
$queue->insert($listener->listener, $listener->priority);
@@ -32,11 +34,15 @@ class EventProvider implements ListenerProviderInterface
/**
* @param string $event
* @param callable $handler
* @param array|\Closure|string $handler
* @param int $zOrder
* @throws \Exception
*/
public function on(string $event, callable $handler, int $zOrder = 1)
public function on(string $event, array|\Closure|string $handler, int $zOrder = 1)
{
if (is_string($handler) && !is_callable($handler, true)) {
throw new \Exception('Event handler must is execute function.');
}
$this->_listeners[$event][] = new Struct($event, $handler, $zOrder);
}
@@ -46,7 +52,7 @@ class EventProvider implements ListenerProviderInterface
* @param callable $handler
* @return void
*/
public function off(string $event, callable $handler)
public function off(string $event, callable $handler): void
{
$events = $this->_listeners[$event] ?? [];
+2 -2
View File
@@ -12,11 +12,11 @@ class Struct
public string $event;
public array|\Closure $listener;
public array|\Closure|string $listener;
public int $priority;
public function __construct(string $event, callable $listener, int $priority)
public function __construct(string $event, array|\Closure|string $listener, int $priority)
{
$this->event = $event;
$this->listener = $listener;