vendor/sulu/sulu/src/Sulu/Component/Webspace/Analyzer/RequestAnalyzer.php line 52

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\Component\Webspace\Analyzer;
  11. use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes;
  12. use Sulu\Component\Webspace\Analyzer\Attributes\RequestProcessorInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. /**
  16.  * Default request analyzer will be used for sulu-admin and extended for sulu-website.
  17.  */
  18. class RequestAnalyzer implements RequestAnalyzerInterface
  19. {
  20.     public const SULU_ATTRIBUTE '_sulu';
  21.     /**
  22.      * @var RequestProcessorInterface[]
  23.      */
  24.     private $requestProcessors;
  25.     /**
  26.      * @var RequestStack
  27.      */
  28.     private $requestStack;
  29.     public function __construct(
  30.         RequestStack $requestStack,
  31.         array $requestProcessors
  32.     ) {
  33.         $this->requestStack $requestStack;
  34.         $this->requestProcessors $requestProcessors;
  35.     }
  36.     public function analyze(Request $request)
  37.     {
  38.         if ($request->attributes->has(static::SULU_ATTRIBUTE)) {
  39.             return;
  40.         }
  41.         $attributes = new RequestAttributes(['scheme' => $request->getScheme(), 'requestUri' => $request->getRequestUri()]);
  42.         foreach ($this->requestProcessors as $requestProcessor) {
  43.             $attributes $attributes->merge($requestProcessor->process($request$attributes));
  44.         }
  45.         $request->attributes->set(static::SULU_ATTRIBUTE$attributes);
  46.     }
  47.     public function validate(Request $request)
  48.     {
  49.         $attributes $request->attributes->get(static::SULU_ATTRIBUTE);
  50.         foreach ($this->requestProcessors as $provider) {
  51.             $provider->validate($attributes);
  52.         }
  53.     }
  54.     public function getAttribute($name$default null)
  55.     {
  56.         $requestAttributes $this->getAttributes();
  57.         if (!$requestAttributes) {
  58.             return $default;
  59.         }
  60.         return $requestAttributes->getAttribute($name$default);
  61.     }
  62.     private function getAttributes()
  63.     {
  64.         $request $this->requestStack->getCurrentRequest();
  65.         if (null === $request) {
  66.             return null;
  67.         }
  68.         if (!$request->attributes->has(static::SULU_ATTRIBUTE)) {
  69.             return null;
  70.         }
  71.         return $request->attributes->get(static::SULU_ATTRIBUTE);
  72.     }
  73.     private function setAttributes(RequestAttributes $attributes)
  74.     {
  75.         $request $this->requestStack->getCurrentRequest();
  76.         $request->attributes->set(static::SULU_ATTRIBUTE$attributes);
  77.     }
  78.     public function getMatchType()
  79.     {
  80.         return $this->getAttribute('matchType');
  81.     }
  82.     public function getDateTime()
  83.     {
  84.         return $this->getAttribute('dateTime');
  85.     }
  86.     public function getWebspace()
  87.     {
  88.         return $this->getAttribute('webspace');
  89.     }
  90.     public function getPortal()
  91.     {
  92.         return $this->getAttribute('portal');
  93.     }
  94.     public function getSegment()
  95.     {
  96.         return $this->getAttribute('segment');
  97.     }
  98.     public function changeSegment(string $segmentKey)
  99.     {
  100.         $segment $this->getWebspace()->getSegment($segmentKey);
  101.         $requestAttributes = (new RequestAttributes(['segment' => $segment]))->merge($this->getAttributes());
  102.         $this->setAttributes($requestAttributes);
  103.     }
  104.     public function getCurrentLocalization()
  105.     {
  106.         return $this->getAttribute('localization');
  107.     }
  108.     public function getPortalUrl()
  109.     {
  110.         return $this->getAttribute('portalUrl');
  111.     }
  112.     public function getRedirect()
  113.     {
  114.         return $this->getAttribute('redirect');
  115.     }
  116.     public function getResourceLocator()
  117.     {
  118.         return $this->getAttribute('resourceLocator'false);
  119.     }
  120.     public function getResourceLocatorPrefix()
  121.     {
  122.         return $this->getAttribute('resourceLocatorPrefix');
  123.     }
  124.     public function getPortalInformation()
  125.     {
  126.         return $this->getAttribute('portalInformation');
  127.     }
  128. }