TraceableEventDispatcher.php 9.6 KB

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