Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7f5c4127e1 | |||
| f22a2a70b6 | |||
| 4ec09dcafd | |||
| 34bae8fc44 | |||
| 04d2d3094b | |||
| ca5ce86ee3 | |||
| a6e5c46844 | |||
| 30e9b3e51d | |||
| e035b6248e | |||
| 0e4d0efdb3 | |||
| 64d4767a66 | |||
| e56458f2dc |
+1
-1
@@ -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('@'));
|
||||
|
||||
}
|
||||
|
||||
+26
-30
@@ -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,40 +17,37 @@ class EventDispatch extends Component implements EventDispatcherInterface
|
||||
|
||||
|
||||
/**
|
||||
* @param EventProvider $eventProvider
|
||||
* @param array $config
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(public EventProvider $eventProvider, array $config = [])
|
||||
* @param object $event
|
||||
* @return object
|
||||
*/
|
||||
public function dispatch(object $event): object
|
||||
{
|
||||
parent::__construct($config);
|
||||
$lists = make(EventProvider::class)->getListenersForEvent($event);
|
||||
|
||||
return $this->execute($lists, $event);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param object $event
|
||||
* @return object
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function dispatch(object $event): object
|
||||
/**
|
||||
* @param SplPriorityQueue $lists
|
||||
* @param object $event
|
||||
* @return 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()) {
|
||||
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();
|
||||
}
|
||||
foreach ($lists as $item) {
|
||||
try {
|
||||
call_user_func($item, $event);
|
||||
} catch (\Throwable $exception) {
|
||||
error($exception);
|
||||
}
|
||||
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $event;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
interface EventInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function process(): void;
|
||||
|
||||
}
|
||||
+43
-41
@@ -1,8 +1,10 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Psr\EventDispatcher\ListenerProviderInterface;
|
||||
use SplPriorityQueue;
|
||||
|
||||
@@ -12,54 +14,54 @@ use SplPriorityQueue;
|
||||
class EventProvider implements ListenerProviderInterface
|
||||
{
|
||||
|
||||
/** @var Struct[] */
|
||||
private array $_listeners = [];
|
||||
/** @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 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 \Exception
|
||||
*/
|
||||
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);
|
||||
}
|
||||
/**
|
||||
* @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] ?? [];
|
||||
/**
|
||||
* @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;
|
||||
});
|
||||
}
|
||||
$this->_listeners[$event] = array_filter($events, function ($value) use ($handler) {
|
||||
return $value->listener !== $handler;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
class TestListener implements EventInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function process(): void
|
||||
{
|
||||
// TODO: Implement process() method.
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user