Fr3nch13/CakePHP Utilities

EventManager
in package
implements EventManagerInterface

The event manager is responsible for keeping track of event listeners, passing the correct data to them, and firing them in the correct order, when associated events are triggered. You can create multiple instances of this object to manage local events or keep a single instance and pass it around to manage all events in your app.

Table of Contents

Interfaces

EventManagerInterface
Interface EventManagerInterface

Properties

$defaultPriority  : int
The default priority queue value for new, attached listeners
$_eventList  : EventList|null
The event list object.
$_generalManager  : EventManager|null
The globally available instance, used for dispatching events attached from any scope
$_isGlobal  : bool
Internal flag to distinguish a common manager from the singleton
$_listeners  : array<string|int, mixed>
List of listener callbacks associated to
$_trackEvents  : bool
Enables automatic adding of events to the event list object if it is present.

Methods

__debugInfo()  : array<string, mixed>
Debug friendly object properties.
addEventToList()  : $this
Adds an event to the list if the event list object is present.
dispatch()  : EventInterface
Dispatches a new event to all configured listeners
getEventList()  : EventList|null
Returns the event list.
instance()  : EventManager
Returns the globally available instance of a Cake\Event\EventManager this is used for dispatching events attached from outside the scope other managers were created. Usually for creating hook systems or inter-class communication
isTrackingEvents()  : bool
Returns whether this manager is set up to track events
listeners()  : array<string|int, mixed>
Returns a list of all listeners for an eventKey in the order they should be called
matchingListeners()  : array<string|int, mixed>
Returns the listeners matching a specified pattern
off()  : $this
Remove a listener from the active listeners.
on()  : $this
Adds a new listener to an event.
prioritisedListeners()  : array<string|int, mixed>
Returns the listeners for the specified event key indexed by priority
setEventList()  : $this
Enables the listing of dispatched events.
trackEvents()  : $this
Enables / disables event tracking at runtime.
unsetEventList()  : $this
Disables the listing of dispatched events.
_attachSubscriber()  : void
Auxiliary function to attach all implemented callbacks of a Cake\Event\EventListenerInterface class instance as individual methods on this manager
_callListener()  : mixed
Calls a listener.
_detachSubscriber()  : void
Auxiliary function to help detach all listeners provided by an object implementing EventListenerInterface
_extractCallable()  : array<string|int, mixed>
Auxiliary function to extract and return a PHP callback type out of the callable definition from the return value of the `implementedEvents()` method on a {@link \Cake\Event\EventListenerInterface}

Properties

$defaultPriority

The default priority queue value for new, attached listeners

public static int $defaultPriority = 10

$_generalManager

The globally available instance, used for dispatching events attached from any scope

protected static EventManager|null $_generalManager

$_isGlobal

Internal flag to distinguish a common manager from the singleton

protected bool $_isGlobal = false

$_listeners

List of listener callbacks associated to

protected array<string|int, mixed> $_listeners = []

$_trackEvents

Enables automatic adding of events to the event list object if it is present.

protected bool $_trackEvents = false

Methods

__debugInfo()

Debug friendly object properties.

public __debugInfo() : array<string, mixed>
Return values
array<string, mixed>

addEventToList()

Adds an event to the list if the event list object is present.

public addEventToList(EventInterface $event) : $this
Parameters
$event : EventInterface

An event to add to the list.

Return values
$this

dispatch()

Dispatches a new event to all configured listeners

public dispatch(mixed $event) : EventInterface
Parameters
$event : mixed

The event key name or instance of EventInterface.

Tags
inheritDoc
Return values
EventInterface

instance()

Returns the globally available instance of a Cake\Event\EventManager this is used for dispatching events attached from outside the scope other managers were created. Usually for creating hook systems or inter-class communication

public static instance([EventManager|null $manager = null ]) : EventManager

If called with the first parameter, it will be set as the globally available instance

Parameters
$manager : EventManager|null = null

Event manager instance.

Return values
EventManager

The global event manager

isTrackingEvents()

Returns whether this manager is set up to track events

public isTrackingEvents() : bool
Return values
bool

listeners()

Returns a list of all listeners for an eventKey in the order they should be called

public listeners(string $eventKey) : array<string|int, mixed>
Parameters
$eventKey : string

Event key.

Tags
inheritDoc
Return values
array<string|int, mixed>

matchingListeners()

Returns the listeners matching a specified pattern

public matchingListeners(string $eventKeyPattern) : array<string|int, mixed>
Parameters
$eventKeyPattern : string

Pattern to match.

Return values
array<string|int, mixed>

off()

Remove a listener from the active listeners.

public off(mixed $eventKey[, mixed $callable = null ]) : $this
Parameters
$eventKey : mixed

The event unique identifier name with which the callback has been associated, or the $listener you want to remove.

$callable : mixed = null

The callback you want to detach.

Tags
inheritDoc
Return values
$this

on()

Adds a new listener to an event.

public on(mixed $eventKey[, mixed $options = [] ][, callable|null $callable = null ]) : $this
Parameters
$eventKey : mixed

The event unique identifier name with which the callback will be associated. If $eventKey is an instance of Cake\Event\EventListenerInterface its events will be bound using the implementedEvents() methods.

$options : mixed = []

Either an array of options or the callable you wish to bind to $eventKey. If an array of options, the priority key can be used to define the order. Priorities are treated as queues. Lower values are called before higher ones, and multiple attachments added to the same priority queue will be treated in the order of insertion.

$callable : callable|null = null

The callable function you want invoked.

Tags
inheritDoc
Return values
$this

prioritisedListeners()

Returns the listeners for the specified event key indexed by priority

public prioritisedListeners(string $eventKey) : array<string|int, mixed>
Parameters
$eventKey : string

Event key.

Return values
array<string|int, mixed>

setEventList()

Enables the listing of dispatched events.

public setEventList(EventList $eventList) : $this
Parameters
$eventList : EventList

The event list object to use.

Return values
$this

trackEvents()

Enables / disables event tracking at runtime.

public trackEvents(bool $enabled) : $this
Parameters
$enabled : bool

True or false to enable / disable it.

Return values
$this

unsetEventList()

Disables the listing of dispatched events.

public unsetEventList() : $this
Return values
$this

_attachSubscriber()

Auxiliary function to attach all implemented callbacks of a Cake\Event\EventListenerInterface class instance as individual methods on this manager

protected _attachSubscriber(EventListenerInterface $subscriber) : void
Parameters
$subscriber : EventListenerInterface

Event listener.

_callListener()

Calls a listener.

protected _callListener(callable $listener, EventInterface $event) : mixed
Parameters
$listener : callable

The listener to trigger.

$event : EventInterface

Event instance.

Return values
mixed

The result of the $listener function.

_detachSubscriber()

Auxiliary function to help detach all listeners provided by an object implementing EventListenerInterface

protected _detachSubscriber(EventListenerInterface $subscriber[, string|null $eventKey = null ]) : void
Parameters
$subscriber : EventListenerInterface

the subscriber to be detached

$eventKey : string|null = null

optional event key name to unsubscribe the listener from

_extractCallable()

Auxiliary function to extract and return a PHP callback type out of the callable definition from the return value of the `implementedEvents()` method on a {@link \Cake\Event\EventListenerInterface}

protected _extractCallable(array<string|int, mixed> $function, EventListenerInterface $object) : array<string|int, mixed>
Parameters
$function : array<string|int, mixed>

the array taken from a handler definition for an event

$object : EventListenerInterface

The handler object

Return values
array<string|int, mixed>

        
On this page

Search results