Class SharedEventManager

Summary

Fully Qualified Name: Zend\EventManager\SharedEventManager
Implements: SharedEventManagerInterface

Description

Shared/contextual EventManager

Allows attaching to EMs composed by other classes without having an instance first. The assumption is that the SharedEventManager will be injected into EventManager instances, and then queried for additional listeners when triggering an event.

Methods

Name Description Defined By
attach() Attach a listener to an event emitted by components with specific identifiers. SharedEventManager
clearListeners() SharedEventManager
detach() SharedEventManager
getListeners() Retrieve all listeners for a given identifier and event SharedEventManager

Method Details

attach()

Attach a listener to an event emitted by components with specific identifiers.

Allows attaching a listener to an event offered by an identifying components. As an example, the following connects to the "getAll" event of both an AbstractResource and EntityResource:

$sharedEventManager = new SharedEventManager(); foreach (['My\Resource\AbstractResource', 'My\Resource\EntityResource'] as $identifier) {

$sharedEventManager->attach(
    $identifier,
    'getAll',
    function ($e) use ($cache) {
        if (!$id = $e->getParam('id', false)) {
            return;
        }
        if (!$data = $cache->load(get_class($resource) . '::getOne::' . $id )) {
            return;
        }
        return $data;
    }
);

}

Parameter Name Type Description
$identifier string Identifier
$event string
$listener callable Listener
$priority int Priority

Returns: void

clearListeners()

Parameter Name Type Description
$identifier
$eventName

Returns:

detach()

Parameter Name Type Description
$listener
$identifier
$eventName
$force

Returns:

getListeners()

Retrieve all listeners for a given identifier and event

Parameter Name Type Description
$identifiers string[]
$eventName string

Returns: array[]

Top