TraceableEventDispatcher.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 Psr\Log\LoggerInterface;
  12. use Symfony\Component\EventDispatcher\Event;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Stopwatch\Stopwatch;
  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. try {
  120. $this->preDispatch($eventName, $event);
  121. try {
  122. $e = $this->stopwatch->start($eventName, 'section');
  123. try {
  124. $this->dispatcher->dispatch($eventName, $event);
  125. } finally {
  126. if ($e->isStarted()) {
  127. $e->stop();
  128. }
  129. }
  130. } finally {
  131. $this->postDispatch($eventName, $event);
  132. }
  133. } finally {
  134. $this->postProcess($eventName);
  135. }
  136. return $event;
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function getCalledListeners()
  142. {
  143. $called = array();
  144. foreach ($this->called as $eventName => $listeners) {
  145. foreach ($listeners as $listener) {
  146. $called[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
  147. }
  148. }
  149. return $called;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getNotCalledListeners()
  155. {
  156. try {
  157. $allListeners = $this->getListeners();
  158. } catch (\Exception $e) {
  159. if (null !== $this->logger) {
  160. $this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
  161. }
  162. // unable to retrieve the uncalled listeners
  163. return array();
  164. }
  165. $notCalled = array();
  166. foreach ($allListeners as $eventName => $listeners) {
  167. foreach ($listeners as $listener) {
  168. $called = false;
  169. if (isset($this->called[$eventName])) {
  170. foreach ($this->called[$eventName] as $l) {
  171. if ($l->getWrappedListener() === $listener) {
  172. $called = true;
  173. break;
  174. }
  175. }
  176. }
  177. if (!$called) {
  178. if (!$listener instanceof WrappedListener) {
  179. $listener = new WrappedListener($listener, null, $this->stopwatch, $this);
  180. }
  181. $notCalled[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
  182. }
  183. }
  184. }
  185. uasort($notCalled, array($this, 'sortListenersByPriority'));
  186. return $notCalled;
  187. }
  188. public function getOrphanedEvents(): array
  189. {
  190. return $this->orphanedEvents;
  191. }
  192. public function reset()
  193. {
  194. $this->called = array();
  195. $this->orphanedEvents = array();
  196. }
  197. /**
  198. * Proxies all method calls to the original event dispatcher.
  199. *
  200. * @param string $method The method name
  201. * @param array $arguments The method arguments
  202. *
  203. * @return mixed
  204. */
  205. public function __call($method, $arguments)
  206. {
  207. return $this->dispatcher->{$method}(...$arguments);
  208. }
  209. /**
  210. * Called before dispatching the event.
  211. *
  212. * @param string $eventName The event name
  213. * @param Event $event The event
  214. */
  215. protected function preDispatch($eventName, Event $event)
  216. {
  217. }
  218. /**
  219. * Called after dispatching the event.
  220. *
  221. * @param string $eventName The event name
  222. * @param Event $event The event
  223. */
  224. protected function postDispatch($eventName, Event $event)
  225. {
  226. }
  227. private function preProcess($eventName)
  228. {
  229. if (!$this->dispatcher->hasListeners($eventName)) {
  230. $this->orphanedEvents[] = $eventName;
  231. return;
  232. }
  233. foreach ($this->dispatcher->getListeners($eventName) as $listener) {
  234. $priority = $this->getListenerPriority($eventName, $listener);
  235. $wrappedListener = new WrappedListener($listener instanceof WrappedListener ? $listener->getWrappedListener() : $listener, null, $this->stopwatch, $this);
  236. $this->wrappedListeners[$eventName][] = $wrappedListener;
  237. $this->dispatcher->removeListener($eventName, $listener);
  238. $this->dispatcher->addListener($eventName, $wrappedListener, $priority);
  239. }
  240. }
  241. private function postProcess($eventName)
  242. {
  243. unset($this->wrappedListeners[$eventName]);
  244. $skipped = false;
  245. foreach ($this->dispatcher->getListeners($eventName) as $listener) {
  246. if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
  247. continue;
  248. }
  249. // Unwrap listener
  250. $priority = $this->getListenerPriority($eventName, $listener);
  251. $this->dispatcher->removeListener($eventName, $listener);
  252. $this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
  253. if (null !== $this->logger) {
  254. $context = array('event' => $eventName, 'listener' => $listener->getPretty());
  255. }
  256. if ($listener->wasCalled()) {
  257. if (null !== $this->logger) {
  258. $this->logger->debug('Notified event "{event}" to listener "{listener}".', $context);
  259. }
  260. if (!isset($this->called[$eventName])) {
  261. $this->called[$eventName] = new \SplObjectStorage();
  262. }
  263. $this->called[$eventName]->attach($listener);
  264. }
  265. if (null !== $this->logger && $skipped) {
  266. $this->logger->debug('Listener "{listener}" was not called for event "{event}".', $context);
  267. }
  268. if ($listener->stoppedPropagation()) {
  269. if (null !== $this->logger) {
  270. $this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
  271. }
  272. $skipped = true;
  273. }
  274. }
  275. }
  276. private function sortListenersByPriority($a, $b)
  277. {
  278. if (\is_int($a['priority']) && !\is_int($b['priority'])) {
  279. return 1;
  280. }
  281. if (!\is_int($a['priority']) && \is_int($b['priority'])) {
  282. return -1;
  283. }
  284. if ($a['priority'] === $b['priority']) {
  285. return 0;
  286. }
  287. if ($a['priority'] > $b['priority']) {
  288. return -1;
  289. }
  290. return 1;
  291. }
  292. }