Event manager: notification system

Use the EventManager when you want to create a per-instance notification system for your objects.

 Methods

Constructor

__construct(null | string | int | array | \Traversable $identifiers) 

Allows optionally specifying identifier(s) to use to pull signals from a SharedEventManagerInterface.

Parameters

$identifiers

nullstringintarray\Traversable

Add some identifier(s) (appends to any currently set identifiers)

addIdentifiers(string | int | array | \Traversable $identifiers) : \Zend\EventManager\EventManager

Parameters

$identifiers

stringintarray\Traversable

Returns

\Zend\EventManager\EventManagerProvides a fluent interface

Attach a listener to an event

attach(string | array | \Zend\EventManager\ListenerAggregateInterface $event, \Zend\EventManager\callable | int $callback, int $priority) : \Zend\Stdlib\CallbackHandler | mixed

The first argument is the event, and the next argument describes a callback that will respond to that event. A CallbackHandler instance describing the event listener combination will be returned.

The last argument indicates a priority at which the event should be executed. By default, this value is 1; however, you may set it for any integer value. Higher values have higher priority (i.e., execute first).

You can specify "*" for the event name. In such cases, the listener will be triggered for every event.

Parameters

$event

stringarray\Zend\EventManager\ListenerAggregateInterface

An event or array of event names. If a ListenerAggregateInterface, proxies to {@link attachAggregate()}.

$callback

\Zend\EventManager\callableint

If string $event provided, expects PHP callback; for a ListenerAggregateInterface $event, this will be the priority

$priority

int

If provided, the priority at which to register the callable

Exceptions

\Zend\EventManager\Exception\InvalidArgumentException

Returns

\Zend\Stdlib\CallbackHandlermixedCallbackHandler if attaching callable (to allow later unsubscribe); mixed if attaching aggregate

Attach a listener aggregate

attachAggregate(\Zend\EventManager\ListenerAggregateInterface $aggregate, int $priority) : mixed

Listener aggregates accept an EventManagerInterface instance, and call attach() one or more times, typically to attach to multiple events using local methods.

Parameters

$priority

int

If provided, a suggested priority for the aggregate to use

Returns

mixedreturn value of {@link ListenerAggregateInterface::attach()}

Clear all listeners for a given event

clearListeners(string $event) : void

Parameters

$event

string

Unsubscribe a listener from an event

detach(\Zend\Stdlib\CallbackHandler | \Zend\EventManager\ListenerAggregateInterface $listener) : bool

Parameters

Exceptions

\Zend\EventManager\Exception\InvalidArgumentException if invalid listener provided

Returns

boolReturns true if event and listener found, and unsubscribed; returns false if either event or listener not found

Detach a listener aggregate

detachAggregate(\Zend\EventManager\ListenerAggregateInterface $aggregate) : mixed

Listener aggregates accept an EventManagerInterface instance, and call detach() of all previously attached listeners.

Parameters

Returns

mixedreturn value of {@link ListenerAggregateInterface::detach()}

Retrieve all registered events

getEvents() : array

Returns

array

Get the identifier(s) for this EventManager

getIdentifiers() : array

Returns

array

Retrieve all listeners for a given event

getListeners(string $event) : \Zend\Stdlib\PriorityQueue

Parameters

$event

string

Returns

Get shared event manager

getSharedManager() : false | \Zend\EventManager\SharedEventManagerInterface

If one is not defined, but we have a static instance in StaticEventManager, that one will be used and set in this instance.

If none is available in the StaticEventManager, a boolean false is returned.

Returns

Prepare arguments

prepareArgs(array $args) : \ArrayObject

Use this method if you want to be able to modify arguments from within a listener. It returns an ArrayObject of the arguments, which may then be passed to trigger() or triggerUntil().

Parameters

$args

array

Returns

Set the event class to utilize

setEventClass(string $class) : \Zend\EventManager\EventManager

Parameters

$class

string

Returns

Set the identifiers (overrides any currently set identifiers)

setIdentifiers(string | int | array | \Traversable $identifiers) : \Zend\EventManager\EventManager

Parameters

$identifiers

stringintarray\Traversable

Returns

\Zend\EventManager\EventManagerProvides a fluent interface

Set shared event manager

setSharedManager(\Zend\EventManager\SharedEventManagerInterface $sharedEventManager) : \Zend\EventManager\EventManager

Trigger all listeners for a given event

trigger(string $event, string | object $target, array | \ArrayAccess $argv, null | \Zend\EventManager\callable $callback) : \Zend\EventManager\ResponseCollection

Can emulate triggerUntil() if the last argument provided is a callback.

Parameters

$event

string

$target

stringobject

Object calling emit, or symbol describing target (such as static method name)

$argv

array\ArrayAccess

Array of arguments; typically, should be associative

$callback

null\Zend\EventManager\callable

Exceptions

\Zend\EventManager\Exception\InvalidCallbackException

Returns

\Zend\EventManager\ResponseCollectionAll listener return values

Trigger listeners until return value of one causes a callback to evaluate to true

triggerUntil(string $event, string | object $target, array | \ArrayAccess $argv, \Zend\EventManager\callable $callback) : \Zend\EventManager\ResponseCollection

Triggers listeners until the provided callback evaluates the return value of one as true, or until all listeners have been executed.

Parameters

$event

string

$target

stringobject

Object calling emit, or symbol describing target (such as static method name)

$argv

array\ArrayAccess

Array of arguments; typically, should be associative

$callback

\Zend\EventManager\callable

Exceptions

\Zend\EventManager\Exception\InvalidCallbackException if invalid callable provided

Returns

Remove any shared event manager currently attached

unsetSharedManager() : void

Get list of all listeners attached to the shared event manager for identifiers registered by this instance

getSharedListeners(string $event) : array

Parameters

$event

string

Returns

array

Add listeners to the master queue of listeners

insertListeners(\Zend\Stdlib\PriorityQueue $masterListeners, \Zend\Stdlib\PriorityQueue $listeners) : void

Used to inject shared listeners and wildcard listeners.

Parameters

$masterListeners

\Zend\Stdlib\PriorityQueue

Trigger listeners

triggerListeners(string $event, \Zend\EventManager\EventInterface $e, null | \Zend\EventManager\callable $callback) : \Zend\EventManager\ResponseCollection

Actual functionality for triggering listeners, to which both trigger() and triggerUntil() delegate.

Parameters

$event

string

Event name

$callback

null\Zend\EventManager\callable

Returns

 Properties

 

<p>Class representing the event being emitted</p>

$eventClass : string

 

Subscribed events and their listeners

$events : array

 

Identifiers, used to pull shared signals from SharedEventManagerInterface instance

$identifiers : array

 

Shared event manager

$sharedManager : false | null | \Zend\EventManager\SharedEventManagerInterface