vendor/sulu/sulu/src/Sulu/Bundle/SnippetBundle/Twig/SnippetAreaTwigExtension.php line 89

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\SnippetBundle\Twig;
  11. use Sulu\Bundle\SnippetBundle\Snippet\DefaultSnippetManagerInterface;
  12. use Sulu\Bundle\SnippetBundle\Snippet\SnippetResolverInterface;
  13. use Sulu\Bundle\SnippetBundle\Snippet\WrongSnippetTypeException;
  14. use Sulu\Component\DocumentManager\Exception\DocumentNotFoundException;
  15. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  16. use Twig\Extension\AbstractExtension;
  17. use Twig\TwigFunction;
  18. /**
  19.  * Provides snippets by area.
  20.  */
  21. class SnippetAreaTwigExtension extends AbstractExtension
  22. {
  23.     /**
  24.      * @var DefaultSnippetManagerInterface
  25.      */
  26.     private $defaultSnippetManager;
  27.     /**
  28.      * @var RequestAnalyzerInterface
  29.      */
  30.     private $requestAnalyzer;
  31.     /**
  32.      * @var SnippetResolverInterface
  33.      */
  34.     private $snippetResolver;
  35.     public function __construct(
  36.         DefaultSnippetManagerInterface $defaultSnippetManager,
  37.         RequestAnalyzerInterface $requestAnalyzer,
  38.         SnippetResolverInterface $snippetResolver
  39.     ) {
  40.         $this->defaultSnippetManager $defaultSnippetManager;
  41.         $this->requestAnalyzer $requestAnalyzer;
  42.         $this->snippetResolver $snippetResolver;
  43.     }
  44.     public function getFunctions()
  45.     {
  46.         return [
  47.             new TwigFunction('sulu_snippet_load_by_area', [$this'loadByArea']),
  48.         ];
  49.     }
  50.     /**
  51.      * Load snippet for webspace by area.
  52.      *
  53.      * @param string $area
  54.      * @param string $webspaceKey
  55.      * @param string $locale
  56.      *
  57.      * @return array
  58.      */
  59.     public function loadByArea($area$webspaceKey null$locale null)
  60.     {
  61.         if (!$webspaceKey) {
  62.             $webspaceKey $this->requestAnalyzer->getWebspace()->getKey();
  63.         }
  64.         if (!$locale) {
  65.             $locale $this->requestAnalyzer->getCurrentLocalization()->getLocale();
  66.         }
  67.         try {
  68.             $snippet $this->defaultSnippetManager->load($webspaceKey$area$locale);
  69.         } catch (WrongSnippetTypeException $exception) {
  70.             return null;
  71.         } catch (DocumentNotFoundException $exception) {
  72.             return null;
  73.         }
  74.         if (!$snippet) {
  75.             return null;
  76.         }
  77.         $snippets $this->snippetResolver->resolve([$snippet->getUuid()], $webspaceKey$locale);
  78.         if (!\array_key_exists(0$snippets)) {
  79.             return null;
  80.         }
  81.         return $snippets[0];
  82.     }
  83. }