You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
11 KiB

3 years ago
  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\EventDispatcher\StoppableEventInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\Stopwatch\Stopwatch;
  18. use Symfony\Contracts\Service\ResetInterface;
  19. /**
  20. * Collects some data about event listeners.
  21. *
  22. * This event dispatcher delegates the dispatching to another one.
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. */
  26. class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterface
  27. {
  28. protected $logger;
  29. protected $stopwatch;
  30. private $callStack;
  31. private $dispatcher;
  32. private $wrappedListeners;
  33. private $orphanedEvents;
  34. private $requestStack;
  35. private $currentRequestHash = '';
  36. public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null, RequestStack $requestStack = null)
  37. {
  38. $this->dispatcher = $dispatcher;
  39. $this->stopwatch = $stopwatch;
  40. $this->logger = $logger;
  41. $this->wrappedListeners = [];
  42. $this->orphanedEvents = [];
  43. $this->requestStack = $requestStack;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function addListener(string $eventName, $listener, int $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(string $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(string $eventName = null)
  86. {
  87. return $this->dispatcher->getListeners($eventName);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getListenerPriority(string $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(string $eventName = null)
  109. {
  110. return $this->dispatcher->hasListeners($eventName);
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function dispatch(object $event, string $eventName = null): object
  116. {
  117. $eventName = $eventName ?? \get_class($event);
  118. if (null === $this->callStack) {
  119. $this->callStack = new \SplObjectStorage();
  120. }
  121. $currentRequestHash = $this->currentRequestHash = $this->requestStack && ($request = $this->requestStack->getCurrentRequest()) ? spl_object_hash($request) : '';
  122. if (null !== $this->logger && $event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
  123. $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
  124. }
  125. $this->preProcess($eventName);
  126. try {
  127. $this->beforeDispatch($eventName, $event);
  128. try {
  129. $e = $this->stopwatch->start($eventName, 'section');
  130. try {
  131. $this->dispatcher->dispatch($event, $eventName);
  132. } finally {
  133. if ($e->isStarted()) {
  134. $e->stop();
  135. }
  136. }
  137. } finally {
  138. $this->afterDispatch($eventName, $event);
  139. }
  140. } finally {
  141. $this->currentRequestHash = $currentRequestHash;
  142. $this->postProcess($eventName);
  143. }
  144. return $event;
  145. }
  146. /**
  147. * @return array
  148. */
  149. public function getCalledListeners(Request $request = null)
  150. {
  151. if (null === $this->callStack) {
  152. return [];
  153. }
  154. $hash = $request ? spl_object_hash($request) : null;
  155. $called = [];
  156. foreach ($this->callStack as $listener) {
  157. [$eventName, $requestHash] = $this->callStack->getInfo();
  158. if (null === $hash || $hash === $requestHash) {
  159. $called[] = $listener->getInfo($eventName);
  160. }
  161. }
  162. return $called;
  163. }
  164. /**
  165. * @return array
  166. */
  167. public function getNotCalledListeners(Request $request = null)
  168. {
  169. try {
  170. $allListeners = $this->getListeners();
  171. } catch (\Exception $e) {
  172. if (null !== $this->logger) {
  173. $this->logger->info('An exception was thrown while getting the uncalled listeners.', ['exception' => $e]);
  174. }
  175. // unable to retrieve the uncalled listeners
  176. return [];
  177. }
  178. $hash = $request ? spl_object_hash($request) : null;
  179. $calledListeners = [];
  180. if (null !== $this->callStack) {
  181. foreach ($this->callStack as $calledListener) {
  182. [, $requestHash] = $this->callStack->getInfo();
  183. if (null === $hash || $hash === $requestHash) {
  184. $calledListeners[] = $calledListener->getWrappedListener();
  185. }
  186. }
  187. }
  188. $notCalled = [];
  189. foreach ($allListeners as $eventName => $listeners) {
  190. foreach ($listeners as $listener) {
  191. if (!\in_array($listener, $calledListeners, true)) {
  192. if (!$listener instanceof WrappedListener) {
  193. $listener = new WrappedListener($listener, null, $this->stopwatch, $this);
  194. }
  195. $notCalled[] = $listener->getInfo($eventName);
  196. }
  197. }
  198. }
  199. uasort($notCalled, [$this, 'sortNotCalledListeners']);
  200. return $notCalled;
  201. }
  202. public function getOrphanedEvents(Request $request = null): array
  203. {
  204. if ($request) {
  205. return $this->orphanedEvents[spl_object_hash($request)] ?? [];
  206. }
  207. if (!$this->orphanedEvents) {
  208. return [];
  209. }
  210. return array_merge(...array_values($this->orphanedEvents));
  211. }
  212. public function reset()
  213. {
  214. $this->callStack = null;
  215. $this->orphanedEvents = [];
  216. $this->currentRequestHash = '';
  217. }
  218. /**
  219. * Proxies all method calls to the original event dispatcher.
  220. *
  221. * @param string $method The method name
  222. * @param array $arguments The method arguments
  223. *
  224. * @return mixed
  225. */
  226. public function __call(string $method, array $arguments)
  227. {
  228. return $this->dispatcher->{$method}(...$arguments);
  229. }
  230. /**
  231. * Called before dispatching the event.
  232. */
  233. protected function beforeDispatch(string $eventName, object $event)
  234. {
  235. }
  236. /**
  237. * Called after dispatching the event.
  238. */
  239. protected function afterDispatch(string $eventName, object $event)
  240. {
  241. }
  242. private function preProcess(string $eventName): void
  243. {
  244. if (!$this->dispatcher->hasListeners($eventName)) {
  245. $this->orphanedEvents[$this->currentRequestHash][] = $eventName;
  246. return;
  247. }
  248. foreach ($this->dispatcher->getListeners($eventName) as $listener) {
  249. $priority = $this->getListenerPriority($eventName, $listener);
  250. $wrappedListener = new WrappedListener($listener instanceof WrappedListener ? $listener->getWrappedListener() : $listener, null, $this->stopwatch, $this);
  251. $this->wrappedListeners[$eventName][] = $wrappedListener;
  252. $this->dispatcher->removeListener($eventName, $listener);
  253. $this->dispatcher->addListener($eventName, $wrappedListener, $priority);
  254. $this->callStack->attach($wrappedListener, [$eventName, $this->currentRequestHash]);
  255. }
  256. }
  257. private function postProcess(string $eventName): void
  258. {
  259. unset($this->wrappedListeners[$eventName]);
  260. $skipped = false;
  261. foreach ($this->dispatcher->getListeners($eventName) as $listener) {
  262. if (!$listener instanceof WrappedListener) { // #12845: a new listener was added during dispatch.
  263. continue;
  264. }
  265. // Unwrap listener
  266. $priority = $this->getListenerPriority($eventName, $listener);
  267. $this->dispatcher->removeListener($eventName, $listener);
  268. $this->dispatcher->addListener($eventName, $listener->getWrappedListener(), $priority);
  269. if (null !== $this->logger) {
  270. $context = ['event' => $eventName, 'listener' => $listener->getPretty()];
  271. }
  272. if ($listener->wasCalled()) {
  273. if (null !== $this->logger) {
  274. $this->logger->debug('Notified event "{event}" to listener "{listener}".', $context);
  275. }
  276. } else {
  277. $this->callStack->detach($listener);
  278. }
  279. if (null !== $this->logger && $skipped) {
  280. $this->logger->debug('Listener "{listener}" was not called for event "{event}".', $context);
  281. }
  282. if ($listener->stoppedPropagation()) {
  283. if (null !== $this->logger) {
  284. $this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', $context);
  285. }
  286. $skipped = true;
  287. }
  288. }
  289. }
  290. private function sortNotCalledListeners(array $a, array $b)
  291. {
  292. if (0 !== $cmp = strcmp($a['event'], $b['event'])) {
  293. return $cmp;
  294. }
  295. if (\is_int($a['priority']) && !\is_int($b['priority'])) {
  296. return 1;
  297. }
  298. if (!\is_int($a['priority']) && \is_int($b['priority'])) {
  299. return -1;
  300. }
  301. if ($a['priority'] === $b['priority']) {
  302. return 0;
  303. }
  304. if ($a['priority'] > $b['priority']) {
  305. return -1;
  306. }
  307. return 1;
  308. }
  309. }