composer package updates
[openemr.git] / vendor / symfony / event-dispatcher / Debug / TraceableEventDispatcher.php
blobdb76bb845021a465aaaea4790df03c7459b616d1
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\EventDispatcher\Debug;
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16 use Symfony\Component\EventDispatcher\Event;
17 use Symfony\Component\Stopwatch\Stopwatch;
18 use Psr\Log\LoggerInterface;
20 /**
21 * Collects some data about event listeners.
23 * This event dispatcher delegates the dispatching to another one.
25 * @author Fabien Potencier <fabien@symfony.com>
27 class TraceableEventDispatcher implements TraceableEventDispatcherInterface
29 protected $logger;
30 protected $stopwatch;
32 private $called;
33 private $dispatcher;
34 private $wrappedListeners;
36 public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null)
38 $this->dispatcher = $dispatcher;
39 $this->stopwatch = $stopwatch;
40 $this->logger = $logger;
41 $this->called = array();
42 $this->wrappedListeners = array();
45 /**
46 * {@inheritdoc}
48 public function addListener($eventName, $listener, $priority = 0)
50 $this->dispatcher->addListener($eventName, $listener, $priority);
53 /**
54 * {@inheritdoc}
56 public function addSubscriber(EventSubscriberInterface $subscriber)
58 $this->dispatcher->addSubscriber($subscriber);
61 /**
62 * {@inheritdoc}
64 public function removeListener($eventName, $listener)
66 if (isset($this->wrappedListeners[$eventName])) {
67 foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
68 if ($wrappedListener->getWrappedListener() === $listener) {
69 $listener = $wrappedListener;
70 unset($this->wrappedListeners[$eventName][$index]);
71 break;
76 return $this->dispatcher->removeListener($eventName, $listener);
79 /**
80 * {@inheritdoc}
82 public function removeSubscriber(EventSubscriberInterface $subscriber)
84 return $this->dispatcher->removeSubscriber($subscriber);
87 /**
88 * {@inheritdoc}
90 public function getListeners($eventName = null)
92 return $this->dispatcher->getListeners($eventName);
95 /**
96 * {@inheritdoc}
98 public function getListenerPriority($eventName, $listener)
100 if (!method_exists($this->dispatcher, 'getListenerPriority')) {
101 return 0;
104 return $this->dispatcher->getListenerPriority($eventName, $listener);
108 * {@inheritdoc}
110 public function hasListeners($eventName = null)
112 return $this->dispatcher->hasListeners($eventName);
116 * {@inheritdoc}
118 public function dispatch($eventName, Event $event = null)
120 if (null === $event) {
121 $event = new Event();
124 if (null !== $this->logger && $event->isPropagationStopped()) {
125 $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
128 $this->preProcess($eventName);
129 $this->preDispatch($eventName, $event);
131 $e = $this->stopwatch->start($eventName, 'section');
133 $this->dispatcher->dispatch($eventName, $event);
135 if ($e->isStarted()) {
136 $e->stop();
139 $this->postDispatch($eventName, $event);
140 $this->postProcess($eventName);
142 return $event;
146 * {@inheritdoc}
148 public function getCalledListeners()
150 $called = array();
151 foreach ($this->called as $eventName => $listeners) {
152 foreach ($listeners as $listener) {
153 $info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
154 $called[$eventName.'.'.$info['pretty']] = $info;
158 return $called;
162 * {@inheritdoc}
164 public function getNotCalledListeners()
166 try {
167 $allListeners = $this->getListeners();
168 } catch (\Exception $e) {
169 if (null !== $this->logger) {
170 $this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
173 // unable to retrieve the uncalled listeners
174 return array();
177 $notCalled = array();
178 foreach ($allListeners as $eventName => $listeners) {
179 foreach ($listeners as $listener) {
180 $called = false;
181 if (isset($this->called[$eventName])) {
182 foreach ($this->called[$eventName] as $l) {
183 if ($l->getWrappedListener() === $listener) {
184 $called = true;
186 break;
191 if (!$called) {
192 $info = $this->getListenerInfo($listener, $eventName);
193 $notCalled[$eventName.'.'.$info['pretty']] = $info;
198 uasort($notCalled, array($this, 'sortListenersByPriority'));
200 return $notCalled;
204 * Proxies all method calls to the original event dispatcher.
206 * @param string $method The method name
207 * @param array $arguments The method arguments
209 * @return mixed
211 public function __call($method, $arguments)
213 return call_user_func_array(array($this->dispatcher, $method), $arguments);
217 * Called before dispatching the event.
219 * @param string $eventName The event name
220 * @param Event $event The event
222 protected function preDispatch($eventName, Event $event)
227 * Called after dispatching the event.
229 * @param string $eventName The event name
230 * @param Event $event The event
232 protected function postDispatch($eventName, Event $event)
236 private function preProcess($eventName)
238 foreach ($this->dispatcher->getListeners($eventName) as $listener) {
239 $info = $this->getListenerInfo($listener, $eventName);
240 $name = isset($info['class']) ? $info['class'] : $info['type'];
241 $wrappedListener = new WrappedListener($listener, $name, $this->stopwatch, $this);
242 $this->wrappedListeners[$eventName][] = $wrappedListener;
243 $this->dispatcher->removeListener($eventName, $listener);
244 $this->dispatcher->addListener($eventName, $wrappedListener, $info['priority']);
248 private function postProcess($eventName)
250 unset($this->wrappedListeners[$eventName]);
251 $skipped = false;
252 foreach ($this->dispatcher->getListeners($eventName) as $listener) {
253 if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
254 continue;
256 // Unwrap listener
257 $priority = $this->getListenerPriority($eventName, $listener);
258 $this->dispatcher->removeListener($eventName, $listener);
259 $this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
261 $info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
262 if ($listener->wasCalled()) {
263 if (null !== $this->logger) {
264 $this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty']));
267 if (!isset($this->called[$eventName])) {
268 $this->called[$eventName] = new \SplObjectStorage();
271 $this->called[$eventName]->attach($listener);
274 if (null !== $this->logger && $skipped) {
275 $this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', $info['pretty'], $eventName));
278 if ($listener->stoppedPropagation()) {
279 if (null !== $this->logger) {
280 $this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', $info['pretty'], $eventName));
283 $skipped = true;
289 * Returns information about the listener.
291 * @param object $listener The listener
292 * @param string $eventName The event name
294 * @return array Information about the listener
296 private function getListenerInfo($listener, $eventName)
298 $info = array(
299 'event' => $eventName,
300 'priority' => $this->getListenerPriority($eventName, $listener),
303 // unwrap for correct listener info
304 if ($listener instanceof WrappedListener) {
305 $listener = $listener->getWrappedListener();
308 if ($listener instanceof \Closure) {
309 $info += array(
310 'type' => 'Closure',
311 'pretty' => 'closure',
313 } elseif (is_string($listener)) {
314 try {
315 $r = new \ReflectionFunction($listener);
316 $file = $r->getFileName();
317 $line = $r->getStartLine();
318 } catch (\ReflectionException $e) {
319 $file = null;
320 $line = null;
322 $info += array(
323 'type' => 'Function',
324 'function' => $listener,
325 'file' => $file,
326 'line' => $line,
327 'pretty' => $listener,
329 } elseif (is_array($listener) || (is_object($listener) && is_callable($listener))) {
330 if (!is_array($listener)) {
331 $listener = array($listener, '__invoke');
333 $class = is_object($listener[0]) ? get_class($listener[0]) : $listener[0];
334 try {
335 $r = new \ReflectionMethod($class, $listener[1]);
336 $file = $r->getFileName();
337 $line = $r->getStartLine();
338 } catch (\ReflectionException $e) {
339 $file = null;
340 $line = null;
342 $info += array(
343 'type' => 'Method',
344 'class' => $class,
345 'method' => $listener[1],
346 'file' => $file,
347 'line' => $line,
348 'pretty' => $class.'::'.$listener[1],
352 return $info;
355 private function sortListenersByPriority($a, $b)
357 if (is_int($a['priority']) && !is_int($b['priority'])) {
358 return 1;
361 if (!is_int($a['priority']) && is_int($b['priority'])) {
362 return -1;
365 if ($a['priority'] === $b['priority']) {
366 return 0;
369 if ($a['priority'] > $b['priority']) {
370 return -1;
373 return 1;