TraceableEventDispatcher.php 10.0 KB

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