vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/EventListener/RouterListener.php line 57

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\WebsiteBundle\EventListener;
  11. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\HttpKernel\EventListener\RouterListener as BaseRouterListener;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. /**
  18.  * This listener replaces the default RouterListener delivered by Symfony and adds the analyzing of the request as
  19.  * required by Sulu. Validating the result can be deactivated by passing `false` to the `_requestAnalyzer` default in
  20.  * the route.
  21.  */
  22. class RouterListener implements EventSubscriberInterface
  23. {
  24.     public const REQUEST_ANALYZER '_requestAnalyzer';
  25.     /**
  26.      * @var BaseRouterListener
  27.      */
  28.     private $baseRouteListener;
  29.     /**
  30.      * @var RequestAnalyzerInterface
  31.      */
  32.     private $requestAnalyzer;
  33.     public function __construct(BaseRouterListener $baseRouterListenerRequestAnalyzerInterface $requestAnalyzer)
  34.     {
  35.         $this->baseRouteListener $baseRouterListener;
  36.         $this->requestAnalyzer $requestAnalyzer;
  37.     }
  38.     /**
  39.      * Analyzes the request before passing the event to the default RouterListener from symfony and validates the result
  40.      * afterwards.
  41.      */
  42.     public function onKernelRequest(RequestEvent $event)
  43.     {
  44.         $request $event->getRequest();
  45.         // This call is required in all cases, because the default router needs our webspace information
  46.         // Would be nice to also only call this if the _requestAnalyzer attribute is set, but it's set on the next line
  47.         $this->requestAnalyzer->analyze($request);
  48.         $this->baseRouteListener->onKernelRequest($event);
  49.         if (false !== $request->attributes->getBoolean(static::REQUEST_ANALYZERtrue)) {
  50.             $this->requestAnalyzer->validate($request);
  51.         }
  52.     }
  53.     /**
  54.      * Simply pass the event to the route listener, because we have nothing to add here.
  55.      */
  56.     public function onKernelFinishRequest(FinishRequestEvent $event)
  57.     {
  58.         $this->baseRouteListener->onKernelFinishRequest($event);
  59.     }
  60.     public static function getSubscribedEvents()
  61.     {
  62.         return [
  63.             KernelEvents::REQUEST => [['onKernelRequest'32]],
  64.             KernelEvents::FINISH_REQUEST => [['onKernelFinishRequest'0]],
  65.         ];
  66.     }
  67. }