Event manager: notification system

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

package Default

 Methods

Constructor

__construct(null|string|integer|array|\Traversable $identifiers = null

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

Parameters

$identifiers

nullstringintegerarray\Traversable

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

addIdentifiers(string|integer|array|\Traversable $identifiers) : \Zend\EventManager\EventManager
inherited_from \Zend\EventManager\EventManagerInterface::addIdentifiers()

Parameters

$identifiers

stringintegerarray\Traversable

Returns

\Zend\EventManager\EventManagerProvides a fluent interface

Attach a listener to an event

attach(string|array|\Zend\EventManager\ListenerAggregateInterface $event, callable|integer $callback = null, integer $priority = 1) : \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.

inherited_from \Zend\EventManager\EventManagerInterface::attach()

Parameters

$event

stringarray\Zend\EventManager\ListenerAggregateInterface

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

$callback

callableinteger

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

$priority

integer

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, integer $priority = 1) : mixed

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

inherited_from \Zend\EventManager\EventManagerInterface::attachAggregate()

Parameters

$aggregate

\Zend\EventManager\ListenerAggregateInterface

$priority

integer

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
inherited_from \Zend\EventManager\EventManagerInterface::clearListeners()

Parameters

$event

string

Unsubscribe a listener from an event

detach(\Zend\Stdlib\CallbackHandler|\Zend\EventManager\ListenerAggregateInterface $listener) : boolean
inherited_from \Zend\EventManager\EventManagerInterface::detach()

Parameters

$listener

\Zend\Stdlib\CallbackHandler\Zend\EventManager\ListenerAggregateInterface

Exceptions

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

Returns

booleanReturns 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.

inherited_from \Zend\EventManager\EventManagerInterface::detachAggregate()

Parameters

$aggregate

\Zend\EventManager\ListenerAggregateInterface

Returns

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

Retrieve all registered events

getEvents() : array
inherited_from \Zend\EventManager\EventManagerInterface::getEvents()

Returns

array

Get the identifier(s) for this EventManager

getIdentifiers() : array
inherited_from \Zend\EventManager\EventManagerInterface::getIdentifiers()

Returns

array

Retrieve all listeners for a given event

getListeners(string $event) : \Zend\Stdlib\PriorityQueue
inherited_from \Zend\EventManager\EventManagerInterface::getListeners()

Parameters

$event

string

Returns

\Zend\Stdlib\PriorityQueue

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

false\Zend\EventManager\SharedEventManagerInterface

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

Parameters

$args

array

Returns

\ArrayObject

Set the event class to utilize

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

Parameters

$class

string

Returns

\Zend\EventManager\EventManager

Set the identifiers (overrides any currently set identifiers)

setIdentifiers(string|integer|array|\Traversable $identifiers) : \Zend\EventManager\EventManager
inherited_from \Zend\EventManager\EventManagerInterface::setIdentifiers()

Parameters

$identifiers

stringintegerarray\Traversable

Returns

\Zend\EventManager\EventManagerProvides a fluent interface

Set shared event manager

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

Parameters

$sharedEventManager

\Zend\EventManager\SharedEventManagerInterface

Returns

\Zend\EventManager\EventManager

Trigger all listeners for a given event

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

Should allow handling the following scenarios:

  • Passing Event object only
  • Passing event name and Event object only
  • Passing event name, target, and Event object
  • Passing event name, target, and array|ArrayAccess of arguments
  • Passing event name, target, array|ArrayAccess of arguments, and callback
inherited_from \Zend\EventManager\EventManagerInterface::trigger()

Parameters

$event

string\Zend\EventManager\EventInterface

$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

nullcallable

Trigger listeners until return value of this callback evaluate to true

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|\Zend\EventManager\EventInterface $event, string|object $target, array|\ArrayAccess $argv = null, callable $callback = null) : \Zend\EventManager\ResponseCollection

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

deprecated Please use trigger()
inherited_from \Zend\EventManager\EventManagerInterface::triggerUntil()

Parameters

$event

string\Zend\EventManager\EventInterface

$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

callable

Exceptions

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

Returns

\Zend\EventManager\ResponseCollection

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, array|\Traversable $listeners) : void

Used to inject shared listeners and wildcard listeners.

Parameters

$masterListeners

\Zend\Stdlib\PriorityQueue

$listeners

array\Traversable

Trigger listeners

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

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

Parameters

$event

string

Event name

$e

\Zend\EventManager\EventInterface

$callback

nullcallable

Returns

\Zend\EventManager\ResponseCollection

 Properties

 

Class representing the event being emitted

$eventClass : string

Default

'Zend\EventManager\Event'
 

Subscribed events and their listeners

$events : array

Default

array()
 

Identifiers, used to pull shared signals from SharedEventManagerInterface instance

$identifiers : array

Default

array()
 

Shared event manager

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

Default

null