class Dispatcher implements Dispatcher (View source)

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected Container $container

The IoC container instance.

protected array<string, callable|array|class-string|null> $listeners

The registered event listeners.

protected Closure|string> $wildcards

The wildcard listeners.

protected Closure|string> $wildcardsCache

The cached wildcard listeners.

protected Queue $queueResolver

The queue resolver instance.

protected callable $transactionManagerResolver

The database transaction manager resolver instance.

protected array $deferredEvents

The currently deferred events.

protected bool $deferringEvents

Indicates if events should be deferred.

protected string[]|null $eventsToDefer

The specific events to defer (null means defer all events).

Methods

static void
macro(string $name, object|callable $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Checks if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

string
firstClosureParameterType(Closure $closure)

Get the class name of the first parameter of the given Closure.

array
firstClosureParameterTypes(Closure $closure)

Get the class names of the first parameter of the given Closure, including union types.

array
closureParameterTypes(Closure $closure)

Get the class names / types of the parameters of the given Closure.

list<class-string>
closureReturnTypes(Closure $closure)

Get the class names / types of the return type of the given Closure.

__construct(Container|null $container = null)

Create a new event dispatcher instance.

void
listen(Closure|string|array $events, Closure|string|array|null $listener = null)

Register an event listener with the dispatcher.

void
setupWildcardListen(string $event, Closure|string $listener)

Setup a wildcard listener callback.

bool
hasListeners(string $eventName)

Determine if a given event has listeners.

bool
hasWildcardListeners(string $eventName)

Determine if the given event has any wildcard listeners.

void
push(string $event, array $payload = [])

Register an event and payload to be fired later.

void
flush(string $event)

Flush a set of pushed events.

void
subscribe(object|string $subscriber)

Register an event subscriber with the dispatcher.

($subscriber is object ? object : mixed)
resolveSubscriber(object|class-string $subscriber)

Resolve the subscriber instance.

mixed
until(string|object $event, mixed $payload = [])

Fire an event until the first non-null response is returned.

array|null
dispatch(string|object $event, mixed $payload = [], bool $halt = false)

Fire an event and call the listeners.

array|null
invokeListeners(string|object $event, mixed $payload, bool $halt = false)

Broadcast an event and call its listeners.

array{string, array}
parseEventAndPayload(mixed $event, mixed $payload)

Parse the given event and payload and prepare them for dispatching.

bool
shouldBroadcast(array $payload)

Determine if the payload has a broadcastable event.

bool
broadcastWhen(mixed $event)

Check if the event should be broadcasted by the condition.

void
broadcastEvent(ShouldBroadcast $event)

Broadcast the given event class.

array
getListeners(string $eventName)

Get all of the listeners for a given event name.

array
getWildcardListeners(string $eventName)

Get the wildcard listeners for the event.

array
addInterfaceListeners(string $eventName, array $listeners = [])

Add the listeners for the event's interfaces to the given array.

prepareListeners(string $eventName)

Prepare the listeners for a given event.

makeListener(Closure|string|array{class-string, string} $listener, bool $wildcard = false)

Register an event listener with the dispatcher.

createClassListener(string $listener, bool $wildcard = false)

Create a class based listener using the IoC container.

callable
createClassCallable(array{class-string, string}|string $listener)

Create the class based event callable.

array{class-string, string}
parseClassCallable(string $listener)

Parse the class listener into class and method.

bool
handlerShouldBeQueued(class-string $class)

Determine if the event handler class should be queued.

Closure(): void
createQueuedHandlerCallable(class-string $class, string $method)

Create a callable for putting an event handler on the queue.

bool
handlerShouldBeDispatchedAfterDatabaseTransactions(mixed $listener)

Determine if the given event handler should be dispatched after all database transactions have committed.

createCallbackForListenerRunningAfterCommits(mixed $listener, string $method)

Create a callable for dispatching a listener after database transactions.

bool
handlerWantsToBeQueued(class-string $class, array $arguments)

Determine if the event handler wants to be queued.

void
queueHandler(string $class, string $method, array $arguments)

Queue the handler class.

TListener, mixed}
createListenerAndJob(TListener> $class, string $method, array $arguments)

Create the listener and job for a queued listener.

propagateListenerOptions(mixed $listener, CallQueuedListener $job)

Propagate listener options to the job.

void
forget(string $event)

Remove a set of listeners from the dispatcher.

void
forgetPushed()

Forget all of the pushed listeners.

resolveQueue()

Get the queue implementation from the resolver.

$this
setQueueResolver(callable $resolver)

Set the queue resolver implementation.

resolveTransactionManager()

Get the database transaction manager implementation from the resolver.

$this
setTransactionManagerResolver(callable $resolver)

Set the database transaction manager resolver implementation.

TResult
defer(callable $callback, array|null $events = null)

Execute the given callback while deferring events, then dispatch all deferred events.

bool
shouldDeferEvent(string $event)

Determine if the given event should be deferred.

array
getRawListeners()

Gets the raw, unprepared listeners.

Details

static void macro(string $name, object|callable $macro)

Register a custom macro.

Parameters

string $name
object|callable $macro

Return Value

void

static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

static bool hasMacro(string $name)

Checks if macro is registered.

Parameters

string $name

Return Value

bool

static void flushMacros()

Flush the existing macros.

Return Value

void

static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

protected string firstClosureParameterType(Closure $closure)

Get the class name of the first parameter of the given Closure.

Parameters

Closure $closure

Return Value

string

Exceptions

ReflectionException
RuntimeException

protected array firstClosureParameterTypes(Closure $closure)

Get the class names of the first parameter of the given Closure, including union types.

Parameters

Closure $closure

Return Value

array

Exceptions

ReflectionException
RuntimeException

protected array closureParameterTypes(Closure $closure)

Get the class names / types of the parameters of the given Closure.

Parameters

Closure $closure

Return Value

array

Exceptions

ReflectionException

protected list<class-string> closureReturnTypes(Closure $closure)

Get the class names / types of the return type of the given Closure.

Parameters

Closure $closure

Return Value

list<class-string>

Exceptions

ReflectionException

__construct(Container|null $container = null)

Create a new event dispatcher instance.

Parameters

Container|null $container

void listen(Closure|string|array $events, Closure|string|array|null $listener = null)

Register an event listener with the dispatcher.

Parameters

Closure|string|array $events
Closure|string|array|null $listener

Return Value

void

protected void setupWildcardListen(string $event, Closure|string $listener)

Setup a wildcard listener callback.

Parameters

string $event
Closure|string $listener

Return Value

void

bool hasListeners(string $eventName)

Determine if a given event has listeners.

Parameters

string $eventName

Return Value

bool

bool hasWildcardListeners(string $eventName)

Determine if the given event has any wildcard listeners.

Parameters

string $eventName

Return Value

bool

void push(string $event, array $payload = [])

Register an event and payload to be fired later.

Parameters

string $event
array $payload

Return Value

void

void flush(string $event)

Flush a set of pushed events.

Parameters

string $event

Return Value

void

void subscribe(object|string $subscriber)

Register an event subscriber with the dispatcher.

Parameters

object|string $subscriber

Return Value

void

protected ($subscriber is object ? object : mixed) resolveSubscriber(object|class-string $subscriber)

Resolve the subscriber instance.

Parameters

object|class-string $subscriber

Return Value

($subscriber is object ? object : mixed)

mixed until(string|object $event, mixed $payload = [])

Fire an event until the first non-null response is returned.

Parameters

string|object $event
mixed $payload

Return Value

mixed

array|null dispatch(string|object $event, mixed $payload = [], bool $halt = false)

Fire an event and call the listeners.

Parameters

string|object $event
mixed $payload
bool $halt

Return Value

array|null

protected array|null invokeListeners(string|object $event, mixed $payload, bool $halt = false)

Broadcast an event and call its listeners.

Parameters

string|object $event
mixed $payload
bool $halt

Return Value

array|null

protected array{string, array} parseEventAndPayload(mixed $event, mixed $payload)

Parse the given event and payload and prepare them for dispatching.

Parameters

mixed $event
mixed $payload

Return Value

array{string, array}

protected bool shouldBroadcast(array $payload)

Determine if the payload has a broadcastable event.

Parameters

array $payload

Return Value

bool

protected bool broadcastWhen(mixed $event)

Check if the event should be broadcasted by the condition.

Parameters

mixed $event

Return Value

bool

protected void broadcastEvent(ShouldBroadcast $event)

Broadcast the given event class.

Parameters

ShouldBroadcast $event

Return Value

void

array getListeners(string $eventName)

Get all of the listeners for a given event name.

Parameters

string $eventName

Return Value

array

protected array getWildcardListeners(string $eventName)

Get the wildcard listeners for the event.

Parameters

string $eventName

Return Value

array

protected array addInterfaceListeners(string $eventName, array $listeners = [])

Add the listeners for the event's interfaces to the given array.

Parameters

string $eventName
array $listeners

Return Value

array

protected Closure[] prepareListeners(string $eventName)

Prepare the listeners for a given event.

Parameters

string $eventName

Return Value

Closure[]

Closure makeListener(Closure|string|array{class-string, string} $listener, bool $wildcard = false)

Register an event listener with the dispatcher.

Parameters

Closure|string|array{class-string, string} $listener
bool $wildcard

Return Value

Closure

Closure createClassListener(string $listener, bool $wildcard = false)

Create a class based listener using the IoC container.

Parameters

string $listener
bool $wildcard

Return Value

Closure

protected callable createClassCallable(array{class-string, string}|string $listener)

Create the class based event callable.

Parameters

array{class-string, string}|string $listener

Return Value

callable

protected array{class-string, string} parseClassCallable(string $listener)

Parse the class listener into class and method.

Parameters

string $listener

Return Value

array{class-string, string}

protected bool handlerShouldBeQueued(class-string $class)

Determine if the event handler class should be queued.

Parameters

class-string $class

Return Value

bool

protected Closure(): void createQueuedHandlerCallable(class-string $class, string $method)

Create a callable for putting an event handler on the queue.

Parameters

class-string $class
string $method

Return Value

Closure(): void

protected bool handlerShouldBeDispatchedAfterDatabaseTransactions(mixed $listener)

Determine if the given event handler should be dispatched after all database transactions have committed.

Parameters

mixed $listener

Return Value

bool

protected Closure createCallbackForListenerRunningAfterCommits(mixed $listener, string $method)

Create a callable for dispatching a listener after database transactions.

Parameters

mixed $listener
string $method

Return Value

Closure

protected bool handlerWantsToBeQueued(class-string $class, array $arguments)

Determine if the event handler wants to be queued.

Parameters

class-string $class
array $arguments

Return Value

bool

protected void queueHandler(string $class, string $method, array $arguments)

Queue the handler class.

Parameters

string $class
string $method
array $arguments

Return Value

void

protected TListener, mixed} createListenerAndJob(TListener> $class, string $method, array $arguments)

Create the listener and job for a queued listener.

Parameters

TListener> $class
string $method
array $arguments

Return Value

TListener, mixed}

protected CallQueuedListener propagateListenerOptions(mixed $listener, CallQueuedListener $job)

Propagate listener options to the job.

Parameters

mixed $listener
CallQueuedListener $job

Return Value

CallQueuedListener

void forget(string $event)

Remove a set of listeners from the dispatcher.

Parameters

string $event

Return Value

void

void forgetPushed()

Forget all of the pushed listeners.

Return Value

void

protected Queue resolveQueue()

Get the queue implementation from the resolver.

Return Value

Queue

$this setQueueResolver(callable $resolver)

Set the queue resolver implementation.

Parameters

callable $resolver

Return Value

$this

protected DatabaseTransactionsManager|null resolveTransactionManager()

Get the database transaction manager implementation from the resolver.

Return Value

DatabaseTransactionsManager|null

$this setTransactionManagerResolver(callable $resolver)

Set the database transaction manager resolver implementation.

Parameters

callable $resolver

Return Value

$this

TResult defer(callable $callback, array|null $events = null)

Execute the given callback while deferring events, then dispatch all deferred events.

Parameters

callable $callback
array|null $events

Return Value

TResult

protected bool shouldDeferEvent(string $event)

Determine if the given event should be deferred.

Parameters

string $event

Return Value

bool

array getRawListeners()

Gets the raw, unprepared listeners.

Return Value

array

Laravel Cloud is the best place to deploy Laravel, Nuxt, Express, Hono, Node.js, Bun, Go, Flask, Python, and more, with dedicated infrastructure available on AWS through Laravel Private Cloud.