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.

Package: Cake\Event
Copyright: Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
License: MIT License
Location: Cake/Event/CakeEventManager.php

Properties summary

  • $_generalManager protected static
    The globally available instance, used for dispatching events attached from any scope
  • $_isGlobal protected
    boolean
    Internal flag to distinguish a common manager from the singleton
  • $_listeners protected
    object
    List of listener callbacks associated to
  • $defaultPriority public static
    integer
    The default priority queue value for new, attached listeners

Method Summary

  • Auxiliary function to attach all implemented callbacks of a CakeEventListener class instance as individual methods on this manager

  • Auxiliary 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() public
    Adds a new listener to an event. Listeners
  • detach() public
    Removes a listener from the active listeners.
  • dispatch() public
    Dispatches a new event to all configured listeners
  • instance() public static

    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() public
    Returns a list of all listeners for an eventKey in the order they should be called
  • Returns 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

callable

attach()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 and passParams 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

InvalidArgumentException

When 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

$event

instance()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

array

prioritisedListeners()source public

prioritisedListeners( string $eventKey )

Returns the listeners for the specified event key indexed by priority

Parameters

string $eventKey
Event key.

Returns

array

Properties 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

$_listenerssource

protected object

List of listener callbacks associated to

array()

$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.
https://api.cakephp.org/2.9/class-CakeEventManager.html