TraceableEventDispatcher.php 9.7KB

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