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