18 Commits

Author SHA1 Message Date
as2252258 6a6096ea11 eee 2026-06-12 23:57:22 +08:00
as2252258 b2a89a6f72 eee 2026-01-09 01:10:20 +08:00
as2252258 c08c1731d1 eee 2026-01-09 01:06:50 +08:00
as2252258 846e195efa eee 2025-12-31 00:19:30 +08:00
as2252258 85df6626c1 eee 2025-07-14 15:35:45 +08:00
as2252258 79f430d898 eee 2024-10-09 16:01:22 +08:00
as2252258 7f5c4127e1 eee 2023-12-12 15:35:36 +08:00
as2252258 f22a2a70b6 eee 2023-12-12 10:56:44 +08:00
as2252258 4ec09dcafd eee 2023-10-24 17:22:30 +08:00
as2252258 34bae8fc44 eee 2023-10-17 21:19:09 +08:00
as2252258 04d2d3094b eee 2023-10-17 21:18:52 +08:00
as2252258 ca5ce86ee3 qqq 2023-08-11 00:12:33 +08:00
as2252258 a6e5c46844 qqq 2023-05-25 16:59:18 +08:00
as2252258 30e9b3e51d 变更 2023-04-18 23:47:31 +08:00
as2252258 e035b6248e 变更 2023-04-17 01:39:32 +08:00
as2252258 0e4d0efdb3 变更 2023-04-16 02:46:54 +08:00
as2252258 64d4767a66 变更 2023-04-16 01:45:34 +08:00
as2252258 e56458f2dc 变更 2023-04-03 11:08:11 +08:00
8 changed files with 148 additions and 73 deletions
+1 -1
View File
@@ -12,6 +12,6 @@ namespace PHPSTORM_META {
// override(\make(0), map('@')); // override(\make(0), map('@'));
override(\di(0), map('@')); override(\di(0), map('@'));
override(\duplicate(0), map('@')); override(\duplicate(0), map('@'));
override(Context::getContext(0), map('@')); override(Context::get(0), map('@'));
} }
+26 -30
View File
@@ -1,13 +1,12 @@
<?php <?php
declare(strict_types=1);
namespace Kiri\Events; namespace Kiri\Events;
use Kiri;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\EventDispatcher\EventDispatcherInterface; use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\StoppableEventInterface; use Psr\EventDispatcher\StoppableEventInterface;
use SplPriorityQueue;
/** /**
@@ -18,40 +17,37 @@ class EventDispatch extends Component implements EventDispatcherInterface
/** /**
* @param EventProvider $eventProvider * @param object $event
* @param array $config * @return object
* @throws \Exception */
*/ public function dispatch(object $event): object
public function __construct(public EventProvider $eventProvider, array $config = [])
{ {
parent::__construct($config); $lists = make(EventProvider::class)->getListenersForEvent($event);
return $this->execute($lists, $event);
} }
/** /**
* @param object $event * @param SplPriorityQueue $lists
* @return object * @param object $event
* @throws ContainerExceptionInterface * @return object
* @throws NotFoundExceptionInterface */
*/ public function execute(SplPriorityQueue $lists, object $event): object
public function dispatch(object $event): object
{ {
$lists = $this->eventProvider->getListenersForEvent($event); if ($lists->isEmpty()) {
if (!$lists->valid()) {
return $event; return $event;
} }
$lists->top(); foreach ($lists as $item) {
while ($lists->valid()) { try {
try { call_user_func($item, $event);
call_user_func($lists->current(), $event); } catch (\Throwable $exception) {
} catch (\Throwable $exception) { \Kiri::getLogger()->json_log($exception);
$this->logger->error($exception->getMessage(), [$exception]); }
} if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) { break;
break; }
} }
$lists->next();
}
return $event; return $event;
} }
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace Kiri\Events;
interface EventInterface
{
/**
* @param object $event
* @return void
*/
public function process(object $event): void;
}
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace Kiri\Events;
use Exception;
use Kiri\Di\Interface\InjectTargetInterface;
#[\Attribute]
readonly class EventListener implements InjectTargetInterface
{
/**
* @param string $event
* @throws Exception
*/
public function __construct(public string $event)
{
}
/**
* @param string $class
* @return void
* @throws Exception
*/
public function dispatch(string $class): void
{
// TODO: Implement dispatch() method.
$target = \Kiri::getDi()->getReflectionClass($class)->newInstanceWithoutConstructor();
if (!($target instanceof EventInterface)) {
throw new Exception("Event listen must implement " . EventInterface::class);
}
on($this->event, [$target, "process"]);
}
}
+43 -41
View File
@@ -1,8 +1,10 @@
<?php <?php
declare(strict_types=1);
namespace Kiri\Events; namespace Kiri\Events;
use Closure;
use Psr\EventDispatcher\ListenerProviderInterface; use Psr\EventDispatcher\ListenerProviderInterface;
use SplPriorityQueue; use SplPriorityQueue;
@@ -12,54 +14,54 @@ use SplPriorityQueue;
class EventProvider implements ListenerProviderInterface class EventProvider implements ListenerProviderInterface
{ {
/** @var Struct[] */ /** @var Struct[] */
private array $_listeners = []; private array $_listeners = [];
/** /**
* @param object $event * @param object $event
* @return SplPriorityQueue * @return SplPriorityQueue
*/ */
public function getListenersForEvent(object $event): SplPriorityQueue public function getListenersForEvent(object $event): SplPriorityQueue
{ {
$queue = new SplPriorityQueue(); $queue = new SplPriorityQueue();
$queue->setExtractFlags(SplPriorityQueue::EXTR_DATA); $queue->setExtractFlags(SplPriorityQueue::EXTR_DATA);
// TODO: Implement getListenersForEvent() method. // TODO: Implement getListenersForEvent() method.
foreach ($this->_listeners[get_class($event)] ?? [] as $listener) { foreach ($this->_listeners[get_class($event)] ?? [] as $listener) {
$queue->insert($listener->listener, $listener->priority); $queue->insert($listener->listener, $listener->priority);
} }
return $queue; return $queue;
} }
/** /**
* @param string $event * @param string $event
* @param array|\Closure|string $handler * @param array|Closure|string $handler
* @param int $zOrder * @param int $zOrder
* @throws \Exception * @throws
*/ */
public function on(string $event, array|\Closure|string $handler, int $zOrder = 1) public function on(string $event, array|Closure|string $handler, int $zOrder = 1): void
{ {
if (is_string($handler) && !is_callable($handler, true)) { if (is_string($handler) && !is_callable($handler, true)) {
throw new \Exception('Event handler must is execute function.'); throw new \Exception('Event handler must is execute function.');
} }
$this->_listeners[$event][] = new Struct($event, $handler, $zOrder); $this->_listeners[$event][] = new Struct($event, $handler, $zOrder);
} }
/** /**
* @param string $event * @param string $event
* @param callable $handler * @param callable $handler
* @return void * @return void
*/ */
public function off(string $event, callable $handler): void public function off(string $event, callable $handler): void
{ {
$events = $this->_listeners[$event] ?? []; $events = $this->_listeners[$event] ?? [];
$this->_listeners[$event] = array_filter($events, function ($value) use ($handler) { $this->_listeners[$event] = array_filter($events, function ($value) use ($handler) {
return $value->listener !== $handler; return $value->listener !== $handler;
}); });
} }
} }
+6
View File
@@ -16,6 +16,12 @@ class Struct
public int $priority; public int $priority;
/**
* @param string $event
* @param array|\Closure|string $listener
* @param int $priority
*/
public function __construct(string $event, array|\Closure|string $listener, int $priority) public function __construct(string $event, array|\Closure|string $listener, int $priority)
{ {
$this->event = $event; $this->event = $event;
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Kiri\Events;
class TestListener implements EventInterface
{
/**
* @param object $event
* @return void
*/
public function process(object $event): void
{
// TODO: Implement process() method.
}
}
+1 -1
View File
@@ -9,7 +9,7 @@
], ],
"license": "MIT", "license": "MIT",
"require": { "require": {
"php": ">=8.0", "php": ">=8.5",
"psr/event-dispatcher": "^1.0" "psr/event-dispatcher": "^1.0"
}, },
"autoload": { "autoload": {