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.

Namespace: Cake\Event
Located at Event/EventManager.php

Method Detail

__debugInfosource public

__debugInfo( )

Debug friendly object properties.

Returns

array
array

_attachSubscribersource protected

_attachSubscriber( Cake\Event\EventListenerInterface $subscriber )

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.

_callListenersource protected

_callListener( callable $listener , Cake\Event\Event $event )

Calls a listener.

Direct callback invocation is up to 30% faster than using call_user_func_array. Optimize the common cases to provide improved performance.

Parameters

callable $listener
The listener to trigger.
Cake\Event\Event $event
Event instance.

Returns

mixed
The result of the $listener function.

_detachSubscribersource protected

_detachSubscriber( Cake\Event\EventListenerInterface $subscriber , string|null $eventKey null )

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 null
optional event key name to unsubscribe the listener from

_extractCallablesource protected

_extractCallable( array $function , Cake\Event\EventListenerInterface $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 Cake\Event\EventListenerInterface

Parameters

array $function
the array taken from a handler definition for an event
Cake\Event\EventListenerInterface $object
The handler object

Returns

callable
callback

attachsource public

attach( callable|Cake\Event\EventListenerInterface $callable , string $eventKey null , array $options [] )

Adds a new listener to an event.

Deprecated

3.0.0 Use on() instead.

Parameters

callable|Cake\Event\EventListenerInterface $callable
PHP valid callback type or instance of Cake\Event\EventListenerInterface to be called when the event named with $eventKey is triggered. If a Cake\Event\EventListenerInterface 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 Cake\Event\EventListenerInterface this argument will be ignored
array $options optional []
used to set the priority flag to the listener. In the future more options may be added. 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.

Throws

InvalidArgumentException
When event key is missing or callable is not an instance of Cake\Event\EventListenerInterface.

detachsource public

detach( callable|Cake\Event\EventListenerInterface $callable , string|null $eventKey null )

Removes a listener from the active listeners.

Deprecated

3.0.0 Use off() instead.

Parameters

callable|Cake\Event\EventListenerInterface $callable
any valid PHP callback type or an instance of EventListenerInterface
string|null $eventKey optional null
The event unique identifier name with which the callback has been associated

dispatchsource public

dispatch( string|Cake\Event\Event $event )

Dispatches a new event to all configured listeners

Parameters

string|Cake\Event\Event $event
the event key name or instance of Event

Returns

Cake\Event\Event
\Cake\Event\Event

Triggers

$event

instancesource public static

instance( Cake\Event\EventManager $manager null )

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 $manager optional null
Event manager instance.

Returns

Cake\Event\EventManager
the global event manager

listenerssource 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

array
array

offsource public

off( string|Cake\Event\EventListenerInterface $eventKey , callable $callable null )

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

string|Cake\Event\EventListenerInterface $eventKey
The event unique identifier name with which the callback has been associated, or the $listener you want to remove.
callable $callable optional null
The callback you want to detach.

onsource public

on( string|Cake\Event\EventListenerInterface $eventKey null , array|callable $options [] , callable $callable null )

Adds 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 optional null
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.
array|callable $options optional []
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 optional null
The callable function you want invoked.

Throws

InvalidArgumentException
When event key is missing or callable is not an instance of Cake\Event\EventListenerInterface.

prioritisedListenerssource public

prioritisedListeners( string $eventKey )

Returns the listeners for the specified event key indexed by priority

Parameters

string $eventKey
Event key.

Returns

array
array

Properties summary

$_generalManagersource

protected static Cake\Event\EventManager

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

$_listenerssource

protected object

List of listener callbacks associated to

[]

$defaultPrioritysource

public static integer

The default priority queue value for new, attached listeners

10

© 2005–2016 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.
http://api.cakephp.org/3.1/class-Cake.Event.EventManager.html