This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
SharedEventManager.php
148 lines (138 loc) · 4.51 KB
/
SharedEventManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_EventManager
*/
namespace Zend\EventManager;
use Zend\Stdlib\CallbackHandler;
use Zend\Stdlib\PriorityQueue;
/**
* 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.
*
* @category Zend
* @package Zend_EventManager
*/
class SharedEventManager implements SharedEventManagerInterface
{
/**
* Identifiers with event connections
* @var array
*/
protected $identifiers = array();
/**
* Attach a listener to an event
*
* Allows attaching a callback to an event offered by one or more
* identifying components. As an example, the following connects to the
* "getAll" event of both an AbstractResource and EntityResource:
*
* <code>
* $sharedEventManager = new SharedEventManager();
* $sharedEventManager->attach(
* array('My\Resource\AbstractResource', 'My\Resource\EntityResource'),
* 'getAll',
* function ($e) use ($cache) {
* if (!$id = $e->getParam('id', false)) {
* return;
* }
* if (!$data = $cache->load(get_class($resource) . '::getOne::' . $id )) {
* return;
* }
* return $data;
* }
* );
* </code>
*
* @param string|array $id Identifier(s) for event emitting component(s)
* @param string $event
* @param callable $callback PHP Callback
* @param int $priority Priority at which listener should execute
* @return CallbackHandler|array Either CallbackHandler or array of CallbackHandlers
*/
public function attach($id, $event, $callback, $priority = 1)
{
$ids = (array) $id;
$listeners = array();
foreach ($ids as $id) {
if (!array_key_exists($id, $this->identifiers)) {
$this->identifiers[$id] = new EventManager();
}
$listeners[] = $this->identifiers[$id]->attach($event, $callback, $priority);
}
if (count($listeners) > 1) {
return $listeners;
}
return $listeners[0];
}
/**
* Detach a listener from an event offered by a given resource
*
* @param string|int $id
* @param CallbackHandler $listener
* @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found
*/
public function detach($id, CallbackHandler $listener)
{
if (!array_key_exists($id, $this->identifiers)) {
return false;
}
return $this->identifiers[$id]->detach($listener);
}
/**
* Retrieve all registered events for a given resource
*
* @param string|int $id
* @return array
*/
public function getEvents($id)
{
if (!array_key_exists($id, $this->identifiers)) {
//Check if there are any id wildcards listeners
if ('*' != $id && array_key_exists('*', $this->identifiers)) {
return $this->identifiers['*']->getEvents();
}
return false;
}
return $this->identifiers[$id]->getEvents();
}
/**
* Retrieve all listeners for a given identifier and event
*
* @param string|int $id
* @param string|int $event
* @return false|PriorityQueue
*/
public function getListeners($id, $event)
{
if (!array_key_exists($id, $this->identifiers)) {
return false;
}
return $this->identifiers[$id]->getListeners($event);
}
/**
* Clear all listeners for a given identifier, optionally for a specific event
*
* @param string|int $id
* @param null|string $event
* @return bool
*/
public function clearListeners($id, $event = null)
{
if (!array_key_exists($id, $this->identifiers)) {
return false;
}
if (null === $event) {
unset($this->identifiers[$id]);
return true;
}
return $this->identifiers[$id]->clearListeners($event);
}
}