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

/vendor/symfony/http-kernel/EventListener/ProfilerListener.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 165 lines | 100 code | 21 blank | 44 comment | 15 complexity | 079b12b7f0631e470a9cb59972bbe3d6 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\HttpKernel\EventListener;
  11. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  12. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  13. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  14. use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\HttpKernel\Profiler\Profiler;
  17. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. /**
  21. * ProfilerListener collects data for the current request by listening to the kernel events.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. class ProfilerListener implements EventSubscriberInterface
  26. {
  27. protected $profiler;
  28. protected $matcher;
  29. protected $onlyException;
  30. protected $onlyMasterRequests;
  31. protected $exception;
  32. protected $requests = array();
  33. protected $profiles;
  34. protected $requestStack;
  35. protected $parents;
  36. /**
  37. * Constructor.
  38. *
  39. * @param Profiler $profiler A Profiler instance
  40. * @param RequestMatcherInterface|null $matcher A RequestMatcher instance
  41. * @param bool $onlyException true if the profiler only collects data when an exception occurs, false otherwise
  42. * @param bool $onlyMasterRequests true if the profiler only collects data when the request is a master request, false otherwise
  43. * @param RequestStack|null $requestStack A RequestStack instance
  44. */
  45. public function __construct(Profiler $profiler, RequestMatcherInterface $matcher = null, $onlyException = false, $onlyMasterRequests = false, RequestStack $requestStack = null)
  46. {
  47. if (null === $requestStack) {
  48. // Prevent the deprecation notice to be triggered all the time.
  49. // The onKernelRequest() method fires some logic only when the
  50. // RequestStack instance is not provided as a dependency.
  51. @trigger_error('Since version 2.4, the '.__METHOD__.' method must accept a RequestStack instance to get the request instead of using the '.__CLASS__.'::onKernelRequest method that will be removed in 3.0.', E_USER_DEPRECATED);
  52. }
  53. $this->profiler = $profiler;
  54. $this->matcher = $matcher;
  55. $this->onlyException = (bool) $onlyException;
  56. $this->onlyMasterRequests = (bool) $onlyMasterRequests;
  57. $this->profiles = new \SplObjectStorage();
  58. $this->parents = new \SplObjectStorage();
  59. $this->requestStack = $requestStack;
  60. }
  61. /**
  62. * Handles the onKernelException event.
  63. *
  64. * @param GetResponseForExceptionEvent $event A GetResponseForExceptionEvent instance
  65. */
  66. public function onKernelException(GetResponseForExceptionEvent $event)
  67. {
  68. if ($this->onlyMasterRequests && !$event->isMasterRequest()) {
  69. return;
  70. }
  71. $this->exception = $event->getException();
  72. }
  73. /**
  74. * @deprecated since version 2.4, to be removed in 3.0.
  75. */
  76. public function onKernelRequest(GetResponseEvent $event)
  77. {
  78. if (null === $this->requestStack) {
  79. $this->requests[] = $event->getRequest();
  80. }
  81. }
  82. /**
  83. * Handles the onKernelResponse event.
  84. *
  85. * @param FilterResponseEvent $event A FilterResponseEvent instance
  86. */
  87. public function onKernelResponse(FilterResponseEvent $event)
  88. {
  89. $master = $event->isMasterRequest();
  90. if ($this->onlyMasterRequests && !$master) {
  91. return;
  92. }
  93. if ($this->onlyException && null === $this->exception) {
  94. return;
  95. }
  96. $request = $event->getRequest();
  97. $exception = $this->exception;
  98. $this->exception = null;
  99. if (null !== $this->matcher && !$this->matcher->matches($request)) {
  100. return;
  101. }
  102. if (!$profile = $this->profiler->collect($request, $event->getResponse(), $exception)) {
  103. return;
  104. }
  105. $this->profiles[$request] = $profile;
  106. if (null !== $this->requestStack) {
  107. $this->parents[$request] = $this->requestStack->getParentRequest();
  108. } elseif (!$master) {
  109. // to be removed when requestStack is required
  110. array_pop($this->requests);
  111. $this->parents[$request] = end($this->requests);
  112. }
  113. }
  114. public function onKernelTerminate(PostResponseEvent $event)
  115. {
  116. // attach children to parents
  117. foreach ($this->profiles as $request) {
  118. // isset call should be removed when requestStack is required
  119. if (isset($this->parents[$request]) && null !== $parentRequest = $this->parents[$request]) {
  120. if (isset($this->profiles[$parentRequest])) {
  121. $this->profiles[$parentRequest]->addChild($this->profiles[$request]);
  122. }
  123. }
  124. }
  125. // save profiles
  126. foreach ($this->profiles as $request) {
  127. $this->profiler->saveProfile($this->profiles[$request]);
  128. }
  129. $this->profiles = new \SplObjectStorage();
  130. $this->parents = new \SplObjectStorage();
  131. $this->requests = array();
  132. }
  133. public static function getSubscribedEvents()
  134. {
  135. return array(
  136. // kernel.request must be registered as early as possible to not break
  137. // when an exception is thrown in any other kernel.request listener
  138. KernelEvents::REQUEST => array('onKernelRequest', 1024),
  139. KernelEvents::RESPONSE => array('onKernelResponse', -100),
  140. KernelEvents::EXCEPTION => 'onKernelException',
  141. KernelEvents::TERMINATE => array('onKernelTerminate', -1024),
  142. );
  143. }
  144. }