Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b2a89a6f72 | |||
| c08c1731d1 | |||
| 846e195efa | |||
| 85df6626c1 | |||
| 79f430d898 | |||
| 7f5c4127e1 | |||
| f22a2a70b6 | |||
| 4ec09dcafd | |||
| 34bae8fc44 | |||
| 04d2d3094b | |||
| ca5ce86ee3 | |||
| a6e5c46844 | |||
| 30e9b3e51d | |||
| e035b6248e | |||
| 0e4d0efdb3 | |||
| 64d4767a66 | |||
| e56458f2dc | |||
| 29410876e1 | |||
| f22d8463e3 | |||
| af3ed4a235 | |||
| ffefd79006 | |||
| d96aec90b7 | |||
| 9580aebffd | |||
| 745308577d | |||
| 6bc22c92a5 | |||
| 3bb1398e31 | |||
| af647afb93 | |||
| acce084300 | |||
| 38f6830190 | |||
| a44cfb70b1 | |||
| 100ce506ea | |||
| a4c50f975e | |||
| f7630a4c2a | |||
| bf3182660b | |||
| df7b50563f | |||
| b5f20d7d4b | |||
| 244e84772e | |||
| 9529907710 | |||
| 2c31bea602 | |||
| fd01c8b33c |
+2
-2
@@ -3,7 +3,7 @@
|
||||
namespace PHPSTORM_META {
|
||||
|
||||
// Reflect
|
||||
use Http\Context\Context;
|
||||
use Kiri\Message\Context\Context;
|
||||
use Kiri\Di\Container;
|
||||
|
||||
override(Container::get(0), map('@'));
|
||||
@@ -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('@'));
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
use Kiri\Abstracts\Component;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Psr\EventDispatcher\StoppableEventInterface;
|
||||
use SplPriorityQueue;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class EventDispatch extends Component implements EventDispatcherInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param object $event
|
||||
* @return object
|
||||
*/
|
||||
public function dispatch(object $event): object
|
||||
{
|
||||
$lists = make(EventProvider::class)->getListenersForEvent($event);
|
||||
|
||||
return $this->execute($lists, $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()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $event;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
interface EventInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param object $event
|
||||
* @return void
|
||||
*/
|
||||
public function process(object $event): void;
|
||||
|
||||
}
|
||||
@@ -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"]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Psr\EventDispatcher\ListenerProviderInterface;
|
||||
use SplPriorityQueue;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class EventProvider implements ListenerProviderInterface
|
||||
{
|
||||
|
||||
/** @var Struct[] */
|
||||
private array $_listeners = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param object $event
|
||||
* @return SplPriorityQueue
|
||||
*/
|
||||
public function getListenersForEvent(object $event): 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);
|
||||
}
|
||||
return $queue;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $event
|
||||
* @param array|Closure|string $handler
|
||||
* @param int $zOrder
|
||||
* @throws
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Struct
|
||||
{
|
||||
|
||||
public string $event;
|
||||
|
||||
public array|\Closure|string $listener;
|
||||
|
||||
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;
|
||||
$this->listener = $listener;
|
||||
$this->priority = $priority;
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
}
|
||||
|
||||
}
|
||||
+2
-4
@@ -9,16 +9,14 @@
|
||||
],
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"ext-json": "*",
|
||||
"php": ">=8.4",
|
||||
"psr/event-dispatcher": "^1.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Kiri\\Events\\": "src/"
|
||||
"Kiri\\Events\\": "./"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
"kwn/php-rdkafka-stubs": "^2.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Kiri;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Psr\EventDispatcher\StoppableEventInterface;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class EventDispatch extends Component implements EventDispatcherInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param object $event
|
||||
* @return object
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function dispatch(object $event): object
|
||||
{
|
||||
$lists = $this->provider()->getListenersForEvent($event);
|
||||
foreach ($lists as $listener) {
|
||||
/** @var Struct $list */
|
||||
$listener($event);
|
||||
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $event;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return EventProvider
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private function provider(): EventProvider
|
||||
{
|
||||
return Kiri::getDi()->get(EventProvider::class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
|
||||
use Psr\EventDispatcher\ListenerProviderInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class EventProvider implements ListenerProviderInterface
|
||||
{
|
||||
|
||||
/** @var Struct[] */
|
||||
private array $_listeners = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param object $event
|
||||
* @return iterable
|
||||
*/
|
||||
public function getListenersForEvent(object $event): iterable
|
||||
{
|
||||
$queue = new \SplPriorityQueue();
|
||||
// TODO: Implement getListenersForEvent() method.
|
||||
foreach ($this->_listeners[get_class($event)] ?? [] as $listener) {
|
||||
$queue->insert($listener->listener, $listener->priority);
|
||||
}
|
||||
return $queue;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $event
|
||||
* @param callable $handler
|
||||
* @param int $zOrder
|
||||
*/
|
||||
public function on(string $event, callable $handler, int $zOrder = 1)
|
||||
{
|
||||
$this->_listeners[$event][] = new Struct($event, $handler, $zOrder);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Struct
|
||||
{
|
||||
|
||||
public string $event;
|
||||
|
||||
public array|\Closure $listener;
|
||||
|
||||
public int $priority;
|
||||
|
||||
public function __construct(string $event, callable $listener, int $priority)
|
||||
{
|
||||
$this->event = $event;
|
||||
$this->listener = $listener;
|
||||
$this->priority = $priority;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user