TraceableEventDispatcher.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\EventDispatcher\Debug;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\EventDispatcher\Event;
  14. use Symfony\Component\Stopwatch\Stopwatch;
  15. use Psr\Log\LoggerInterface;
  16. /**
  17. * Collects some data about event listeners.
  18. *
  19. * This event dispatcher delegates the dispatching to another one.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class TraceableEventDispatcher implements TraceableEventDispatcherInterface
  24. {
  25. protected $logger;
  26. protected $stopwatch;
  27. private $called;
  28. private $dispatcher;
  29. private $wrappedListeners;
  30. /**
  31. * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
  32. * @param Stopwatch $stopwatch A Stopwatch instance
  33. * @param LoggerInterface $logger A LoggerInterface instance
  34. */
  35. public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null)
  36. {
  37. $this->dispatcher = $dispatcher;
  38. $this->stopwatch = $stopwatch;
  39. $this->logger = $logger;
  40. $this->called = array();
  41. $this->wrappedListeners = array();
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function addListener($eventName, $listener, $priority = 0)
  47. {
  48. $this->dispatcher->addListener($eventName, $listener, $priority);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function addSubscriber(EventSubscriberInterface $subscriber)
  54. {
  55. $this->dispatcher->addSubscriber($subscriber);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function removeListener($eventName, $listener)
  61. {
  62. if (isset($this->wrappedListeners[$eventName])) {
  63. foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
  64. if ($wrappedListener->getWrappedListener() === $listener) {
  65. $listener = $wrappedListener;
  66. unset($this->wrappedListeners[$eventName][$index]);
  67. break;
  68. }
  69. }
  70. }
  71. return $this->dispatcher->removeListener($eventName, $listener);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function removeSubscriber(EventSubscriberInterface $subscriber)
  77. {
  78. return $this->dispatcher->removeSubscriber($subscriber);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getListeners($eventName = null)
  84. {
  85. return $this->dispatcher->getListeners($eventName);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getListenerPriority($eventName, $listener)
  91. {
  92. // we might have wrapped listeners for the event (if called while dispatching)
  93. // in that case get the priority by wrapper
  94. if (isset($this->wrappedListeners[$eventName])) {
  95. foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
  96. if ($wrappedListener->getWrappedListener() === $listener) {
  97. return $this->dispatcher->getListenerPriority($eventName, $wrappedListener);
  98. }
  99. }
  100. }
  101. return $this->dispatcher->getListenerPriority($eventName, $listener);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function hasListeners($eventName = null)
  107. {
  108. return $this->dispatcher->hasListeners($eventName);
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function dispatch($eventName, Event $event = null)
  114. {
  115. if (null === $event) {
  116. $event = new Event();
  117. }
  118. if (null !== $this->logger && $event->isPropagationStopped()) {
  119. $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
  120. }
  121. $this->preProcess($eventName);
  122. $this->preDispatch($eventName, $event);
  123. $e = $this->stopwatch->start($eventName, 'section');
  124. $this->dispatcher->dispatch($eventName, $event);
  125. if ($e->isStarted()) {
  126. $e->stop();
  127. }
  128. $this->postDispatch($eventName, $event);
  129. $this->postProcess($eventName);
  130. return $event;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function getCalledListeners()
  136. {
  137. $called = array();
  138. foreach ($this->called as $eventName => $listeners) {
  139. foreach ($listeners as $listener) {
  140. $called[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
  141. }
  142. }
  143. return $called;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function getNotCalledListeners()
  149. {
  150. try {
  151. $allListeners = $this->getListeners();
  152. } catch (\Exception $e) {
  153. if (null !== $this->logger) {
  154. $this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
  155. }
  156. // unable to retrieve the uncalled listeners
  157. return array();
  158. }
  159. $notCalled = array();
  160. foreach ($allListeners as $eventName => $listeners) {
  161. foreach ($listeners as $listener) {
  162. $called = false;
  163. if (isset($this->called[$eventName])) {
  164. foreach ($this->called[$eventName] as $l) {
  165. if ($l->getWrappedListener() === $listener) {
  166. $called = true;
  167. break;
  168. }
  169. }
  170. }
  171. if (!$called) {
  172. if (!$listener instanceof WrappedListener) {
  173. $listener = new WrappedListener($listener, null, $this->stopwatch, $this);
  174. }
  175. $notCalled[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
  176. }
  177. }
  178. }
  179. uasort($notCalled, array($this, 'sortListenersByPriority'));
  180. return $notCalled;
  181. }
  182. /**
  183. * Proxies all method calls to the original event dispatcher.
  184. *
  185. * @param string $method The method name
  186. * @param array $arguments The method arguments
  187. *
  188. * @return mixed
  189. */
  190. public function __call($method, $arguments)
  191. {
  192. return call_user_func_array(array($this->dispatcher, $method), $arguments);
  193. }
  194. /**
  195. * Called before dispatching the event.
  196. *
  197. * @param string $eventName The event name
  198. * @param Event $event The event
  199. */
  200. protected function preDispatch($eventName, Event $event)
  201. {
  202. }
  203. /**
  204. * Called after dispatching the event.
  205. *
  206. * @param string $eventName The event name
  207. * @param Event $event The event
  208. */
  209. protected function postDispatch($eventName, Event $event)
  210. {
  211. }
  212. private function preProcess($eventName)
  213. {
  214. foreach ($this->dispatcher->getListeners($eventName) as $listener) {
  215. $priority = $this->getListenerPriority($eventName, $listener);
  216. $wrappedListener = new WrappedListener($listener, null, $this->stopwatch, $this);
  217. $this->wrappedListeners[$eventName][] = $wrappedListener;
  218. $this->dispatcher->removeListener($eventName, $listener);
  219. $this->dispatcher->addListener($eventName, $wrappedListener, $priority);
  220. }
  221. }
  222. private function postProcess($eventName)
  223. {
  224. unset($this->wrappedListeners[$eventName]);
  225. $skipped = false;
  226. foreach ($this->dispatcher->getListeners($eventName) as $listener) {
  227. if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
  228. continue;
  229. }
  230. // Unwrap listener
  231. $priority = $this->getListenerPriority($eventName, $listener);
  232. $this->dispatcher->removeListener($eventName, $listener);
  233. $this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
  234. if (null !== $this->logger) {
  235. $context = array('event' => $eventName, 'listener' => $listener->getPretty());
  236. }
  237. if ($listener->wasCalled()) {
  238. if (null !== $this->logger) {
  239. $this->logger->debug('Notified event "{event}" to listener "{listener}".', $context);
  240. }
  241. if (!isset($this->called[$eventName])) {
  242. $this->called[$eventName] = new \SplObjectStorage();
  243. }
  244. $this->called[$eventName]->attach($listener);
  245. }
  246. if (null !== $this->logger && $skipped) {
  247. $this->logger->debug('Listener "{listener}" was not called for event "{event}".', $context);
  248. }
  249. if ($listener->stoppedPropagation()) {
  250. if (null !== $this->logger) {
  251. $this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
  252. }
  253. $skipped = true;
  254. }
  255. }
  256. }
  257. private function sortListenersByPriority($a, $b)
  258. {
  259. if (is_int($a['priority']) && !is_int($b['priority'])) {
  260. return 1;
  261. }
  262. if (!is_int($a['priority']) && is_int($b['priority'])) {
  263. return -1;
  264. }
  265. if ($a['priority'] === $b['priority']) {
  266. return 0;
  267. }
  268. if ($a['priority'] > $b['priority']) {
  269. return -1;
  270. }
  271. return 1;
  272. }
  273. }