SharedEventManager
in package
implements
SharedEventManagerInterface
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.
Interfaces, Classes, Traits and Enums
- SharedEventManagerInterface
- Interface for shared event listener collections
Table of Contents
- $identifiers : array<string|int, mixed>
- Identifiers with event connections
- attach() : void
- Attach a listener to an event emitted by components with specific identifiers.
- clearListeners() : mixed
- Clear all listeners for a given identifier, optionally for a specific event
- detach() : mixed
- Detach a shared listener.
- getListeners() : array<string|int, array<string|int, mixed>>
- Retrieve all listeners for a given identifier and event
Properties
$identifiers
Identifiers with event connections
protected
array<string|int, mixed>
$identifiers
= []
Methods
attach()
Attach a listener to an event emitted by components with specific identifiers.
public
attach(string $identifier, string $event, callable $listener[, int $priority = 1 ]) : void
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;
}
);
}
Parameters
- $identifier : string
-
Identifier for event emitting component.
- $event : string
- $listener : callable
-
Listener that will handle the event.
- $priority : int = 1
-
Priority at which listener should execute
Tags
Return values
void —clearListeners()
Clear all listeners for a given identifier, optionally for a specific event
public
clearListeners(mixed $identifier[, mixed $eventName = null ]) : mixed
Parameters
- $identifier : mixed
- $eventName : mixed = null
Tags
Return values
mixed —detach()
Detach a shared listener.
public
detach(callable $listener[, mixed $identifier = null ][, mixed $eventName = null ][, mixed $force = false ]) : mixed
Parameters
- $listener : callable
-
Listener to detach.
- $identifier : mixed = null
-
Identifier from which to detach; null indicates all registered identifiers.
- $eventName : mixed = null
-
Event from which to detach; null indicates all registered events.
- $force : mixed = false
Tags
Return values
mixed —getListeners()
Retrieve all listeners for a given identifier and event
public
getListeners(array<string|int, string> $identifiers, string $eventName) : array<string|int, array<string|int, mixed>>
Parameters
- $identifiers : array<string|int, string>
- $eventName : string