vendor/sulu/sulu/src/Sulu/Bundle/SnippetBundle/Snippet/SnippetResolver.php line 46

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\Snippet;
  11. use Sulu\Bundle\WebsiteBundle\Resolver\StructureResolverInterface;
  12. use Sulu\Component\Content\Mapper\ContentMapperInterface;
  13. use Sulu\Component\DocumentManager\Exception\DocumentNotFoundException;
  14. /**
  15.  * Resolves snippets by UUIDs.
  16.  */
  17. class SnippetResolver implements SnippetResolverInterface
  18. {
  19.     /**
  20.      * @var array
  21.      */
  22.     private $snippetCache = [];
  23.     /**
  24.      * @var ContentMapperInterface
  25.      */
  26.     private $contentMapper;
  27.     /**
  28.      * @var StructureResolverInterface
  29.      */
  30.     private $structureResolver;
  31.     public function __construct(
  32.         ContentMapperInterface $contentMapper,
  33.         StructureResolverInterface $structureResolver
  34.     ) {
  35.         $this->contentMapper $contentMapper;
  36.         $this->structureResolver $structureResolver;
  37.     }
  38.     public function resolve($uuids$webspaceKey$locale$shadowLocale null$loadExcerpt false)
  39.     {
  40.         $snippets = [];
  41.         foreach ($uuids as $uuid) {
  42.             if (!\array_key_exists($uuid$this->snippetCache)) {
  43.                 try {
  44.                     $snippet $this->contentMapper->load($uuid$webspaceKey$locale);
  45.                 } catch (DocumentNotFoundException $e) {
  46.                     continue;
  47.                 }
  48.                 if (!$snippet->getHasTranslation() && null !== $shadowLocale) {
  49.                     $snippet $this->contentMapper->load($uuid$webspaceKey$shadowLocale);
  50.                 }
  51.                 $snippet->setIsShadow(null !== $shadowLocale);
  52.                 $snippet->setShadowBaseLanguage($shadowLocale);
  53.                 $resolved $this->structureResolver->resolve($snippet$loadExcerpt);
  54.                 if ($loadExcerpt) {
  55.                     $resolved['content']['taxonomies'] = [
  56.                         'categories' => $resolved['extension']['excerpt']['categories'],
  57.                         'tags' => $resolved['extension']['excerpt']['tags'],
  58.                     ];
  59.                 }
  60.                 $resolved['view']['template'] = $snippet->getKey();
  61.                 $resolved['view']['uuid'] = $snippet->getUuid();
  62.                 $this->snippetCache[$uuid] = $resolved;
  63.             }
  64.             $snippets[] = $this->snippetCache[$uuid];
  65.         }
  66.         return $snippets;
  67.     }
  68. }