12 Commits

Author SHA1 Message Date
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
6 changed files with 106 additions and 72 deletions
+1 -1
View File
@@ -12,6 +12,6 @@ namespace PHPSTORM_META {
// override(\make(0), map('@'));
override(\di(0), map('@'));
override(\duplicate(0), map('@'));
override(Context::getContext(0), map('@'));
override(Context::get(0), map('@'));
}
+14 -18
View File
@@ -1,13 +1,12 @@
<?php
declare(strict_types=1);
namespace Kiri\Events;
use Kiri;
use Kiri\Abstracts\Component;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\StoppableEventInterface;
use SplPriorityQueue;
/**
@@ -18,39 +17,36 @@ class EventDispatch extends Component implements EventDispatcherInterface
/**
* @param EventProvider $eventProvider
* @param array $config
* @throws \Exception
* @param object $event
* @return object
*/
public function __construct(public EventProvider $eventProvider, array $config = [])
public function dispatch(object $event): object
{
parent::__construct($config);
$lists = make(EventProvider::class)->getListenersForEvent($event);
return $this->execute($lists, $event);
}
/**
* @param SplPriorityQueue $lists
* @param object $event
* @return object
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function dispatch(object $event): object
public function execute(SplPriorityQueue $lists, object $event): object
{
$lists = $this->eventProvider->getListenersForEvent($event);
if (!$lists->valid()) {
if ($lists->isEmpty()) {
return $event;
}
$lists->top();
while ($lists->valid()) {
foreach ($lists as $item) {
try {
call_user_func($lists->current(), $event);
call_user_func($item, $event);
} catch (\Throwable $exception) {
$this->logger->error($exception->getMessage(), [$exception]);
error($exception);
}
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
break;
}
$lists->next();
}
return $event;
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Kiri\Events;
interface EventInterface
{
/**
* @return void
*/
public function process(): void;
}
+5 -3
View File
@@ -1,8 +1,10 @@
<?php
declare(strict_types=1);
namespace Kiri\Events;
use Closure;
use Psr\EventDispatcher\ListenerProviderInterface;
use SplPriorityQueue;
@@ -34,11 +36,11 @@ class EventProvider implements ListenerProviderInterface
/**
* @param string $event
* @param array|\Closure|string $handler
* @param array|Closure|string $handler
* @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)) {
throw new \Exception('Event handler must is execute function.');
+6
View File
@@ -16,6 +16,12 @@ class Struct
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)
{
$this->event = $event;
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace Kiri\Events;
class TestListener implements EventInterface
{
/**
* @return void
*/
public function process(): void
{
// TODO: Implement process() method.
}
}