vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Resolver/StructureResolver.php line 116

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\Structure\PageBridge;
  12. use Sulu\Component\Content\Compat\StructureInterface;
  13. use Sulu\Component\Content\ContentTypeManagerInterface;
  14. use Sulu\Component\Content\Document\Behavior\ExtensionBehavior;
  15. use Sulu\Component\Content\Document\Behavior\LocalizedAuthorBehavior;
  16. use Sulu\Component\Content\Document\Extension\ExtensionContainer;
  17. use Sulu\Component\Content\Extension\ExtensionManagerInterface;
  18. use Sulu\Component\Content\PreResolvableContentTypeInterface;
  19. /**
  20.  * Class that "resolves" the view data for a given structure.
  21.  */
  22. class StructureResolver implements StructureResolverInterface
  23. {
  24.     /**
  25.      * @var ContentTypeManagerInterface
  26.      */
  27.     protected $contentTypeManager;
  28.     /**
  29.      * @var ExtensionManagerInterface
  30.      */
  31.     protected $extensionManager;
  32.     /**
  33.      * @var bool
  34.      */
  35.     private $enabledTwigAttributes true;
  36.     public function __construct(
  37.         ContentTypeManagerInterface $contentTypeManager,
  38.         ExtensionManagerInterface $structureManager,
  39.         array $enabledTwigAttributes = [
  40.             'path' => true,
  41.         ]
  42.     ) {
  43.         $this->contentTypeManager $contentTypeManager;
  44.         $this->extensionManager $structureManager;
  45.         $this->enabledTwigAttributes $enabledTwigAttributes;
  46.         if ($enabledTwigAttributes['path'] ?? true) {
  47.             @trigger_deprecation('sulu/sulu''2.3''Enabling the "path" parameter is deprecated.');
  48.         }
  49.     }
  50.     public function resolve(StructureInterface $structurebool $loadExcerpt true/*, array $includedProperties = null*/)
  51.     {
  52.         $includedProperties = (\func_num_args() > 2) ? \func_get_arg(2) : null;
  53.         $data = [
  54.             'view' => [],
  55.             'content' => [],
  56.             'id' => $structure->getUuid(),
  57.             'uuid' => $structure->getUuid(),
  58.             'creator' => $structure->getCreator(),
  59.             'changer' => $structure->getChanger(),
  60.             'created' => $structure->getCreated(),
  61.             'changed' => $structure->getChanged(),
  62.             'template' => $structure->getKey(),
  63.         ];
  64.         if ($this->enabledTwigAttributes['path'] ?? true) {
  65.             $data['path'] = $structure->getPath();
  66.         }
  67.         $document $structure->getDocument();
  68.         if ($document instanceof ExtensionBehavior && $loadExcerpt) {
  69.             $extensionData null;
  70.             if (\method_exists($structure'getExt')) {
  71.                 // BC Layer for old behaviour
  72.                 $extensionData $structure->getExt();
  73.             }
  74.             if (!$extensionData) {
  75.                 $extensionData $document->getExtensionsData();
  76.             }
  77.             // Not in all cases you get a ExtensionContainer as setExtensionData is also called with array only
  78.             if ($extensionData instanceof ExtensionContainer) {
  79.                 $extensionData $extensionData->toArray();
  80.             }
  81.             $data['extension'] = $extensionData $extensionData : [];
  82.             foreach ($data['extension'] as $name => $value) {
  83.                 $extension $this->extensionManager->getExtension($structure->getKey(), $name);
  84.                 $data['extension'][$name] = $extension->getContentData($value);
  85.             }
  86.         }
  87.         if ($structure instanceof PageBridge) {
  88.             $data['urls'] = $structure->getUrls();
  89.             $data['published'] = $structure->getPublished();
  90.             $data['shadowBaseLocale'] = $structure->getShadowBaseLanguage();
  91.             $data['webspaceKey'] = $structure->getWebspaceKey();
  92.         }
  93.         if ($document instanceof LocalizedAuthorBehavior) {
  94.             $data['authored'] = $document->getAuthored();
  95.             $data['author'] = $document->getAuthor();
  96.         }
  97.         // pre-resolve content-types
  98.         foreach ($structure->getProperties(true) as $property) {
  99.             if (null === $includedProperties || \in_array($property->getName(), $includedProperties)) {
  100.                 $contentType $this->contentTypeManager->get($property->getContentTypeName());
  101.                 if ($contentType instanceof PreResolvableContentTypeInterface) {
  102.                     $contentType->preResolve($property);
  103.                 }
  104.             }
  105.         }
  106.         foreach ($structure->getProperties(true) as $property) {
  107.             if (null === $includedProperties || \in_array($property->getName(), $includedProperties)) {
  108.                 $contentType $this->contentTypeManager->get($property->getContentTypeName());
  109.                 $data['view'][$property->getName()] = $contentType->getViewData($property);
  110.                 $data['content'][$property->getName()] = $contentType->getContentData($property);
  111.             }
  112.         }
  113.         return $data;
  114.     }
  115. }