vendor/elements/framework-bundle/src/EventListener/ProfilerDisableListener.php line 91

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Enterprise License (PEL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  14.  */
  15. namespace Elements\Bundle\FrameworkBundle\EventListener;
  16. use Elements\Bundle\FrameworkBundle\HttpFoundation\EnvVarMatcher;
  17. use Elements\Bundle\FrameworkBundle\HttpFoundation\UserAgentMatcher;
  18. use Pimcore\Http\RequestMatcherFactory;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  21. use Symfony\Component\HttpKernel\Event\RequestEvent;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. use Symfony\Component\HttpKernel\Profiler\Profiler;
  24. class ProfilerDisableListener implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var RequestMatcherFactory
  28.      */
  29.     private $requestMatcherFactory;
  30.     /**
  31.      * @var Profiler
  32.      */
  33.     private $profiler;
  34.     /**
  35.      * @var array
  36.      */
  37.     private $disabledRequests = [];
  38.     /**
  39.      * @var array
  40.      */
  41.     private $disabledUserAgentPatterns = [];
  42.     /**
  43.      * @var array
  44.      */
  45.     private $envVars = [];
  46.     /**
  47.      * @var RequestMatcherInterface[]
  48.      */
  49.     private $disabledMatchers;
  50.     public function __construct(
  51.         RequestMatcherFactory $requestMatcherFactory,
  52.         array $disabledRequests,
  53.         array $disabledUserAgentPatterns,
  54.         array $envVars
  55.     ) {
  56.         $this->requestMatcherFactory     $requestMatcherFactory;
  57.         $this->disabledRequests          $disabledRequests;
  58.         $this->disabledUserAgentPatterns $disabledUserAgentPatterns;
  59.         $this->envVars                   $envVars;
  60.     }
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             // disable the profiler as early as possible
  65.             KernelEvents::REQUEST => ['onKernelRequest'4096]
  66.         ];
  67.     }
  68.     /**
  69.      * Profiler will be set on demand if it exists
  70.      *
  71.      * @param Profiler $profiler
  72.      */
  73.     public function setProfiler(Profiler $profiler)
  74.     {
  75.         $this->profiler $profiler;
  76.     }
  77.     public function onKernelRequest(RequestEvent $event)
  78.     {
  79.         if (null === $this->profiler) {
  80.             return;
  81.         }
  82.         if (!$event->isMasterRequest()) {
  83.             return;
  84.         }
  85.         $matchers $this->getRequestMatchers();
  86.         if (empty($matchers)) {
  87.             return;
  88.         }
  89.         $request $event->getRequest();
  90.         $disable false;
  91.         foreach ($matchers as $matcher) {
  92.             if ($matcher->matches($request)) {
  93.                 $disable true;
  94.                 break;
  95.             }
  96.         }
  97.         if ($disable) {
  98.             $this->profiler->disable();
  99.         }
  100.     }
  101.     /**
  102.      * @return RequestMatcherInterface[]
  103.      */
  104.     private function getRequestMatchers(): array
  105.     {
  106.         if (null !== $this->disabledMatchers) {
  107.             return $this->disabledMatchers;
  108.         }
  109.         $this->disabledMatchers = [];
  110.         if (!empty($this->disabledRequests)) {
  111.             $this->disabledMatchers array_merge(
  112.                 $this->disabledMatchers,
  113.                 $this->requestMatcherFactory->buildRequestMatchers($this->disabledRequests)
  114.             );
  115.         }
  116.         if (!empty($this->disabledUserAgentPatterns)) {
  117.             $this->disabledMatchers[] = new UserAgentMatcher($this->disabledUserAgentPatterns);
  118.         }
  119.         if (!empty($this->envVars)) {
  120.             foreach ($this->envVars as $envVar) {
  121.                 $this->disabledMatchers[] = new EnvVarMatcher($envVar);
  122.             }
  123.         }
  124.         return $this->disabledMatchers;
  125.     }
  126. }