TraceableEventDispatcher.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. return $this->dispatcher->getListenerPriority($eventName, $listener);
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function hasListeners($eventName = null)
  100. {
  101. return $this->dispatcher->hasListeners($eventName);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function dispatch($eventName, Event $event = null)
  107. {
  108. if (null === $event) {
  109. $event = new Event();
  110. }
  111. if (null !== $this->logger && $event->isPropagationStopped()) {
  112. $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
  113. }
  114. $this->preProcess($eventName);
  115. $this->preDispatch($eventName, $event);
  116. $e = $this->stopwatch->start($eventName, 'section');
  117. $this->dispatcher->dispatch($eventName, $event);
  118. if ($e->isStarted()) {
  119. $e->stop();
  120. }
  121. $this->postDispatch($eventName, $event);
  122. $this->postProcess($eventName);
  123. return $event;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function getCalledListeners()
  129. {
  130. $called = array();
  131. foreach ($this->called as $eventName => $listeners) {
  132. foreach ($listeners as $listener) {
  133. $called[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
  134. }
  135. }
  136. return $called;
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function getNotCalledListeners()
  142. {
  143. try {
  144. $allListeners = $this->getListeners();
  145. } catch (\Exception $e) {
  146. if (null !== $this->logger) {
  147. $this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
  148. }
  149. // unable to retrieve the uncalled listeners
  150. return array();
  151. }
  152. $notCalled = array();
  153. foreach ($allListeners as $eventName => $listeners) {
  154. foreach ($listeners as $listener) {
  155. $called = false;
  156. if (isset($this->called[$eventName])) {
  157. foreach ($this->called[$eventName] as $l) {
  158. if ($l->getWrappedListener() === $listener) {
  159. $called = true;
  160. break;
  161. }
  162. }
  163. }
  164. if (!$called) {
  165. if (!$listener instanceof WrappedListener) {
  166. $listener = new WrappedListener($listener, null, $this->stopwatch, $this);
  167. }
  168. $notCalled[$eventName.'.'.$listener->getPretty()] = $listener->getInfo($eventName);
  169. }
  170. }
  171. }
  172. uasort($notCalled, array($this, 'sortListenersByPriority'));
  173. return $notCalled;
  174. }
  175. /**
  176. * Proxies all method calls to the original event dispatcher.
  177. *
  178. * @param string $method The method name
  179. * @param array $arguments The method arguments
  180. *
  181. * @return mixed
  182. */
  183. public function __call($method, $arguments)
  184. {
  185. return call_user_func_array(array($this->dispatcher, $method), $arguments);
  186. }
  187. /**
  188. * Called before dispatching the event.
  189. *
  190. * @param string $eventName The event name
  191. * @param Event $event The event
  192. */
  193. protected function preDispatch($eventName, Event $event)
  194. {
  195. }
  196. /**
  197. * Called after dispatching the event.
  198. *
  199. * @param string $eventName The event name
  200. * @param Event $event The event
  201. */
  202. protected function postDispatch($eventName, Event $event)
  203. {
  204. }
  205. private function preProcess($eventName)
  206. {
  207. foreach ($this->dispatcher->getListeners($eventName) as $listener) {
  208. $priority = $this->getListenerPriority($eventName, $listener);
  209. $wrappedListener = new WrappedListener($listener, null, $this->stopwatch, $this);
  210. $this->wrappedListeners[$eventName][] = $wrappedListener;
  211. $this->dispatcher->removeListener($eventName, $listener);
  212. $this->dispatcher->addListener($eventName, $wrappedListener, $priority);
  213. }
  214. }
  215. private function postProcess($eventName)
  216. {
  217. unset($this->wrappedListeners[$eventName]);
  218. $skipped = false;
  219. foreach ($this->dispatcher->getListeners($eventName) as $listener) {
  220. if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
  221. continue;
  222. }
  223. // Unwrap listener
  224. $priority = $this->getListenerPriority($eventName, $listener);
  225. $this->dispatcher->removeListener($eventName, $listener);
  226. $this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
  227. if (null !== $this->logger) {
  228. $context = array('event' => $eventName, 'listener' => $listener->getPretty());
  229. }
  230. if ($listener->wasCalled()) {
  231. if (null !== $this->logger) {
  232. $this->logger->debug('Notified event "{event}" to listener "{listener}".', $context);
  233. }
  234. if (!isset($this->called[$eventName])) {
  235. $this->called[$eventName] = new \SplObjectStorage();
  236. }
  237. $this->called[$eventName]->attach($listener);
  238. }
  239. if (null !== $this->logger && $skipped) {
  240. $this->logger->debug('Listener "{listener}" was not called for event "{event}".', $context);
  241. }
  242. if ($listener->stoppedPropagation()) {
  243. if (null !== $this->logger) {
  244. $this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
  245. }
  246. $skipped = true;
  247. }
  248. }
  249. }
  250. private function sortListenersByPriority($a, $b)
  251. {
  252. if (is_int($a['priority']) && !is_int($b['priority'])) {
  253. return 1;
  254. }
  255. if (!is_int($a['priority']) && is_int($b['priority'])) {
  256. return -1;
  257. }
  258. if ($a['priority'] === $b['priority']) {
  259. return 0;
  260. }
  261. if ($a['priority'] > $b['priority']) {
  262. return -1;
  263. }
  264. return 1;
  265. }
  266. }