PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/techniconline/kmc
PHP | 335 lines | 215 code | 41 blank | 79 comment | 20 complexity | 413df0679daa49b680274ff2aa8c1c6f 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 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. }