diff --git a/webprofiler/src/EventSubscriber/ProfilerSubscriber.php b/webprofiler/src/EventSubscriber/ProfilerSubscriber.php new file mode 100644 index 0000000000000000000000000000000000000000..d3f3124fc0131602d4c4049a81b0b101e0e6f05d --- /dev/null +++ b/webprofiler/src/EventSubscriber/ProfilerSubscriber.php @@ -0,0 +1,118 @@ +profiler = $profiler; + $this->matcher = $matcher; + $this->onlyException = (bool) $onlyException; + $this->onlyMasterRequests = (bool) $onlyMasterRequests; + $this->profiles = new \SplObjectStorage(); + $this->parents = new \SplObjectStorage(); + $this->requestStack = $requestStack; + } + + /** + * Handles the onKernelException event. + */ + public function onKernelException(GetResponseForExceptionEvent $event) { + if ($this->onlyMasterRequests && !$event->isMasterRequest()) { + return; + } + + $this->exception = $event->getException(); + } + + /** + * Handles the onKernelResponse event. + */ + public function onKernelResponse(FilterResponseEvent $event) { + $master = $event->isMasterRequest(); + if ($this->onlyMasterRequests && !$master) { + return; + } + + if ($this->onlyException && NULL === $this->exception) { + return; + } + + $request = $event->getRequest(); + $exception = $this->exception; + $this->exception = NULL; + + if (NULL !== $this->matcher && !$this->matcher->matches($request)) { + return; + } + + if (!$profile = $this->profiler->collect($request, $event->getResponse(), $exception)) { + return; + } + + $this->profiles[$request] = $profile; + + $this->parents[$request] = $this->requestStack->getParentRequest(); + } + + public function onKernelFinishRequest(FinishRequestEvent $event) { + // attach children to parents + foreach ($this->profiles as $request) { + if (NULL !== $parentRequest = $this->parents[$request]) { + if (isset($this->profiles[$parentRequest])) { + $this->profiles[$parentRequest]->addChild($this->profiles[$request]); + } + } + } + + // save profiles + foreach ($this->profiles as $request) { + $this->profiler->saveProfile($this->profiles[$request]); + } + + $this->profiles = new \SplObjectStorage(); + $this->parents = new \SplObjectStorage(); + } + + public static function getSubscribedEvents() { + return [ + KernelEvents::RESPONSE => ['onKernelResponse', -100], + KernelEvents::EXCEPTION => 'onKernelException', + KernelEvents::FINISH_REQUEST => ['onKernelFinishRequest', -1024], + ]; + } +} diff --git a/webprofiler/webprofiler.services.yml b/webprofiler/webprofiler.services.yml index 6332fef9260b578bd7ddbfaefa365fafa957b93a..50527926a47ea4c5d8c8d5b59e664b23a9004a20 100644 --- a/webprofiler/webprofiler.services.yml +++ b/webprofiler/webprofiler.services.yml @@ -46,7 +46,7 @@ services: # event subscribers webprofiler.profiler_listener: - class: Symfony\Component\HttpKernel\EventListener\ProfilerListener + class: Drupal\webprofiler\EventSubscriber\ProfilerSubscriber arguments: ['@profiler', '@request_stack', '@?webprofiler.matcher', '%webprofiler.only_exceptions%', '%webprofiler.only_master_requests%'] tags: - { name: event_subscriber }