vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Twig/Content/ContentPathTwigExtension.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\Twig\Content;
  11. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  12. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  13. use Twig\Extension\AbstractExtension;
  14. use Twig\TwigFunction;
  15. /**
  16.  * provides the content_path function to generate real urls for frontend.
  17.  */
  18. class ContentPathTwigExtension extends AbstractExtension implements ContentPathInterface
  19. {
  20.     /**
  21.      * @var RequestAnalyzerInterface
  22.      */
  23.     private $requestAnalyzer;
  24.     /**
  25.      * @var WebspaceManagerInterface
  26.      */
  27.     private $webspaceManager;
  28.     /**
  29.      * @var string
  30.      */
  31.     private $environment;
  32.     public function __construct(
  33.         WebspaceManagerInterface $webspaceManager,
  34.         $environment,
  35.         ?RequestAnalyzerInterface $requestAnalyzer null
  36.     ) {
  37.         $this->webspaceManager $webspaceManager;
  38.         $this->environment $environment;
  39.         $this->requestAnalyzer $requestAnalyzer;
  40.     }
  41.     public function getFunctions()
  42.     {
  43.         return [
  44.             new TwigFunction('sulu_content_path', [$this'getContentPath']),
  45.             new TwigFunction('sulu_content_root_path', [$this'getContentRootPath']),
  46.         ];
  47.     }
  48.     public function getContentPath($route$webspaceKey null$locale null$domain null$scheme null$withoutDomain true)
  49.     {
  50.         // if the request analyzer null or a route is passed which is relative or inclusive a domain nothing should be
  51.         // done (this is important for external-links in navigations)
  52.         if (!$this->requestAnalyzer || !== \strpos($route'/')) {
  53.             return $route;
  54.         }
  55.         $scheme $scheme ?: $this->requestAnalyzer->getAttribute('scheme');
  56.         $locale $locale ?: $this->requestAnalyzer->getCurrentLocalization()->getLocale();
  57.         $webspaceKey $webspaceKey ?: $this->requestAnalyzer->getWebspace()->getKey();
  58.         $url null;
  59.         $host $this->requestAnalyzer->getAttribute('host');
  60.         if (!$domain
  61.             && $this->webspaceManager->findWebspaceByKey($webspaceKey)->hasDomain($host$this->environment$locale)
  62.         ) {
  63.             $domain $host;
  64.         }
  65.         $url $this->webspaceManager->findUrlByResourceLocator(
  66.             $route,
  67.             $this->environment,
  68.             $locale,
  69.             $webspaceKey,
  70.             $domain,
  71.             $scheme
  72.         );
  73.         if (!$withoutDomain && !$url) {
  74.             $url $this->webspaceManager->findUrlByResourceLocator(
  75.                 $route,
  76.                 $this->environment,
  77.                 $locale,
  78.                 $webspaceKey,
  79.                 null,
  80.                 $scheme
  81.             );
  82.         }
  83.         return $url ?: $route;
  84.     }
  85.     public function getContentRootPath($full false)
  86.     {
  87.         return $this->getContentPath('/');
  88.     }
  89. }