vendor/sulu/sulu/src/Sulu/Bundle/CustomUrlBundle/Request/CustomUrlRequestProcessor.php line 75

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\CustomUrlBundle\Request;
  11. use Sulu\Component\Content\Document\WorkflowStage;
  12. use Sulu\Component\CustomUrl\Generator\GeneratorInterface;
  13. use Sulu\Component\CustomUrl\Manager\CustomUrlManagerInterface;
  14. use Sulu\Component\Localization\Localization;
  15. use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes;
  16. use Sulu\Component\Webspace\Analyzer\Attributes\RequestProcessorInterface;
  17. use Sulu\Component\Webspace\Analyzer\RequestAnalyzer;
  18. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  19. use Sulu\Component\Webspace\PortalInformation;
  20. use Symfony\Component\HttpFoundation\Request;
  21. /**
  22.  * Set localization in case of custom-url route.
  23.  */
  24. class CustomUrlRequestProcessor implements RequestProcessorInterface
  25. {
  26.     /**
  27.      * @var CustomUrlManagerInterface
  28.      */
  29.     private $customUrlManager;
  30.     /**
  31.      * @var GeneratorInterface
  32.      */
  33.     private $generator;
  34.     /**
  35.      * @var WebspaceManagerInterface
  36.      */
  37.     private $webspaceManager;
  38.     /**
  39.      * @var string
  40.      */
  41.     private $environment;
  42.     public function __construct(
  43.         CustomUrlManagerInterface $customUrlManager,
  44.         GeneratorInterface $generator,
  45.         WebspaceManagerInterface $webspaceManager,
  46.         $environment
  47.     ) {
  48.         $this->customUrlManager $customUrlManager;
  49.         $this->generator $generator;
  50.         $this->webspaceManager $webspaceManager;
  51.         $this->environment $environment;
  52.     }
  53.     public function process(Request $requestRequestAttributes $requestAttributes)
  54.     {
  55.         $pathInfo $request->getPathInfo();
  56.         if (\strrpos($pathInfo'.')) {
  57.             $pathInfo \substr($pathInfo0\strrpos($pathInfo'.'));
  58.         }
  59.         $queryString $request->getQueryString();
  60.         if (!empty($queryString)) {
  61.             $queryString '?' $queryString;
  62.         }
  63.         $url $this->decodeUrl(\rtrim(\sprintf('%s%s%s'$request->getHost(), $pathInfo$queryString), '/'));
  64.         $portalInformations $this->webspaceManager->findPortalInformationsByUrl($url$this->environment);
  65.         if (=== \count($portalInformations)) {
  66.             return new RequestAttributes();
  67.         }
  68.         /** @var PortalInformation[] $portalInformations */
  69.         $portalInformations \array_filter(
  70.             $portalInformations,
  71.             function(PortalInformation $portalInformation) {
  72.                 return RequestAnalyzer::MATCH_TYPE_WILDCARD === $portalInformation->getType();
  73.             }
  74.         );
  75.         foreach ($portalInformations as $portalInformation) {
  76.             if (!$portalInformation->getWebspace()) {
  77.                 continue;
  78.             }
  79.             if (null !== $attributes $this->matchCustomUrl($url$portalInformation$request)) {
  80.                 return new RequestAttributes($attributes);
  81.             }
  82.         }
  83.         return new RequestAttributes();
  84.     }
  85.     public function validate(RequestAttributes $attributes)
  86.     {
  87.         return true;
  88.     }
  89.     /**
  90.      * Matches given url to portal-information.
  91.      */
  92.     private function matchCustomUrl(string $urlPortalInformation $portalInformationRequest $request): array
  93.     {
  94.         $webspace $portalInformation->getWebspace();
  95.         $routeDocument $this->customUrlManager->findRouteByUrl(
  96.             \rawurldecode($url),
  97.             $webspace->getKey()
  98.         );
  99.         if (!$routeDocument) {
  100.             return [];
  101.         } elseif ($routeDocument->isHistory()) {
  102.             // redirect happen => no portal is needed
  103.             return ['customUrlRoute' => $routeDocument];
  104.         }
  105.         $customUrlDocument $this->customUrlManager->findByUrl(
  106.             \rawurldecode($url),
  107.             $webspace->getKey(),
  108.             $routeDocument->getTargetDocument()->getTargetLocale()
  109.         );
  110.         if (null === $customUrlDocument
  111.             || false === $customUrlDocument->isPublished()
  112.             || null === $customUrlDocument->getTargetDocument()
  113.             || WorkflowStage::PUBLISHED !== $customUrlDocument->getTargetDocument()->getWorkflowStage()
  114.         ) {
  115.             // error happen because this custom-url is not published => no portal is needed
  116.             return ['customUrlRoute' => $routeDocument'customUrl' => $customUrlDocument];
  117.         }
  118.         $localization Localization::createFromString($customUrlDocument->getTargetLocale());
  119.         $portalInformations $this->webspaceManager->findPortalInformationsByWebspaceKeyAndLocale(
  120.             $portalInformation->getWebspace()->getKey(),
  121.             $localization->getLocale(),
  122.             $this->environment
  123.         );
  124.         if (=== \count($portalInformations)) {
  125.             return ['customUrlRoute' => $routeDocument'customUrl' => $customUrlDocument];
  126.         }
  127.         return [
  128.             'portalInformation' => $portalInformation,
  129.             'localization' => $localization,
  130.             'locale' => $localization->getLocale(),
  131.             'customUrlRoute' => $routeDocument,
  132.             'customUrl' => $customUrlDocument,
  133.             'urlExpression' => $this->generator->generate(
  134.                 $customUrlDocument->getBaseDomain(),
  135.                 $customUrlDocument->getDomainParts()
  136.             ),
  137.         ];
  138.     }
  139.     /**
  140.      * Server encodes the url and symfony does not encode it
  141.      * Symfony decodes this data here https://github.com/symfony/symfony/blob/3.3/src/Symfony/Component/Routing/Matcher/UrlMatcher.php#L91.
  142.      */
  143.     private function decodeUrl(string $pathInfo): string
  144.     {
  145.         return \rawurldecode($pathInfo);
  146.     }
  147. }