Skip to content
docs

MilpaEventDispatcherInterface

Event Dispatcher Interface Enables plugins to emit and subscribe to events in a loosely-coupled manner.

MilpaEventDispatcherInterface::dispatch()

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

Dispatch an event to all registered subscribers (exact-match plus any matching wildcard subscriptions), in descending priority order. Listener error isolation: if a handler throws, the dispatcher logs the error and continues to the remaining handlers — one failing listener MUST NOT abort the dispatch or prevent later listeners from running. (An implementation that needs fail-fast semantics must document the deviation.)

Parameters

Parámetros de dispatch()
NombreTipoDescripción
$eventNamestringEvent name (e.g., 'user.registered', 'order.shipped')
$payloadarray<string, mixed>Data to pass to handlers
$asyncboolIf true, dispatch via queue for deferred execution

MilpaEventDispatcherInterface::subscribe()

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

Subscribe a handler to an event name or a wildcard pattern. Wildcard grammar: event names are dot-separated segments; `*` matches exactly ONE segment (it does not span a `.`). Matching is case-sensitive and anchored (the whole name must match). Examples: `user.*` matches `user.created`/`user.deleted` but NOT `user.profile.updated`; `*.created` matches `user.created`/`order.created`; `*` alone matches only single-segment names (e.g. `boot`), not dotted ones.

Parameters

Parámetros de subscribe()
NombreTipoDescripción
$eventNamestringEvent name or wildcard pattern (e.g. 'user.created', 'user.*')
$handlercallableHandler function: fn(string $event, array $payload): void
$priorityintHigher priority handlers execute first (default: 0)

MilpaEventDispatcherInterface::getSubscribers()

abstract public function getSubscribers(string $eventName): array

Get all subscribers for an event (including wildcard matches).

Parameters

Parámetros de getSubscribers()
NombreTipoDescripción
$eventNamestringEvent name

Returns

Array of handlers sorted by priority

MilpaEventDispatcherInterface::hasSubscribers()

abstract public function hasSubscribers(string $eventName): bool

Check if an event has any subscribers.

Parameters

Parámetros de hasSubscribers()
NombreTipoDescripción
$eventNamestringEvent name