class EventDispatcher

Handles operations related to dispatching events.

Properties

protected array $listeners

An array of registered listeners.

Methods

void
listen(string $eventName, callable|string|array $listener)

Listens for an event.

void
dispatch(object $event)

Dispatches an event

void
enqueueListener(string $listenerClass, object $instance, object $event)

Enqueue an event listener for asynchronous execution.

bool
isEnqueueListener(string $listenerClass, object $instance, object $event)

Determine if the listener should be enqueued and enqueue it if applicable.

void
handleListener(mixed $listener, object $event)

Handle a registered listener in any supported shape.

bool
hasQueuePreferences(object $instance)

Check if a listener provides custom queue preferences.

void
processCallableArray(array $listener, object $event)

Process a listener provided as [FQCN|object, 'method'].

void
processClassString(string $listenerClass, object $event)

Process a listener provided as a fully-qualified class name.

string
setQueueName(object $instance)

Resolve the queue name for a queued listener or job.

Details

at line 28
void listen(string $eventName, callable|string|array $listener)

Listens for an event.

Parameters

string $eventName

The event name.

callable|string|array $listener

The listener.

Return Value

void

at line 38
void dispatch(object $event)

Dispatches an event

Parameters

object $event

The event to be dispatches.

Return Value

void

at line 61
private void enqueueListener(string $listenerClass, object $instance, object $event)

Enqueue an event listener for asynchronous execution.

Builds a job payload from the listener and event, applies queue preferences (delay, backoff, max attempts) if the listener implements {\Core\Lib\Events\Contracts\QueuePreferences}, and pushes the job to the specified queue via the {\Core\Lib\Queue\QueueManager}.

Parameters

string $listenerClass

Fully qualified class name of the listener.

object $instance

The instantiated listener object.

object $event

The event object being dispatched.

Return Value

void

at line 86
private bool isEnqueueListener(string $listenerClass, object $instance, object $event)

Determine if the listener should be enqueued and enqueue it if applicable.

If the listener implements {\Core\Lib\Events\Contracts\ShouldQueue}, it will be dispatched to the queue system instead of being executed immediately.

Parameters

string $listenerClass

Fully qualified class name of the listener.

object $instance

The instantiated listener object.

object $event

The event object being dispatched.

Return Value

bool

True if the listener was enqueued, false otherwise.

at line 106
private void handleListener(mixed $listener, object $event)

Handle a registered listener in any supported shape.

Supported forms:

  • string FQCN: "App\Listeners\SendWelcomeEmail" (calls handle())
  • array callable: [FQCN|object, 'method'] (calls given method)

Parameters

mixed $listener

The listener definition (string FQCN or [target, method]).

object $event

The event instance being dispatched.

Return Value

void

at line 127
private bool hasQueuePreferences(object $instance)

Check if a listener provides custom queue preferences.

Parameters

object $instance

The instantiated listener object.

Return Value

bool

True if the listener implements {\Core\Lib\Events\Contracts\QueuePreferences}, false otherwise.

at line 139
private void processCallableArray(array $listener, object $event)

Process a listener provided as [FQCN|object, 'method'].

Parameters

array $listener

A two-item array: [FQCN|string|object $target, string $method].

object $event

The event instance being dispatched.

Return Value

void

at line 161
private void processClassString(string $listenerClass, object $event)

Process a listener provided as a fully-qualified class name.

Assumes a conventional handle($event) method.

Parameters

string $listenerClass

Fully qualified class name of the listener.

object $event

The event instance being dispatched.

Return Value

void

at line 185
private string setQueueName(object $instance)

Resolve the queue name for a queued listener or job.

If the given instance implements {\Core\Lib\Events\Contracts\QueuePreferences} and defines a {\Core\Lib\Events\Contracts\QueuePreferences::viaQueue()} method, its return value will be used as the queue name. If viaQueue() returns null or the method does not exist, the queue name defaults to "default".

Parameters

object $instance

The listener or job instance.

Return Value

string

The resolved queue name.