vendor/contao/core-bundle/src/EventListener/BackendPreviewRedirectListener.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\EventListener;
  11. use Contao\CoreBundle\Routing\ScopeMatcher;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. /**
  15.  * @internal
  16.  */
  17. class BackendPreviewRedirectListener
  18. {
  19.     private ScopeMatcher $scopeMatcher;
  20.     public function __construct(ScopeMatcher $scopeMatcher)
  21.     {
  22.         $this->scopeMatcher $scopeMatcher;
  23.     }
  24.     public function __invoke(RequestEvent $event): void
  25.     {
  26.         if (!$event->isMainRequest()) {
  27.             return;
  28.         }
  29.         $request $event->getRequest();
  30.         if (
  31.             !$request->attributes->has('_preview')
  32.             || true !== $request->attributes->get('_preview')
  33.             || $this->scopeMatcher->isFrontendRequest($request)
  34.             || ($request->attributes->has('_allow_preview') && true === $request->attributes->get('_allow_preview'))
  35.         ) {
  36.             return;
  37.         }
  38.         $event->setResponse(new RedirectResponse($request->getSchemeAndHttpHost().$request->getPathInfo().(null !== ($qs $request->server->get('QUERY_STRING')) ? '?'.$qs '')));
  39.     }
  40. }