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

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\GetResponseEvent;
  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.     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.      * @param GetResponseEvent $event
  43.      */
  44.     public function onKernelRequest(GetResponseEvent $event)
  45.     {
  46.         $request $event->getRequest();
  47.         // This call is required in all cases, because the default router needs our webspace information
  48.         // Would be nice to also only call this if the _requestAnalyzer attribute is set, but it's set on the next line
  49.         $this->requestAnalyzer->analyze($request);
  50.         $this->baseRouteListener->onKernelRequest($event);
  51.         if (false !== $request->attributes->get(static::REQUEST_ANALYZERtrue)) {
  52.             $this->requestAnalyzer->validate($request);
  53.         }
  54.     }
  55.     /**
  56.      * Simply pass the event to the route listener, because we have nothing to add here.
  57.      *
  58.      * @param FinishRequestEvent $event
  59.      */
  60.     public function onKernelFinishRequest(FinishRequestEvent $event)
  61.     {
  62.         $this->baseRouteListener->onKernelFinishRequest($event);
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public static function getSubscribedEvents()
  68.     {
  69.         return [
  70.             KernelEvents::REQUEST => [['onKernelRequest'32]],
  71.             KernelEvents::FINISH_REQUEST => [['onKernelFinishRequest'0]],
  72.         ];
  73.     }
  74. }