TraceableEventDispatcher.php 9.6 KB

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