/usr/share/php/Zend/EventManager/EventManager.php is in php-zend-eventmanager 3.0.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | <?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zend-eventmanager for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-eventmanager/blob/master/LICENSE.md
*/
namespace Zend\EventManager;
use ArrayObject;
/**
* Event manager: notification system
*
* Use the EventManager when you want to create a per-instance notification
* system for your objects.
*/
class EventManager implements EventManagerInterface
{
/**
* Subscribed events and their listeners
*
* @var array[]
*/
protected $events = [];
/**
* @var EventInterface Prototype to use when creating an event at trigger().
*/
protected $eventPrototype;
/**
* Identifiers, used to pull shared signals from SharedEventManagerInterface instance
*
* @var array
*/
protected $identifiers = [];
/**
* Shared event manager
*
* @var null|SharedEventManagerInterface
*/
protected $sharedManager = null;
/**
* Constructor
*
* Allows optionally specifying identifier(s) to use to pull signals from a
* SharedEventManagerInterface.
*
* @param SharedEventManagerInterface $sharedEventManager
* @param array $identifiers
*/
public function __construct(SharedEventManagerInterface $sharedEventManager = null, array $identifiers = [])
{
if ($sharedEventManager) {
$this->sharedManager = $sharedEventManager;
$this->setIdentifiers($identifiers);
}
$this->eventPrototype = new Event();
}
/**
* @inheritDoc
*/
public function setEventPrototype(EventInterface $prototype)
{
$this->eventPrototype = $prototype;
}
/**
* Retrieve the shared event manager, if composed.
*
* @return null|SharedEventManagerInterface $sharedEventManager
*/
public function getSharedManager()
{
return $this->sharedManager;
}
/**
* @inheritDoc
*/
public function getIdentifiers()
{
return $this->identifiers;
}
/**
* @inheritDoc
*/
public function setIdentifiers(array $identifiers)
{
$this->identifiers = array_unique($identifiers);
}
/**
* @inheritDoc
*/
public function addIdentifiers(array $identifiers)
{
$this->identifiers = array_unique(array_merge(
$this->identifiers,
$identifiers
));
}
/**
* @inheritDoc
*/
public function trigger($eventName, $target = null, $argv = [])
{
$event = clone $this->eventPrototype;
$event->setName($eventName);
$event->setTarget($target);
$event->setParams($argv);
return $this->triggerListeners($event);
}
/**
* @inheritDoc
*/
public function triggerUntil(callable $callback, $eventName, $target = null, $argv = [])
{
$event = clone $this->eventPrototype;
$event->setName($eventName);
$event->setTarget($target);
$event->setParams($argv);
return $this->triggerListeners($event, $callback);
}
/**
* @inheritDoc
*/
public function triggerEvent(EventInterface $event)
{
return $this->triggerListeners($event);
}
/**
* @inheritDoc
*/
public function triggerEventUntil(callable $callback, EventInterface $event)
{
return $this->triggerListeners($event, $callback);
}
/**
* @inheritDoc
*/
public function attach($eventName, callable $listener, $priority = 1)
{
if (! is_string($eventName)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects a string for the event; received %s',
__METHOD__,
(is_object($eventName) ? get_class($eventName) : gettype($eventName))
));
}
$this->events[$eventName][((int) $priority) . '.0'][] = $listener;
return $listener;
}
/**
* @inheritDoc
* @throws Exception\InvalidArgumentException for invalid event types.
*/
public function detach(callable $listener, $eventName = null, $force = false)
{
// If event is wildcard, we need to iterate through each listeners
if (null === $eventName || ('*' === $eventName && ! $force)) {
foreach (array_keys($this->events) as $eventName) {
$this->detach($listener, $eventName, true);
}
return;
}
if (! is_string($eventName)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects a string for the event; received %s',
__METHOD__,
(is_object($eventName) ? get_class($eventName) : gettype($eventName))
));
}
if (! isset($this->events[$eventName])) {
return;
}
foreach ($this->events[$eventName] as $priority => $listeners) {
foreach ($listeners as $index => $evaluatedListener) {
if ($evaluatedListener !== $listener) {
continue;
}
// Found the listener; remove it.
unset($this->events[$eventName][$priority][$index]);
// If the queue for the given priority is empty, remove it.
if (empty($this->events[$eventName][$priority])) {
unset($this->events[$eventName][$priority]);
break;
}
}
// If the queue for the given event is empty, remove it.
if (empty($this->events[$eventName])) {
unset($this->events[$eventName]);
break;
}
}
}
/**
* @inheritDoc
*/
public function clearListeners($event)
{
if (isset($this->events[$event])) {
unset($this->events[$event]);
}
}
/**
* Prepare arguments
*
* 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().
*
* @param array $args
* @return ArrayObject
*/
public function prepareArgs(array $args)
{
return new ArrayObject($args);
}
/**
* Trigger listeners
*
* Actual functionality for triggering listeners, to which trigger() delegate.
*
* @param EventInterface $event
* @param null|callable $callback
* @return ResponseCollection
*/
protected function triggerListeners(EventInterface $event, callable $callback = null)
{
$name = $event->getName();
if (empty($name)) {
throw new Exception\RuntimeException('Event is missing a name; cannot trigger!');
}
// Initial value of stop propagation flag should be false
$event->stopPropagation(false);
$responses = new ResponseCollection();
foreach ($this->getListenersByEventName($name) as $listener) {
$response = $listener($event);
$responses->push($response);
// If the event was asked to stop propagating, do so
if ($event->propagationIsStopped()) {
$responses->setStopped(true);
break;
}
// If the result causes our validation callback to return true,
// stop propagation
if ($callback && $callback($response)) {
$responses->setStopped(true);
break;
}
}
return $responses;
}
/**
* Get listeners for the currently triggered event.
*
* @param string $eventName
* @return callable[]
*/
private function getListenersByEventName($eventName)
{
$listeners = array_merge_recursive(
isset($this->events[$eventName]) ? $this->events[$eventName] : [],
isset($this->events['*']) ? $this->events['*'] : [],
$this->sharedManager ? $this->sharedManager->getListeners($this->identifiers, $eventName) : []
);
krsort($listeners, SORT_NUMERIC);
$listenersForEvent = [];
foreach ($listeners as $priority => $listenersByPriority) {
foreach ($listenersByPriority as $listener) {
// Performance note: after some testing, it appears that accumulating listeners and sending
// them at the end of the method is FASTER than using generators (ie. yielding)
$listenersForEvent[] = $listener;
}
}
return $listenersForEvent;
}
}
|