PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/event-dispatcher/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

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