Class CakeEventManager
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.
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/Event/CakeEventManager.php
Properties summary
- The globally available instance, used for dispatching events attached from any scope
-
$_isGlobal
protectedInternal flag to distinguish a common manager from the singletonboolean
-
$_listeners
protectedList of listener callbacks associated toobject
- The default priority queue value for new, attached listeners
integer
Method Summary
- _attachSubscriber() protected
Auxiliary function to attach all implemented callbacks of a CakeEventListener class instance as individual methods on this manager
- _detachSubscriber() protectedAuxiliary function to help detach all listeners provided by an object implementing CakeEventListener
- _extractCallable() protected
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 CakeEventListener - attach() publicAdds a new listener to an event. Listeners
- detach() publicRemoves a listener from the active listeners.
- dispatch() publicDispatches a new event to all configured listeners
-
Returns the globally available instance of a CakeEventManager this is used for dispatching events attached from outside the scope other managers were created. Usually for creating hook systems or inter-class communication
- listeners() publicReturns a list of all listeners for an eventKey in the order they should be called
- prioritisedListeners() publicReturns the listeners for the specified event key indexed by priority
Method Detail
_attachSubscriber()source protected
_attachSubscriber( CakeEventListener $subscriber )
Auxiliary function to attach all implemented callbacks of a CakeEventListener class instance as individual methods on this manager
Parameters
-
CakeEventListener
$subscriber
- Event listener.
_detachSubscriber()source protected
_detachSubscriber( CakeEventListener $subscriber , string $eventKey null )
Auxiliary function to help detach all listeners provided by an object implementing CakeEventListener
Parameters
-
CakeEventListener
$subscriber
- the subscriber to be detached
- string
$eventKey
optional null - optional event key name to unsubscribe the listener from
_extractCallable()source protected
_extractCallable( array $function , CakeEventListener $object )
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 CakeEventListener
Parameters
- array
$function
- the array taken from a handler definition for an event
-
CakeEventListener
$object
- The handler object
Returns
callableattach()source public
attach( callable|CakeEventListener $callable , string $eventKey null , array $options array() )
Adds a new listener to an event. Listeners
Parameters
- callable|
CakeEventListener
$callable
PHP valid callback type or instance of CakeEventListener to be called when the event named with $eventKey is triggered. If a CakeEventListener instance is passed, then the
implementedEvents
method will be called on the object to register the declared events individually as methods to be managed by this class. It is possible to define multiple event handlers per event name.- string
$eventKey
optional null The event unique identifier name with which the callback will be associated. If $callable is an instance of CakeEventListener this argument will be ignored
- array
$options
optional array() used to set the
priority
andpassParams
flags to the listener. Priorities are handled like queues, and multiple attachments added to the same priority queue will be treated in the order of insertion.passParams
means that the event data property will be converted to function arguments when the listener is called. If $called is an instance of CakeEventListener, this parameter will be ignored
Throws
InvalidArgumentExceptionWhen event key is missing or callable is not an instance of CakeEventListener.
detach()source public
detach( callable|CakeEventListener $callable , string $eventKey null )
Removes a listener from the active listeners.
Parameters
- callable|
CakeEventListener
$callable
- any valid PHP callback type or an instance of CakeEventListener
- string
$eventKey
optional null - The event unique identifier name with which the callback has been associated
dispatch()source public
dispatch( string|CakeEvent $event )
Dispatches a new event to all configured listeners
Parameters
- string|
CakeEvent
$event
- the event key name or instance of CakeEvent
Returns
CakeEvent
Triggers
$eventinstance()source public static
instance( CakeEventManager $manager null )
Returns the globally available instance of a CakeEventManager 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
-
CakeEventManager
$manager
optional null - Optional event manager instance.
Returns
CakeEventManager
the global event manager
listeners()source public
listeners( string $eventKey )
Returns a list of all listeners for an eventKey in the order they should be called
Parameters
- string
$eventKey
- Event key.
Returns
arrayprioritisedListeners()source public
prioritisedListeners( string $eventKey )
Returns the listeners for the specified event key indexed by priority
Parameters
- string
$eventKey
- Event key.
Returns
arrayProperties detail
$_generalManagersource
protected static CakeEventManager
The globally available instance, used for dispatching events attached from any scope
null
$_isGlobalsource
protected boolean
Internal flag to distinguish a common manager from the singleton
false
$defaultPrioritysource
public static integer
The default priority queue value for new, attached listeners
10
© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/2.10/class-CakeEventManager.html