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

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