改名
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Kiri\Abstracts\BaseObject;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class EventDispatch extends BaseObject
|
||||
{
|
||||
|
||||
#[Inject(EventProvider::class)]
|
||||
public EventProvider $eventProvider;
|
||||
|
||||
|
||||
/**
|
||||
* @param object $triggerEvent
|
||||
* @return object
|
||||
*/
|
||||
public function dispatch(object $triggerEvent): object
|
||||
{
|
||||
$lists = $this->eventProvider->getListenersForEvent($triggerEvent);
|
||||
foreach ($lists as $listener) {
|
||||
/** @var Struct $list */
|
||||
$listener($triggerEvent);
|
||||
if ($triggerEvent instanceof StoppableEventInterface && $triggerEvent->isPropagationStopped()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $triggerEvent;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
interface EventInterface
|
||||
{
|
||||
|
||||
|
||||
public function process(): void;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class EventProvider implements EventProviders
|
||||
{
|
||||
|
||||
/** @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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
interface EventProviders
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param object $event
|
||||
* An event for which to return the relevant listeners.
|
||||
* @return iterable<callable>
|
||||
* An iterable (array, iterator, or generator) of callables. Each
|
||||
* callable MUST be type-compatible with $event.
|
||||
*/
|
||||
public function getListenersForEvent(object $event): iterable;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Events;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
interface StoppableEventInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Is propagation stopped?
|
||||
*
|
||||
* This will typically only be used by the Dispatcher to determine if the
|
||||
* previous listener halted propagation.
|
||||
*
|
||||
* @return bool
|
||||
* True if the Event is complete and no further listeners should be called.
|
||||
* False to continue calling listeners.
|
||||
*/
|
||||
public function isPropagationStopped() : bool;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?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