<?phpdeclare(strict_types=1);/** * Pimcore * * This source file is available under two different licenses: * - GNU General Public License version 3 (GPLv3) * - Pimcore Enterprise License (PEL) * Full copyright and license information is available in * LICENSE.md which is distributed with this source code. * * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) * @license http://www.pimcore.org/license GPLv3 and PEL */namespace Elements\Bundle\FrameworkBundle\EventListener;use Elements\Bundle\FrameworkBundle\HttpFoundation\EnvVarMatcher;use Elements\Bundle\FrameworkBundle\HttpFoundation\UserAgentMatcher;use Pimcore\Http\RequestMatcherFactory;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\RequestMatcherInterface;use Symfony\Component\HttpKernel\Event\RequestEvent;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Component\HttpKernel\Profiler\Profiler;class ProfilerDisableListener implements EventSubscriberInterface{ /** * @var RequestMatcherFactory */ private $requestMatcherFactory; /** * @var Profiler */ private $profiler; /** * @var array */ private $disabledRequests = []; /** * @var array */ private $disabledUserAgentPatterns = []; /** * @var array */ private $envVars = []; /** * @var RequestMatcherInterface[] */ private $disabledMatchers; public function __construct( RequestMatcherFactory $requestMatcherFactory, array $disabledRequests, array $disabledUserAgentPatterns, array $envVars ) { $this->requestMatcherFactory = $requestMatcherFactory; $this->disabledRequests = $disabledRequests; $this->disabledUserAgentPatterns = $disabledUserAgentPatterns; $this->envVars = $envVars; } public static function getSubscribedEvents() { return [ // disable the profiler as early as possible KernelEvents::REQUEST => ['onKernelRequest', 4096] ]; } /** * Profiler will be set on demand if it exists * * @param Profiler $profiler */ public function setProfiler(Profiler $profiler) { $this->profiler = $profiler; } public function onKernelRequest(RequestEvent $event) { if (null === $this->profiler) { return; } if (!$event->isMasterRequest()) { return; } $matchers = $this->getRequestMatchers(); if (empty($matchers)) { return; } $request = $event->getRequest(); $disable = false; foreach ($matchers as $matcher) { if ($matcher->matches($request)) { $disable = true; break; } } if ($disable) { $this->profiler->disable(); } } /** * @return RequestMatcherInterface[] */ private function getRequestMatchers(): array { if (null !== $this->disabledMatchers) { return $this->disabledMatchers; } $this->disabledMatchers = []; if (!empty($this->disabledRequests)) { $this->disabledMatchers = array_merge( $this->disabledMatchers, $this->requestMatcherFactory->buildRequestMatchers($this->disabledRequests) ); } if (!empty($this->disabledUserAgentPatterns)) { $this->disabledMatchers[] = new UserAgentMatcher($this->disabledUserAgentPatterns); } if (!empty($this->envVars)) { foreach ($this->envVars as $envVar) { $this->disabledMatchers[] = new EnvVarMatcher($envVar); } } return $this->disabledMatchers; }}