Skip to content
docsv0.1.0

EventDispatcher

Reference implementation of {@see MilpaEventDispatcherInterface}. Central hub for string-named, event-driven communication between plugins: exact and dot-segment wildcard subscriptions, priority ordering, listener error isolation (a throwing handler is logged and does not stop the rest), and a pluggable async (queue) seam — see {@see setAsyncDispatcher()} and the `$async` semantics documented on {@see dispatch()}.

EventDispatcher::__construct()

public function __construct(Psr\Log\LoggerInterface $logger):

Parameters

Parameters of __construct()
NameTypeDescription
$loggerPsr\Log\LoggerInterface

EventDispatcher::setAsyncDispatcher()

public function setAsyncDispatcher(callable $dispatcher): void

Wire the async dispatcher (typically a queue service) that `dispatch(..., async: true)` hands events to. Without this call, `$async=true` degrades to synchronous dispatch — see the `$async` semantics on {@see dispatch()}.

Parameters

Parameters of setAsyncDispatcher()
NameTypeDescription
$dispatchercallablefn(string $eventName, array $payload): void

EventDispatcher::dispatch()

public function dispatch(string $eventName, array $payload = [], bool $async = false): void

{@inheritdoc} `$async` semantics for this implementation, per the MAY/MUST contract on {@see MilpaEventDispatcherInterface::dispatch()}: with an async dispatcher wired via {@see setAsyncDispatcher()}, `$async=true` hands the event to that callable instead of invoking subscribers inline — a conformant queue dispatch, not a fallback. Without one wired, `$async=true` degrades to synchronous dispatch (subscribers run inline, in the same call) — a conformant MAY fallback, never a silent drop; the event is always either queued or run.

Parameters

Parameters of dispatch()
NameTypeDescription
$eventNamestring
$payloadarray
$asyncbool

EventDispatcher::subscribe()

public function subscribe(string $eventName, callable $handler, int $priority = 0): void

{@inheritdoc}

Parameters

Parameters of subscribe()
NameTypeDescription
$eventNamestring
$handlercallable
$priorityint

EventDispatcher::getSubscribers()

public function getSubscribers(string $eventName): array

{@inheritdoc}

Parameters

Parameters of getSubscribers()
NameTypeDescription
$eventNamestring

EventDispatcher::hasSubscribers()

public function hasSubscribers(string $eventName): bool

{@inheritdoc}

Parameters

Parameters of hasSubscribers()
NameTypeDescription
$eventNamestring

EventDispatcher::getRegisteredPatterns()

public function getRegisteredPatterns(): array

Get all registered event patterns (for debugging).