W3cubDocs

/CakePHP 4.4

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

Property Summary

  • $_eventList protected
    Cake\Event\EventList|null

    The event list object.

  • $_generalManager protected static
    Cake\Event\EventManager|null

    The globally available instance, used for dispatching events attached from any scope

  • $_isGlobal protected
    bool

    Internal flag to distinguish a common manager from the singleton

  • $_listeners protected
    array

    List of listener callbacks associated to

  • $_trackEvents protected
    bool

    Enables automatic adding of events to the event list object if it is present.

  • $defaultPriority public static
    int

    The default priority queue value for new, attached listeners

Method Summary

  • __debugInfo() public

    Debug friendly object properties.

  • _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

  • _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 {@link \Cake\Event\EventListenerInterface}

  • addEventToList() public

    Adds an event to the list if the event list object is present.

  • 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

  • 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

  • off() public

    Remove a listener from the active listeners.

  • on() public

    Adds a new listener to an event.

  • prioritisedListeners() public

    Returns the listeners for the specified event key indexed by priority

  • 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>

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

void

_callListener() protected

_callListener(callable $listener, Cake\Event\EventInterface $event): mixed

Calls a listener.

Parameters

callable $listener

The listener to trigger.

Cake\Event\EventInterface $event

Event instance.

Returns

mixed

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

void

_extractCallable() protected

_extractCallable(array $function, Cake\Event\EventListenerInterface $object): array

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 {@link \Cake\Event\EventListenerInterface}

Parameters

array $function

the array taken from a handler definition for an event

Cake\Event\EventListenerInterface $object

The handler object

Returns

array

addEventToList() public

addEventToList(Cake\Event\EventInterface $event): $this

Adds 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

$this

dispatch() public

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

Dispatches a new event to all configured listeners

Parameters

Cake\Event\EventInterface|string $event

Returns

Cake\Event\EventInterface

getEventList() public

getEventList(): Cake\Event\EventList|null

Returns the event list.

Returns

Cake\Event\EventList|null

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

isTrackingEvents() public

isTrackingEvents(): bool

Returns whether this manager is set up to track events

Returns

bool

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

array

matchingListeners() public

matchingListeners(string $eventKeyPattern): array

Returns the listeners matching a specified pattern

Parameters

string $eventKeyPattern

Pattern to match.

Returns

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

$this

on() public

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

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

Cake\Event\EventListenerInterface|string $eventKey
callable|array $options optional
callable|null $callable optional

Returns

$this

prioritisedListeners() public

prioritisedListeners(string $eventKey): array

Returns the listeners for the specified event key indexed by priority

Parameters

string $eventKey

Event key.

Returns

array

setEventList() public

setEventList(Cake\Event\EventList $eventList): $this

Enables the listing of dispatched events.

Parameters

Cake\Event\EventList $eventList

The event list object to use.

Returns

$this

trackEvents() public

trackEvents(bool $enabled): $this

Enables / disables event tracking at runtime.

Parameters

bool $enabled

True or false to enable / disable it.

Returns

$this

unsetEventList() public

unsetEventList(): $this

Disables the listing of dispatched events.

Returns

$this

Property Detail

$_eventList protected

The event list object.

Type

Cake\Event\EventList|null

$_generalManager protected static

The globally available instance, used for dispatching events attached from any scope

Type

Cake\Event\EventManager|null

$_isGlobal protected

Internal flag to distinguish a common manager from the singleton

Type

bool

$_listeners protected

List of listener callbacks associated to

Type

array

$_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

© 2005–present 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/4.4/class-Cake.Event.EventManager.html