vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Resolver/ParameterResolver.php line 76

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\Resolver;
  11. use Sulu\Component\Content\Compat\StructureInterface;
  12. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  13. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. /**
  17.  * Implements logic to resolve parameters for website rendering.
  18.  */
  19. class ParameterResolver implements ParameterResolverInterface
  20. {
  21.     /**
  22.      * @var StructureResolverInterface
  23.      */
  24.     private $structureResolver;
  25.     /**
  26.      * @var RequestAnalyzerResolverInterface
  27.      */
  28.     private $requestAnalyzerResolver;
  29.     /**
  30.      * @var WebspaceManagerInterface
  31.      */
  32.     private $webspaceManager;
  33.     /**
  34.      * @var RequestStack
  35.      */
  36.     private $requestStack;
  37.     /**
  38.      * @var string
  39.      */
  40.     private $segmentSwitchUrl;
  41.     /**
  42.      * @var array
  43.      */
  44.     private $enabledTwigAttributes;
  45.     /**
  46.      * ParameterResolver constructor.
  47.      */
  48.     public function __construct(
  49.         StructureResolverInterface $structureResolver,
  50.         RequestAnalyzerResolverInterface $requestAnalyzerResolver,
  51.         WebspaceManagerInterface $webspaceManager,
  52.         RequestStack $requestStack,
  53.         $segmentSwitchUrl,
  54.         array $enabledTwigAttributes = [
  55.             'urls' => true,
  56.         ]
  57.     ) {
  58.         $this->structureResolver $structureResolver;
  59.         $this->requestAnalyzerResolver $requestAnalyzerResolver;
  60.         $this->webspaceManager $webspaceManager;
  61.         $this->requestStack $requestStack;
  62.         $this->segmentSwitchUrl $segmentSwitchUrl;
  63.         $this->enabledTwigAttributes $enabledTwigAttributes;
  64.     }
  65.     public function resolve(
  66.         array $parameter,
  67.         RequestAnalyzerInterface $requestAnalyzer null,
  68.         StructureInterface $structure null,
  69.         $preview false
  70.     ) {
  71.         if (null !== $structure) {
  72.             $structureData $this->structureResolver->resolve($structuretrue);
  73.         } else {
  74.             $structureData = [];
  75.         }
  76.         if (!$requestAnalyzer) {
  77.             return \array_merge($parameter$structureData);
  78.         }
  79.         $requestAnalyzerData $this->requestAnalyzerResolver->resolve($requestAnalyzer);
  80.         $webspace $requestAnalyzer->getWebspace();
  81.         if (null !== ($portal $requestAnalyzer->getPortal())) {
  82.             $allLocalizations $portal->getLocalizations();
  83.         } else {
  84.             $allLocalizations $webspace->getLocalizations();
  85.         }
  86.         $pageUrls = [];
  87.         if (\array_key_exists('urls'$structureData)) {
  88.             $pageUrls $structureData['urls'];
  89.             unset($structureData['urls']);
  90.         }
  91.         $localizations = [];
  92.         foreach ($allLocalizations as $localization) {
  93.             $locale $localization->getLocale();
  94.             $alternate true;
  95.             if (\array_key_exists($locale$pageUrls)) {
  96.                 $url $this->webspaceManager->findUrlByResourceLocator($pageUrls[$locale], null$locale);
  97.             } else {
  98.                 $alternate false;
  99.                 $url $this->webspaceManager->findUrlByResourceLocator('/'null$locale);
  100.             }
  101.             $localizations[$locale] = [
  102.                 'locale' => $locale,
  103.                 'url' => $url,
  104.                 'country' => $localization->getCountry(),
  105.                 'alternate' => $alternate,
  106.             ];
  107.         }
  108.         if ($this->enabledTwigAttributes['urls'] ?? true) {
  109.             @\trigger_error('Enabling the "urls" parameter is deprecated since Sulu 2.2', \E_USER_DEPRECATED);
  110.             $structureData['urls'] = [];
  111.             foreach ($localizations as $localization) {
  112.                 $structureData['urls'][$localization['locale']] = $localization['url'];
  113.             }
  114.         }
  115.         $structureData['localizations'] = $localizations;
  116.         /** @var Request $request */
  117.         $request $this->requestStack->getCurrentRequest();
  118.         $url $request->getUri();
  119.         $segments = [];
  120.         foreach ($webspace->getSegments() as $segment) {
  121.             $segmentKey $segment->getKey();
  122.             $segmentSwitchUrls[$segmentKey] = $this->segmentSwitchUrl '?segment=' $segmentKey '&url=' $url;
  123.             $segments[$segmentKey] = [
  124.                 'title' => $segment->getTitle($requestAnalyzer->getCurrentLocalization()->getLocale()),
  125.                 'url' => $this->segmentSwitchUrl '?segment=' $segmentKey '&url=' $url,
  126.             ];
  127.         }
  128.         $structureData['segments'] = $segments;
  129.         return \array_merge(
  130.             $parameter,
  131.             $structureData,
  132.             $requestAnalyzerData
  133.         );
  134.     }
  135. }