/usr/share/php/Kdyby/Events/NamespacedEventManager.php is in php-kdyby-events 2.4.0-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 | <?php
/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka (filip@prochazka.su)
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/
namespace Kdyby\Events;
use Doctrine;
use Kdyby;
use Nette;
/**
* @author Filip Procházka <filip@prochazka.su>
*/
class NamespacedEventManager extends EventManager
{
/**
* @var bool
*/
public $dispatchGlobalEvents = FALSE;
/**
* @var EventManager
*/
private $evm;
/**
* @var string
*/
private $namespace;
public function __construct($namespace, EventManager $eventManager)
{
$this->namespace = $namespace;
$this->evm = $eventManager;
}
/**
* {@inheritDoc}
*/
public function dispatchEvent($eventName, Doctrine\Common\EventArgs $eventArgs = null)
{
list($ns, $event) = Event::parseName($eventName);
$this->evm->dispatchEvent($ns !== NULL ? $eventName : $this->namespace . $eventName, $eventArgs);
if ($this->dispatchGlobalEvents && $this->evm->hasListeners($event)) {
$this->evm->dispatchEvent($event, $eventArgs);
}
}
/**
* {@inheritDoc}
*/
public function getListeners($eventName = null)
{
if ($eventName === NULL) {
$listeners = array();
foreach ($this->evm->getListeners(NULL, TRUE) as $eventName => $subscribers) {
list($ns, $event) = Event::parseName($eventName);
if ($ns === NULL || stripos($this->namespace, $ns) !== FALSE) {
$listeners[$eventName] = $subscribers;
}
}
return $listeners;
}
list($ns, $event) = Event::parseName($eventName);
if ($ns !== NULL) {
throw new InvalidArgumentException("Unexpected event with namespace.");
}
return array_merge(
$this->evm->getListeners($event),
$this->evm->getListeners($this->namespace . $event)
);
}
/**
* {@inheritDoc}
*/
public function hasListeners($eventName)
{
list($ns, $event) = Event::parseName($eventName);
if ($ns) {
return $this->evm->hasListeners($eventName) || $this->evm->hasListeners($event);
}
return $this->evm->hasListeners($this->namespace . $eventName) || $this->evm->hasListeners($eventName);
}
/**
* {@inheritDoc}
*/
public function addEventListener($events, $subscriber, $priority = 0)
{
foreach ((array) $events as $eventName) {
list($ns, $event) = Event::parseName($eventName);
$this->evm->addEventListener(array($ns === NULL ? $this->namespace . $event : $eventName), $subscriber);
}
}
/**
* {@inheritDoc}
*/
public function removeEventListener($unsubscribe, $subscriber = NULL)
{
if ($unsubscribe instanceof Doctrine\Common\EventSubscriber) {
$subscriber = $unsubscribe;
$unsubscribe = array();
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
if ((is_array($params) && is_array($params[0])) || !is_numeric($eventName)) {
// [EventName => [[method, priority], ...], ...]
// [EventName => [method, priority], ...] && [EventName => method, .
$unsubscribe[] = $eventName;
} else { // [EventName, ...]
$unsubscribe[] = $params;
}
}
}
$namespace = $this->namespace;
$unsubscribe = array_map(function ($eventName) use ($namespace) {
list($ns, $event) = Event::parseName($eventName);
return $ns === NULL ? $namespace . $event : $eventName;
}, (array) $unsubscribe);
$this->evm->removeEventListener($unsubscribe, $subscriber);
}
/**
* {@inheritDoc}
*/
public function setExceptionHandler(IExceptionHandler $exceptionHandler)
{
throw new NotSupportedException;
}
/**
* {@inheritDoc}
*/
public function createEvent($name, $defaults = array(), $argsClass = NULL)
{
return $this->evm->createEvent($this->namespace . $name, $defaults, $argsClass);
}
}
|