Class EventManager
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.
Property Summary
-
$defaultPriority public static
intThe default priority queue value for new, attached listeners
-
$eventList protected
Cake\Event\EventList<object>|nullThe event list object.
-
$generalManager protected static
Cake\Event\EventManager|nullThe globally available instance, used for dispatching events attached from any scope
-
$isGlobal protected
boolInternal flag to distinguish a common manager from the singleton
-
$listeners protected
array<string, array<int, array<array{callable: callable}>>>List of listener callbacks associated to
-
$trackEvents protected
boolEnables automatic adding of events to the event list object if it is present.
Method Summary
-
__debugInfo() public
Debug friendly object properties.
-
addEventToList() public
Adds an event to the list if the event list object is present.
-
attachSubscriber() protected
Auxiliary function to attach all implemented callbacks of a Cake\Event\EventListenerInterface class instance as individual methods on this manager
-
callListener() protected
Calls a listener.
-
detachSubscriber() protected
Auxiliary function to help detach all listeners provided by an object implementing EventListenerInterface
-
dispatch() public
Dispatches a new event to all configured listeners
-
getEventList() public
Returns the event list.
-
instance() public static
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
If called with the first parameter, it will be set as the globally available instance
-
isTrackingEvents() public
Returns whether this manager is set up to track events
-
listeners() public
Returns a list of all listeners for an eventKey in the order they should be called
-
matchingListeners() public
Returns the listeners matching a specified pattern
-
normalizeHandler() protected
Builds a single normalized handler.
A normalized handler is an array with these keys:
callable- The event handler closuresettings- The event handler settings
-
normalizeHandlers() protected
Builds an array of normalized handlers.
A normalized handler is an array with these keys:
callable- The event handler closuresettings- The event handler settings
-
off() public
Remove a listener from the active listeners.
Remove a EventListenerInterface entirely:
$manager->off($listener);Remove all listeners for a given event:
$manager->off('My.event');Remove a specific listener:
$manager->off('My.event', $callback);Remove a callback from all events:
$manager->off($callback); -
on() public
Adds a new listener to an event.
Binding an EventListenerInterface:
$eventManager->on($listener);Binding with no options:
$eventManager->on('Model.beforeSave', $callable);Binding with options:
$eventManager->on('Model.beforeSave', $callable, ['priority' => 90]); -
prioritisedListeners() public
Returns the listeners for the specified event key indexed by priority
-
registerAttributeListeners() public
Registers event listeners declared via PHP attributes onto this event manager.
Reads EventListener attribute metadata from the configured AttributeResolver collection and registers the resolved callables.
Example
$eventManager = new EventManager(); $eventManager->registerAttributeListeners(); -
setEventList() public
Enables the listing of dispatched events.
-
trackEvents() public
Enables / disables event tracking at runtime.
-
unsetEventList() public
Disables the listing of dispatched events.
Method Detail
__debugInfo() ¶ public
__debugInfo(): array<string, mixed>
Debug friendly object properties.
Returns
array<string, mixed>addEventToList() ¶ public
addEventToList(Cake\Event\EventInterface<TSubject> $event): $this
Adds an event to the list if the event list object is present.
Templates
TSubject
of object Parameters
-
Cake\Event\EventInterface<TSubject>$event An event to add to the list.
Returns
$thisattachSubscriber() ¶ protected
attachSubscriber(Cake\Event\EventListenerInterface $subscriber): void
Auxiliary function to attach all implemented callbacks of a Cake\Event\EventListenerInterface class instance as individual methods on this manager
Parameters
-
Cake\Event\EventListenerInterface$subscriber Event listener.
Returns
voidcallListener() ¶ protected
callListener(Closure $listener, Cake\Event\EventInterface<TSubject> $event): void
Calls a listener.
Templates
TSubject
of object Parameters
-
Closure$listener The listener to trigger.
-
Cake\Event\EventInterface<TSubject>$event Event instance.
Returns
voiddetachSubscriber() ¶ protected
detachSubscriber(Cake\Event\EventListenerInterface $subscriber, string|null $eventKey = null): void
Auxiliary function to help detach all listeners provided by an object implementing EventListenerInterface
Parameters
-
Cake\Event\EventListenerInterface$subscriber the subscriber to be detached
-
string|null$eventKey optional optional event key name to unsubscribe the listener from
Returns
voiddispatch() ¶ public
dispatch(Cake\Event\EventInterface<TSubject>|string $event): $event is string ? \Cake\Event\EventInterface<object> : \Cake\Event\EventInterface<TSubject>
Dispatches a new event to all configured listeners
Parameters
-
Cake\Event\EventInterface<TSubject>|string$event
Returns
$event is string ? \Cake\Event\EventInterface<object> : \Cake\Event\EventInterface<TSubject>getEventList() ¶ public
getEventList(): Cake\Event\EventList<object>|null
Returns the event list.
Returns
Cake\Event\EventList<object>|nullinstance() ¶ public static
instance(Cake\Event\EventManager|null $manager = null): Cake\Event\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
If called with the first parameter, it will be set as the globally available instance
Parameters
-
Cake\Event\EventManager|null$manager optional Event manager instance.
Returns
Cake\Event\EventManagerThe global event manager
isTrackingEvents() ¶ public
isTrackingEvents(): bool
Returns whether this manager is set up to track events
Returns
boollisteners() ¶ public
listeners(string $eventKey): array
Returns a list of all listeners for an eventKey in the order they should be called
Parameters
-
string$eventKey
Returns
arraymatchingListeners() ¶ public
matchingListeners(string $eventKeyPattern): array
Returns the listeners matching a specified pattern
Parameters
-
string$eventKeyPattern Pattern to match.
Returns
arraynormalizeHandler() ¶ protected
normalizeHandler(Cake\Event\EventListenerInterface $subscriber, callable|array|string $handler): array{callable: \Closure, settings: array}
Builds a single normalized handler.
A normalized handler is an array with these keys:
callable- The event handler closuresettings- The event handler settings
Parameters
-
Cake\Event\EventListenerInterface$subscriber Event subscriber
-
callable|array|string$handler Event handler
Returns
array{callable: \Closure, settings: array}normalizeHandlers() ¶ protected
normalizeHandlers(Cake\Event\EventListenerInterface $subscriber, callable|array|string $handlers): array<array{callable: \Closure, settings: array}>
Builds an array of normalized handlers.
A normalized handler is an array with these keys:
callable- The event handler closuresettings- The event handler settings
Parameters
-
Cake\Event\EventListenerInterface$subscriber Event subscriber
-
callable|array|string$handlers Event handlers
Returns
array<array{callable: \Closure, settings: array}>off() ¶ public
off(Cake\Event\EventListenerInterface|callable|string $eventKey, Cake\Event\EventListenerInterface|callable|null $callable = null): $this
Remove a listener from the active listeners.
Remove a EventListenerInterface entirely:
$manager->off($listener);
Remove all listeners for a given event:
$manager->off('My.event');
Remove a specific listener:
$manager->off('My.event', $callback);
Remove a callback from all events:
$manager->off($callback);
Parameters
-
Cake\Event\EventListenerInterface|callable|string$eventKey -
Cake\Event\EventListenerInterface|callable|null$callable optional
Returns
$thison() ¶ public
on(Cake\Event\EventListenerInterface|string $eventKey, callable|null $callable = null, array $options = []): $this
Adds a new listener to an event.
Binding an EventListenerInterface:
$eventManager->on($listener);
Binding with no options:
$eventManager->on('Model.beforeSave', $callable);
Binding with options:
$eventManager->on('Model.beforeSave', $callable, ['priority' => 90]);
Parameters
-
Cake\Event\EventListenerInterface|string$eventKey -
callable|null$callable optional -
array$options optional
Returns
$thisprioritisedListeners() ¶ public
prioritisedListeners(string $eventKey): array
Returns the listeners for the specified event key indexed by priority
Parameters
-
string$eventKey Event key.
Returns
arrayregisterAttributeListeners() ¶ public
registerAttributeListeners(string $config = 'default', callable|null $listenerResolver = null, callable|null $managerResolver = null): $this
Registers event listeners declared via PHP attributes onto this event manager.
Reads EventListener attribute metadata from the configured AttributeResolver collection and registers the resolved callables.
Example
$eventManager = new EventManager();
$eventManager->registerAttributeListeners();
Parameters
-
string$config optional Attribute resolver config name.
-
callable|null$listenerResolver optional Resolver used to create listener instances.
-
callable|null$managerResolver optional Resolver used to resolve named event managers.
Returns
$thissetEventList() ¶ public
setEventList(Cake\Event\EventList<object> $eventList): $this
Enables the listing of dispatched events.
Parameters
-
Cake\Event\EventList<object>$eventList The event list object to use.
Returns
$thistrackEvents() ¶ public
trackEvents(bool $enabled): $this
Enables / disables event tracking at runtime.
Parameters
-
bool$enabled True or false to enable / disable it.
Returns
$thisunsetEventList() ¶ public
unsetEventList(): $this
Disables the listing of dispatched events.
Returns
$thisProperty Detail
$defaultPriority ¶ public static
The default priority queue value for new, attached listeners
Type
int$generalManager ¶ protected static
The globally available instance, used for dispatching events attached from any scope
Type
Cake\Event\EventManager|null$listeners ¶ protected
List of listener callbacks associated to
Type
array<string, array<int, array<array{callable: callable}>>>$trackEvents ¶ protected
Enables automatic adding of events to the event list object if it is present.
Type
bool