ProfilerSubscriber.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Drupal\webprofiler\EventSubscriber;
  3. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  4. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  5. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\HttpKernel\Profiler\Profiler;
  8. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ProfilerSubscriber implements EventSubscriberInterface {
  12. protected $profiler;
  13. protected $matcher;
  14. protected $onlyException;
  15. protected $onlyMasterRequests;
  16. protected $exception;
  17. protected $profiles;
  18. protected $requestStack;
  19. protected $parents;
  20. /**
  21. * @param Profiler $profiler A Profiler instance
  22. * @param RequestStack $requestStack A RequestStack instance
  23. * @param RequestMatcherInterface|null $matcher A RequestMatcher instance
  24. * @param bool $onlyException True if the profiler only collects data when an
  25. * exception occurs, false otherwise
  26. * @param bool $onlyMasterRequests True if the profiler only collects data
  27. * when the request is a master request, false otherwise
  28. */
  29. public function __construct(Profiler $profiler, RequestStack $requestStack, RequestMatcherInterface $matcher = NULL, $onlyException = FALSE, $onlyMasterRequests = FALSE) {
  30. $this->profiler = $profiler;
  31. $this->matcher = $matcher;
  32. $this->onlyException = (bool) $onlyException;
  33. $this->onlyMasterRequests = (bool) $onlyMasterRequests;
  34. $this->profiles = new \SplObjectStorage();
  35. $this->parents = new \SplObjectStorage();
  36. $this->requestStack = $requestStack;
  37. }
  38. /**
  39. * Handles the onKernelException event.
  40. */
  41. public function onKernelException(GetResponseForExceptionEvent $event) {
  42. if ($this->onlyMasterRequests && !$event->isMasterRequest()) {
  43. return;
  44. }
  45. $this->exception = $event->getException();
  46. }
  47. /**
  48. * Handles the onKernelResponse event.
  49. */
  50. public function onKernelResponse(FilterResponseEvent $event) {
  51. $master = $event->isMasterRequest();
  52. if ($this->onlyMasterRequests && !$master) {
  53. return;
  54. }
  55. if ($this->onlyException && NULL === $this->exception) {
  56. return;
  57. }
  58. $request = $event->getRequest();
  59. $exception = $this->exception;
  60. $this->exception = NULL;
  61. if (NULL !== $this->matcher && !$this->matcher->matches($request)) {
  62. return;
  63. }
  64. if (!$profile = $this->profiler->collect($request, $event->getResponse(), $exception)) {
  65. return;
  66. }
  67. $this->profiles[$request] = $profile;
  68. $this->parents[$request] = $this->requestStack->getParentRequest();
  69. }
  70. public function onKernelFinishRequest(FinishRequestEvent $event) {
  71. // attach children to parents
  72. foreach ($this->profiles as $request) {
  73. if (NULL !== $parentRequest = $this->parents[$request]) {
  74. if (isset($this->profiles[$parentRequest])) {
  75. $this->profiles[$parentRequest]->addChild($this->profiles[$request]);
  76. }
  77. }
  78. }
  79. // save profiles
  80. foreach ($this->profiles as $request) {
  81. $this->profiler->saveProfile($this->profiles[$request]);
  82. }
  83. $this->profiles = new \SplObjectStorage();
  84. $this->parents = new \SplObjectStorage();
  85. }
  86. public static function getSubscribedEvents() {
  87. return [
  88. KernelEvents::RESPONSE => ['onKernelResponse', -100],
  89. KernelEvents::EXCEPTION => 'onKernelException',
  90. KernelEvents::FINISH_REQUEST => ['onKernelFinishRequest', -1024],
  91. ];
  92. }
  93. }