/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php

https://gitlab.com/Pasantias/pasantiasASLG · PHP · 372 lines · 241 code · 49 blank · 82 comment · 28 complexity · 68d6447d932517bc5e70afd39f3f0812 MD5 · raw file

  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. $info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
  134. $called[$eventName.'.'.$info['pretty']] = $info;
  135. }
  136. }
  137. return $called;
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function getNotCalledListeners()
  143. {
  144. try {
  145. $allListeners = $this->getListeners();
  146. } catch (\Exception $e) {
  147. if (null !== $this->logger) {
  148. $this->logger->info('An exception was thrown while getting the uncalled listeners.', array('exception' => $e));
  149. }
  150. // unable to retrieve the uncalled listeners
  151. return array();
  152. }
  153. $notCalled = array();
  154. foreach ($allListeners as $eventName => $listeners) {
  155. foreach ($listeners as $listener) {
  156. $called = false;
  157. if (isset($this->called[$eventName])) {
  158. foreach ($this->called[$eventName] as $l) {
  159. if ($l->getWrappedListener() === $listener) {
  160. $called = true;
  161. break;
  162. }
  163. }
  164. }
  165. if (!$called) {
  166. $info = $this->getListenerInfo($listener, $eventName);
  167. $notCalled[$eventName.'.'.$info['pretty']] = $info;
  168. }
  169. }
  170. }
  171. uasort($notCalled, array($this, 'sortListenersByPriority'));
  172. return $notCalled;
  173. }
  174. /**
  175. * Proxies all method calls to the original event dispatcher.
  176. *
  177. * @param string $method The method name
  178. * @param array $arguments The method arguments
  179. *
  180. * @return mixed
  181. */
  182. public function __call($method, $arguments)
  183. {
  184. return call_user_func_array(array($this->dispatcher, $method), $arguments);
  185. }
  186. /**
  187. * Called before dispatching the event.
  188. *
  189. * @param string $eventName The event name
  190. * @param Event $event The event
  191. */
  192. protected function preDispatch($eventName, Event $event)
  193. {
  194. }
  195. /**
  196. * Called after dispatching the event.
  197. *
  198. * @param string $eventName The event name
  199. * @param Event $event The event
  200. */
  201. protected function postDispatch($eventName, Event $event)
  202. {
  203. }
  204. private function preProcess($eventName)
  205. {
  206. foreach ($this->dispatcher->getListeners($eventName) as $listener) {
  207. $info = $this->getListenerInfo($listener, $eventName);
  208. $name = isset($info['class']) ? $info['class'] : $info['type'];
  209. $wrappedListener = new WrappedListener($listener, $name, $this->stopwatch, $this);
  210. $this->wrappedListeners[$eventName][] = $wrappedListener;
  211. $this->dispatcher->removeListener($eventName, $listener);
  212. $this->dispatcher->addListener($eventName, $wrappedListener, $info['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. $info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
  228. if ($listener->wasCalled()) {
  229. if (null !== $this->logger) {
  230. $this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty']));
  231. }
  232. if (!isset($this->called[$eventName])) {
  233. $this->called[$eventName] = new \SplObjectStorage();
  234. }
  235. $this->called[$eventName]->attach($listener);
  236. }
  237. if (null !== $this->logger && $skipped) {
  238. $this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', $info['pretty'], $eventName));
  239. }
  240. if ($listener->stoppedPropagation()) {
  241. if (null !== $this->logger) {
  242. $this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', $info['pretty'], $eventName));
  243. }
  244. $skipped = true;
  245. }
  246. }
  247. }
  248. /**
  249. * Returns information about the listener.
  250. *
  251. * @param object $listener The listener
  252. * @param string $eventName The event name
  253. *
  254. * @return array Information about the listener
  255. */
  256. private function getListenerInfo($listener, $eventName)
  257. {
  258. $info = array(
  259. 'event' => $eventName,
  260. 'priority' => $this->getListenerPriority($eventName, $listener),
  261. );
  262. if ($listener instanceof \Closure) {
  263. $info += array(
  264. 'type' => 'Closure',
  265. 'pretty' => 'closure',
  266. );
  267. } elseif (is_string($listener)) {
  268. try {
  269. $r = new \ReflectionFunction($listener);
  270. $file = $r->getFileName();
  271. $line = $r->getStartLine();
  272. } catch (\ReflectionException $e) {
  273. $file = null;
  274. $line = null;
  275. }
  276. $info += array(
  277. 'type' => 'Function',
  278. 'function' => $listener,
  279. 'file' => $file,
  280. 'line' => $line,
  281. 'pretty' => $listener,
  282. );
  283. } elseif (is_array($listener) || (is_object($listener) && is_callable($listener))) {
  284. if (!is_array($listener)) {
  285. $listener = array($listener, '__invoke');
  286. }
  287. $class = is_object($listener[0]) ? get_class($listener[0]) : $listener[0];
  288. try {
  289. $r = new \ReflectionMethod($class, $listener[1]);
  290. $file = $r->getFileName();
  291. $line = $r->getStartLine();
  292. } catch (\ReflectionException $e) {
  293. $file = null;
  294. $line = null;
  295. }
  296. $info += array(
  297. 'type' => 'Method',
  298. 'class' => $class,
  299. 'method' => $listener[1],
  300. 'file' => $file,
  301. 'line' => $line,
  302. 'pretty' => $class.'::'.$listener[1],
  303. );
  304. }
  305. return $info;
  306. }
  307. private function sortListenersByPriority($a, $b)
  308. {
  309. if (is_int($a['priority']) && !is_int($b['priority'])) {
  310. return 1;
  311. }
  312. if (!is_int($a['priority']) && is_int($b['priority'])) {
  313. return -1;
  314. }
  315. if ($a['priority'] === $b['priority']) {
  316. return 0;
  317. }
  318. if ($a['priority'] > $b['priority']) {
  319. return -1;
  320. }
  321. return 1;
  322. }
  323. }