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(\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('@'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-18
@@ -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,39 +17,36 @@ class EventDispatch extends Component implements EventDispatcherInterface
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param EventProvider $eventProvider
|
* @param object $event
|
||||||
* @param array $config
|
* @return object
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
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
|
* @param object $event
|
||||||
* @return object
|
* @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->isEmpty()) {
|
||||||
if (!$lists->valid()) {
|
|
||||||
return $event;
|
return $event;
|
||||||
}
|
}
|
||||||
$lists->top();
|
foreach ($lists as $item) {
|
||||||
while ($lists->valid()) {
|
|
||||||
try {
|
try {
|
||||||
call_user_func($lists->current(), $event);
|
call_user_func($item, $event);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$this->logger->error($exception->getMessage(), [$exception]);
|
error($exception);
|
||||||
}
|
}
|
||||||
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
|
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$lists->next();
|
|
||||||
}
|
}
|
||||||
return $event;
|
return $event;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Events;
|
||||||
|
|
||||||
|
interface EventInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function process(): void;
|
||||||
|
|
||||||
|
}
|
||||||
+5
-3
@@ -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;
|
||||||
|
|
||||||
@@ -34,11 +36,11 @@ class EventProvider implements ListenerProviderInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @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.');
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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