TraceableEventDispatcher.php 10 KB

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