feat: Move event runtime implementation into core
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -4,7 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Console\Event;
|
||||
|
||||
use KTXF\Event\EventListenerRegistry;
|
||||
use KTXC\Event\EventListenerRegistry;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Event;
|
||||
|
||||
interface DeferredEventProcessorInterface
|
||||
{
|
||||
public function beginExecution(string $executionId): void;
|
||||
|
||||
public function processDeferred(string $executionId): DeferredProcessingResult;
|
||||
|
||||
public function discardDeferred(string $executionId): void;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Event;
|
||||
|
||||
final readonly class DeferredProcessingResult
|
||||
{
|
||||
public function __construct(
|
||||
public int $processed,
|
||||
public int $remaining,
|
||||
public bool $deadlineExceeded,
|
||||
public bool $limitExceeded = false,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Event;
|
||||
|
||||
use KTXF\Event\DeliveryMode;
|
||||
use KTXF\Event\Event;
|
||||
use KTXF\Event\EventDispatcherInterface;
|
||||
use KTXF\Event\FailurePolicy;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
final class EventDispatcher implements EventDispatcherInterface, DeferredEventProcessorInterface
|
||||
{
|
||||
/** @var array<string, list<Event>> */
|
||||
private array $deferred = [];
|
||||
private ?string $activeExecution = null;
|
||||
private int $dispatchDepth = 0;
|
||||
|
||||
public function __construct(
|
||||
private readonly EventListenerRegistry $registry,
|
||||
private readonly ContainerInterface $container,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
public function dispatch(Event $event): void
|
||||
{
|
||||
if (++$this->dispatchDepth > 32) {
|
||||
--$this->dispatchDepth;
|
||||
throw new \RuntimeException('Event dispatch recursion limit exceeded.');
|
||||
}
|
||||
|
||||
try {
|
||||
$this->invoke($event, DeliveryMode::Immediate);
|
||||
if ($this->registry->listeners($event->getName(), DeliveryMode::Deferred) !== []) {
|
||||
if ($this->activeExecution === null) {
|
||||
throw new \LogicException('Deferred events require an active execution scope.');
|
||||
}
|
||||
$this->deferred[$this->activeExecution][] = $event;
|
||||
}
|
||||
} finally {
|
||||
--$this->dispatchDepth;
|
||||
}
|
||||
}
|
||||
|
||||
public function beginExecution(string $executionId): void
|
||||
{
|
||||
if ($this->activeExecution !== null) {
|
||||
throw new \LogicException('An event execution scope is already active.');
|
||||
}
|
||||
$this->activeExecution = $executionId;
|
||||
$this->deferred[$executionId] = [];
|
||||
}
|
||||
|
||||
public function processDeferred(string $executionId): DeferredProcessingResult
|
||||
{
|
||||
if ($this->activeExecution !== $executionId) {
|
||||
throw new \LogicException('Cannot process deferred events for an inactive execution.');
|
||||
}
|
||||
|
||||
$processed = 0;
|
||||
$deadline = microtime(true) + 1.0;
|
||||
$deadlineExceeded = false;
|
||||
$limitExceeded = false;
|
||||
while (($event = array_shift($this->deferred[$executionId])) !== null) {
|
||||
if ($processed >= 1000) {
|
||||
$limitExceeded = true;
|
||||
array_unshift($this->deferred[$executionId], $event);
|
||||
break;
|
||||
}
|
||||
if (microtime(true) >= $deadline) {
|
||||
$deadlineExceeded = true;
|
||||
array_unshift($this->deferred[$executionId], $event);
|
||||
break;
|
||||
}
|
||||
$processed += $this->invoke($event, DeliveryMode::Deferred);
|
||||
}
|
||||
|
||||
$remaining = count($this->deferred[$executionId]);
|
||||
unset($this->deferred[$executionId]);
|
||||
$this->activeExecution = null;
|
||||
|
||||
return new DeferredProcessingResult(
|
||||
$processed,
|
||||
$remaining,
|
||||
$deadlineExceeded,
|
||||
$limitExceeded,
|
||||
);
|
||||
}
|
||||
|
||||
public function discardDeferred(string $executionId): void
|
||||
{
|
||||
unset($this->deferred[$executionId]);
|
||||
if ($this->activeExecution === $executionId) {
|
||||
$this->activeExecution = null;
|
||||
}
|
||||
}
|
||||
|
||||
private function invoke(Event $event, DeliveryMode $delivery): int
|
||||
{
|
||||
$processed = 0;
|
||||
foreach ($this->registry->listeners($event->getName(), $delivery) as $listener) {
|
||||
if ($event->isPropagationStopped()) {
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
$service = $this->container->get($listener->service);
|
||||
$service->{$listener->method}($event);
|
||||
$processed++;
|
||||
} catch (\Throwable $error) {
|
||||
$this->logger->error('Event listener failed.', [
|
||||
'event' => $event->getName(),
|
||||
'module' => $listener->module,
|
||||
'listener' => $listener->service . '::' . $listener->method,
|
||||
'exception' => $error,
|
||||
]);
|
||||
if ($listener->failurePolicy === FailurePolicy::Propagate) {
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $processed;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Event;
|
||||
|
||||
use KTXF\Event\DeliveryMode;
|
||||
use KTXF\Event\FailurePolicy;
|
||||
|
||||
final readonly class EventListenerDefinition
|
||||
{
|
||||
/**
|
||||
* @param class-string $service
|
||||
*/
|
||||
public function __construct(
|
||||
public string $module,
|
||||
public string $event,
|
||||
public string $service,
|
||||
public string $method,
|
||||
public DeliveryMode $delivery,
|
||||
public int $priority,
|
||||
public FailurePolicy $failurePolicy,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Event;
|
||||
|
||||
use KTXF\Event\DeliveryMode;
|
||||
use KTXF\Event\EventListenerRegistrarInterface;
|
||||
use KTXF\Event\FailurePolicy;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
final class EventListenerRegistry implements EventListenerRegistrarInterface
|
||||
{
|
||||
/** @var array<string, list<EventListenerDefinition>> */
|
||||
private array $listeners = [];
|
||||
/** @var array<string, true> */
|
||||
private array $registrationIds = [];
|
||||
private bool $frozen = false;
|
||||
|
||||
/**
|
||||
* @param class-string $service
|
||||
*/
|
||||
public function listen(
|
||||
string $module,
|
||||
string $event,
|
||||
string $service,
|
||||
string $method,
|
||||
DeliveryMode $delivery = DeliveryMode::Immediate,
|
||||
int $priority = 0,
|
||||
FailurePolicy $failurePolicy = FailurePolicy::Continue,
|
||||
): void {
|
||||
if ($this->frozen) {
|
||||
throw new \LogicException('The event listener registry is frozen.');
|
||||
}
|
||||
if ($module === '' || $event === '' || $service === '' || $method === '') {
|
||||
throw new \InvalidArgumentException('Event listener registrations require module, event, service, and method.');
|
||||
}
|
||||
if (!class_exists($service) || !method_exists($service, $method)) {
|
||||
throw new \InvalidArgumentException("Invalid event listener {$service}::{$method}.");
|
||||
}
|
||||
if ($priority < -10000 || $priority > 10000) {
|
||||
throw new \InvalidArgumentException('Event listener priority must be between -10000 and 10000.');
|
||||
}
|
||||
|
||||
$id = implode('|', [$module, $event, $service, $method, $delivery->value]);
|
||||
if (isset($this->registrationIds[$id])) {
|
||||
throw new \LogicException("Duplicate event listener registration: {$id}.");
|
||||
}
|
||||
$this->registrationIds[$id] = true;
|
||||
|
||||
$this->listeners[$event][] = new EventListenerDefinition(
|
||||
$module,
|
||||
$event,
|
||||
$service,
|
||||
$method,
|
||||
$delivery,
|
||||
$priority,
|
||||
$failurePolicy,
|
||||
);
|
||||
}
|
||||
|
||||
public function freeze(?ContainerInterface $container = null): void
|
||||
{
|
||||
foreach ($this->listeners as &$listeners) {
|
||||
if ($container !== null) {
|
||||
foreach ($listeners as $listener) {
|
||||
if (!$container->has($listener->service)) {
|
||||
throw new \LogicException(
|
||||
"Event listener service is not resolvable: {$listener->service}.",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
usort(
|
||||
$listeners,
|
||||
static fn(EventListenerDefinition $a, EventListenerDefinition $b): int =>
|
||||
$b->priority <=> $a->priority,
|
||||
);
|
||||
}
|
||||
unset($listeners);
|
||||
$this->frozen = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<EventListenerDefinition>
|
||||
*/
|
||||
public function listeners(string $event, DeliveryMode $delivery): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->listeners[$event] ?? [],
|
||||
static fn(EventListenerDefinition $listener): bool => $listener->delivery === $delivery,
|
||||
));
|
||||
}
|
||||
|
||||
public function frozen(): bool
|
||||
{
|
||||
return $this->frozen;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<EventListenerDefinition>
|
||||
*/
|
||||
public function definitions(): array
|
||||
{
|
||||
return array_merge(...array_values($this->listeners));
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -27,11 +27,11 @@ use KTXC\Module\ModuleManager;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use KTXC\Logger\LoggerFactory;
|
||||
use KTXC\Logger\TenantAwareLogger;
|
||||
use KTXF\Event\DeferredEventProcessorInterface;
|
||||
use KTXF\Event\EventDispatcher;
|
||||
use KTXC\Event\DeferredEventProcessorInterface;
|
||||
use KTXC\Event\EventDispatcher;
|
||||
use KTXC\Event\EventListenerRegistry;
|
||||
use KTXF\Event\EventDispatcherInterface;
|
||||
use KTXF\Event\EventListenerRegistrarInterface;
|
||||
use KTXF\Event\EventListenerRegistry;
|
||||
use KTXF\Cache\EphemeralCacheInterface;
|
||||
use KTXF\Cache\PersistentCacheInterface;
|
||||
use KTXF\Cache\BlobCacheInterface;
|
||||
|
||||
Reference in New Issue
Block a user