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
- 
        $_eventList protectedCake\Event\EventList|nullThe event list object. 
- 
        $_generalManager protected staticCake\Event\EventManagerThe globally available instance, used for dispatching events attached from any scope 
- 
        $_isGlobal protectedboolInternal flag to distinguish a common manager from the singleton 
- 
        $_listeners protectedarrayList of listener callbacks associated to 
- 
        $_trackEvents protectedboolEnables automatic adding of events to the event list object if it is present. 
- 
        $defaultPriority public staticintThe default priority queue value for new, attached listeners 
Method Summary
- 
          __debugInfo() publicDebug friendly object properties. 
- 
          _attachSubscriber() protectedAuxiliary function to attach all implemented callbacks of a Cake\Event\EventListenerInterface class instance as individual methods on this manager 
- 
          _callListener() protectedCalls a listener. 
- 
          _detachSubscriber() protectedAuxiliary function to help detach all listeners provided by an object implementing EventListenerInterface 
- 
          _extractCallable() protectedAuxiliary function to extract and return a PHP callback type out of the callable definition from the return value of the implementedEventsmethod on a Cake\Event\EventListenerInterface
- 
          addEventToList() publicAdds an event to the list if the event list object is present. 
- 
          dispatch() publicDispatches a new event to all configured listeners 
- 
          getEventList() publicReturns the event list. 
- 
          instance() public staticReturns 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() publicReturns whether this manager is set up to track events 
- 
          listeners() publicReturns a list of all listeners for an eventKey in the order they should be called 
- 
          matchingListeners() publicReturns the listeners matching a specified pattern 
- 
          off() publicRemove a listener from the active listeners. 
- 
          on() publicAdds a new listener to an event. 
- 
          prioritisedListeners() publicReturns the listeners for the specified event key indexed by priority 
- 
          setEventList() publicEnables the listing of dispatched events. 
- 
          trackEvents() publicEnables / disables event tracking at runtime. 
- 
          unsetEventList() publicDisables the listing of dispatched events. 
Method Detail
_attachSubscriber() ¶ protected
_attachSubscriber(Cake\Event\EventListenerInterface $subscriber): voidAuxiliary 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
void_callListener() ¶ protected
_callListener(callable $listener, Cake\Event\EventInterface $event): mixedCalls a listener.
Parameters
- 
                callable$listener
- The listener to trigger. 
- 
                Cake\Event\EventInterface$event
- Event instance. 
Returns
mixedThe result of the $listener function.
_detachSubscriber() ¶ protected
_detachSubscriber(Cake\Event\EventListenerInterface $subscriber, string|null $eventKey = null): voidAuxiliary 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
void_extractCallable() ¶ protected
_extractCallable(array $function, Cake\Event\EventListenerInterface $object): arrayAuxiliary function to extract and return a PHP callback type out of the callable definition
from the return value of the implementedEvents method on a Cake\Event\EventListenerInterface
Parameters
- 
                array$function
- the array taken from a handler definition for an event 
- 
                Cake\Event\EventListenerInterface$object
- The handler object 
Returns
arrayaddEventToList() ¶ public
addEventToList(Cake\Event\EventInterface $event): $thisAdds an event to the list if the event list object is present.
Parameters
- 
                Cake\Event\EventInterface$event
- An event to add to the list. 
Returns
$thisdispatch() ¶ public
dispatch(string|Cake\Event\EventInterface $event): Cake\Event\EventInterfaceDispatches a new event to all configured listeners
Parameters
- 
                string|Cake\Event\EventInterface$event
Returns
Cake\Event\EventInterfacegetEventList() ¶ public
getEventList(): Cake\Event\EventList|nullReturns the event list.
Returns
Cake\Event\EventList|nullinstance() ¶ public static
instance(Cake\Event\EventManager|null $manager = null): Cake\Event\EventManagerReturns 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(): boolReturns whether this manager is set up to track events
Returns
boollisteners() ¶ public
listeners(string $eventKey): arrayReturns a list of all listeners for an eventKey in the order they should be called
Parameters
- 
                string$eventKey
Returns
arraymatchingListeners() ¶ public
matchingListeners(string $eventKeyPattern): arrayReturns the listeners matching a specified pattern
Parameters
- 
                string$eventKeyPattern
- Pattern to match. 
Returns
arrayoff() ¶ public
off(string|Cake\Event\EventListenerInterface|callable $eventKey, Cake\Event\EventListenerInterface|callable|null $callable = null): $thisRemove 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
- 
                string|Cake\Event\EventListenerInterface|callable$eventKey
- 
                Cake\Event\EventListenerInterface|callable|null$callable optional
Returns
$thison() ¶ public
on(string|Cake\Event\EventListenerInterface $eventKey, array|callable $options = [], callable|null $callable = null): $thisAdds a new listener to an event.
A variadic interface to add listeners that emulates jQuery.on().
Binding an EventListenerInterface:
$eventManager->on($listener);Binding with no options:
$eventManager->on('Model.beforeSave', $callable);Binding with options:
$eventManager->on('Model.beforeSave', ['priority' => 90], $callable);Parameters
- 
                string|Cake\Event\EventListenerInterface$eventKey
- 
                array|callable$options optional
- 
                callable|null$callable optional
Returns
$thisprioritisedListeners() ¶ public
prioritisedListeners(string $eventKey): arrayReturns the listeners for the specified event key indexed by priority
Parameters
- 
                string$eventKey
- Event key. 
Returns
arraysetEventList() ¶ public
setEventList(Cake\Event\EventList $eventList): $thisEnables the listing of dispatched events.
Parameters
- 
                Cake\Event\EventList$eventList
- The event list object to use. 
Returns
$thistrackEvents() ¶ public
trackEvents(bool $enabled): $thisEnables / disables event tracking at runtime.
Parameters
- 
                bool$enabled
- True or false to enable / disable it. 
Returns
$thisunsetEventList() ¶ public
unsetEventList(): $thisDisables the listing of dispatched events.
Returns
$thisProperty Detail
$_generalManager ¶ protected static
The globally available instance, used for dispatching events attached from any scope
Type
Cake\Event\EventManager$_trackEvents ¶ protected
Enables automatic adding of events to the event list object if it is present.
Type
bool$defaultPriority ¶ public static
The default priority queue value for new, attached listeners
Type
int