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

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