vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Twig/Sitemap/SitemapTwigExtension.php line 83

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\Sitemap;
  11. use Sulu\Bundle\WebsiteBundle\Sitemap\SitemapGeneratorInterface;
  12. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  13. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  14. use Twig\Extension\AbstractExtension;
  15. use Twig\TwigFunction;
  16. /**
  17.  * Provides twig functions for sitemap.
  18.  */
  19. class SitemapTwigExtension extends AbstractExtension implements SitemapTwigExtensionInterface
  20. {
  21.     /**
  22.      * @var WebspaceManagerInterface
  23.      */
  24.     private $webspaceManager;
  25.     /**
  26.      * @var SitemapGeneratorInterface
  27.      */
  28.     private $sitemapGenerator;
  29.     /**
  30.      * @var RequestAnalyzerInterface
  31.      */
  32.     private $requestAnalyzer;
  33.     /**
  34.      * @var string
  35.      */
  36.     private $environment;
  37.     public function __construct(
  38.         SitemapGeneratorInterface $sitemapGenerator,
  39.         WebspaceManagerInterface $webspaceManager,
  40.         $environment,
  41.         RequestAnalyzerInterface $requestAnalyzer null
  42.     ) {
  43.         $this->environment $environment;
  44.         $this->sitemapGenerator $sitemapGenerator;
  45.         $this->webspaceManager $webspaceManager;
  46.         $this->requestAnalyzer $requestAnalyzer;
  47.     }
  48.     public function getFunctions()
  49.     {
  50.         return [
  51.             new TwigFunction('sulu_sitemap_url', [$this'sitemapUrlFunction']),
  52.             new TwigFunction('sulu_sitemap', [$this'sitemapFunction']),
  53.         ];
  54.     }
  55.     public function sitemapUrlFunction($url$locale null$webspaceKey null)
  56.     {
  57.         if (null === $webspaceKey) {
  58.             $webspaceKey $this->requestAnalyzer->getWebspace()->getKey();
  59.         }
  60.         if (null === $locale) {
  61.             $locale $this->requestAnalyzer->getCurrentLocalization()->getLocale();
  62.         }
  63.         return $this->webspaceManager->findUrlByResourceLocator(
  64.             $url,
  65.             $this->environment,
  66.             $locale,
  67.             $webspaceKey
  68.         );
  69.     }
  70.     public function sitemapFunction($locale null$webspaceKey null)
  71.     {
  72.         if (null === $webspaceKey) {
  73.             $webspaceKey $this->requestAnalyzer->getWebspace()->getKey();
  74.         }
  75.         if (null === $locale) {
  76.             $locale $this->requestAnalyzer->getCurrentLocalization()->getLocale();
  77.         }
  78.         return $this->sitemapGenerator->generate($webspaceKey$locale)->getSitemap();
  79.     }
  80. }