vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/WebDebugToolbarListener.php line 61

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\CoreBundle\EventListener;
  15. use Pimcore\Http\RequestHelper;
  16. use Pimcore\Http\RequestMatcherFactory;
  17. use Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener as SymfonyWebDebugToolbarListener;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  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. /**
  24.  * Disables the web debug toolbar for frontend requests by admins (iframes inside admin interface)
  25.  *
  26.  * @internal
  27.  */
  28. class WebDebugToolbarListener implements EventSubscriberInterface
  29. {
  30.     /**
  31.      * @var RequestMatcherInterface[]
  32.      */
  33.     protected $excludeMatchers;
  34.     public function __construct(
  35.         protected RequestHelper $requestHelper,
  36.         protected RequestMatcherFactory $requestMatcherFactory,
  37.         protected ?SymfonyWebDebugToolbarListener $debugToolbarListener,
  38.         protected EventDispatcherInterface $eventDispatcher,
  39.         protected array $excludeRoutes
  40.     ) {
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             KernelEvents::REQUEST => ['onKernelResponse', -118],
  49.         ];
  50.     }
  51.     /**
  52.      * @param RequestEvent $event
  53.      */
  54.     public function onKernelResponse(RequestEvent $event)
  55.     {
  56.         if (!$event->isMainRequest()) {
  57.             return;
  58.         }
  59.         $request $event->getRequest();
  60.         // do not show toolbar on frontend-admin requests
  61.         if ($this->requestHelper->isFrontendRequestByAdmin($request)) {
  62.             $this->disableWebDebugToolbar();
  63.         }
  64.         // do not show toolbar on excluded routes (pimcore.web_profiler.toolbar.excluded_routes config entry)
  65.         foreach ($this->getExcludeMatchers() as $excludeMatcher) {
  66.             if ($excludeMatcher->matches($request)) {
  67.                 $this->disableWebDebugToolbar();
  68.             }
  69.         }
  70.     }
  71.     /**
  72.      * @return RequestMatcherInterface[]
  73.      */
  74.     protected function getExcludeMatchers()
  75.     {
  76.         if (null === $this->excludeMatchers) {
  77.             $this->excludeMatchers $this->requestMatcherFactory->buildRequestMatchers($this->excludeRoutes);
  78.         }
  79.         return $this->excludeMatchers;
  80.     }
  81.     protected function disableWebDebugToolbar(): void
  82.     {
  83.         if ($this->debugToolbarListener) {
  84.             $this->eventDispatcher->removeSubscriber($this->debugToolbarListener);
  85.         }
  86.     }
  87. }