src/EventSubscriber/DomainRedirectSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-19
  5.  * Time: 11:19
  6.  */
  7. namespace App\EventSubscriber;
  8. use App\Http\DomainRedirectorInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\HttpKernel\EventListener\RouterListener;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. /**
  15.  * Приоритет листнера редиректов должен быть выше, чем у RouterListener::onKernelRequest()
  16.  *
  17.  * @see RouterListener
  18.  */
  19. class DomainRedirectSubscriber implements EventSubscriberInterface
  20. {
  21.     public function __construct(
  22.         protected DomainRedirectorInterface $domainRedirector
  23.     ) {}
  24.     public function onRequest(RequestEvent $event): void
  25.     {
  26.         if (!$event->isMasterRequest()) {
  27.             return;
  28.         }
  29.         $request $event->getRequest();
  30.         if (null !== $response $this->domainRedirector->getImmediateResponse($request)) {
  31.             $event->setResponse($response);
  32.         }
  33.     }
  34.     /**
  35.      * @inheritDoc
  36.      */
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             KernelEvents::REQUEST => ['onRequest'50],
  41.         ];
  42.     }
  43. }