36 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
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
as2252258 a4c50f975e modify plugin name 2022-02-23 16:32:08 +08:00
as2252258 f7630a4c2a Revert "改名"
This reverts commit fdf58326
2022-01-20 19:04:16 +08:00
as2252258 bf3182660b Revert "改名"
This reverts commit fdf58326
2022-01-14 11:29:16 +08:00
as2252258 df7b50563f Revert "改名"
This reverts commit fdf58326
2022-01-12 14:10:33 +08:00
8 changed files with 156 additions and 38 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('@'));
} }
+24 -6
View File
@@ -1,11 +1,12 @@
<?php <?php
declare(strict_types=1);
namespace Kiri\Events; namespace Kiri\Events;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Kiri;
use Psr\EventDispatcher\EventDispatcherInterface; use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\StoppableEventInterface; use Psr\EventDispatcher\StoppableEventInterface;
use SplPriorityQueue;
/** /**
@@ -18,14 +19,31 @@ class EventDispatch extends Component implements EventDispatcherInterface
/** /**
* @param object $event * @param object $event
* @return object * @return object
* @throws \ReflectionException
*/ */
public function dispatch(object $event): object public function dispatch(object $event): object
{ {
$lists = $this->eventProvider->getListenersForEvent($event); $lists = make(EventProvider::class)->getListenersForEvent($event);
foreach ($lists as $listener) {
/** @var Struct $list */ return $this->execute($lists, $event);
$listener($event); }
/**
* @param SplPriorityQueue $lists
* @param object $event
* @return object
*/
public function execute(SplPriorityQueue $lists, object $event): object
{
if ($lists->isEmpty()) {
return $event;
}
foreach ($lists as $item) {
try {
call_user_func($item, $event);
} catch (\Throwable $exception) {
\Kiri::getLogger()->json_log($exception);
}
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) { if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
break; break;
} }
+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"]);
}
}
+28 -5
View File
@@ -1,9 +1,12 @@
<?php <?php
declare(strict_types=1);
namespace Kiri\Events; namespace Kiri\Events;
use Closure;
use Psr\EventDispatcher\ListenerProviderInterface; use Psr\EventDispatcher\ListenerProviderInterface;
use SplPriorityQueue;
/** /**
* *
@@ -17,11 +20,12 @@ class EventProvider implements ListenerProviderInterface
/** /**
* @param object $event * @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. // 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);
@@ -32,13 +36,32 @@ class EventProvider implements ListenerProviderInterface
/** /**
* @param string $event * @param string $event
* @param callable $handler * @param array|Closure|string $handler
* @param int $zOrder * @param int $zOrder
* @throws
*/ */
public function on(string $event, callable $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)) {
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 callable $handler
* @return void
*/
public function off(string $event, callable $handler): void
{
$events = $this->_listeners[$event] ?? [];
$this->_listeners[$event] = array_filter($events, function ($value) use ($handler) {
return $value->listener !== $handler;
});
}
} }
+8 -2
View File
@@ -12,11 +12,17 @@ class Struct
public string $event; public string $event;
public array|\Closure $listener; public array|\Closure|string $listener;
public int $priority; public int $priority;
public function __construct(string $event, callable $listener, int $priority)
/**
* @param string $event
* @param array|\Closure|string $listener
* @param int $priority
*/
public function __construct(string $event, array|\Closure|string $listener, int $priority)
{ {
$this->event = $event; $this->event = $event;
$this->listener = $listener; $this->listener = $listener;
+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": {