vendor/sulu/sulu/src/Sulu/Component/Content/Document/Extension/ManagedExtensionContainer.php line 108

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\Component\Content\Document\Extension;
  11. use PHPCR\NodeInterface;
  12. use Sulu\Component\Content\Extension\ExtensionManagerInterface;
  13. /**
  14. * The managed extension container lazily loads data from the actual
  15. * extension classes. It serves a similar, but not identical, role to
  16. * the ManagedStructure.
  17. *
  18. * In contrast to the Structure, which is a container of properties and returns Property instances,
  19. * extensions return simple arrays.
  20. *
  21. * Note that we should remove this class as retrieving the processed data
  22. * for extensions should be done externally to the document.
  23. */
  24. class ManagedExtensionContainer extends ExtensionContainer
  25. {
  26. /**
  27. * @var ExtensionManagerInterface
  28. */
  29. private $extensionManager;
  30. /**
  31. * @var NodeInterface
  32. */
  33. private $node;
  34. /**
  35. * @var string
  36. */
  37. private $locale;
  38. /**
  39. * @var string
  40. */
  41. private $prefix;
  42. /**
  43. * @var string
  44. */
  45. private $internalPrefix;
  46. /**
  47. * @var string
  48. */
  49. private $structureType;
  50. /**
  51. * @var string
  52. */
  53. private $webspaceName;
  54. /**
  55. * @param string $structureType
  56. * @param string $locale
  57. * @param string $prefix
  58. * @param string $internalPrefix
  59. * @param string $webspaceName
  60. */
  61. public function __construct(
  62. $structureType,
  63. ExtensionManagerInterface $extensionManager,
  64. NodeInterface $node,
  65. $locale,
  66. $prefix,
  67. $internalPrefix,
  68. $webspaceName
  69. ) {
  70. parent::__construct();
  71. $this->extensionManager = $extensionManager;
  72. $this->node = $node;
  73. $this->locale = $locale;
  74. $this->prefix = $prefix;
  75. $this->internalPrefix = $internalPrefix;
  76. $this->structureType = $structureType;
  77. $this->webspaceName = $webspaceName;
  78. }
  79. /**
  80. * Lazily evaluate the value for the given extension.
  81. *
  82. * @param string $extensionName
  83. */
  84. #[\ReturnTypeWillChange]
  85. public function offsetGet($extensionName)
  86. {
  87. if (isset($this->data[$extensionName])) {
  88. return $this->data[$extensionName];
  89. }
  90. $extension = $this->extensionManager->getExtension($this->structureType, $extensionName);
  91. // TODO: should not pass namespace here.
  92. // and indeed this call should be removed and the extension should be
  93. // passed the document.
  94. $extension->setLanguageCode($this->locale, $this->prefix, $this->internalPrefix);
  95. // passing the webspace and locale would also be unnecessary if we passed the
  96. // document
  97. $data = $extension->load($this->node, $this->webspaceName, $this->locale);
  98. $this->data[$extensionName] = $data;
  99. return $data;
  100. }
  101. #[\ReturnTypeWillChange]
  102. public function offsetExists($extensionName)
  103. {
  104. return $this->extensionManager->hasExtension($this->structureType, $extensionName);
  105. }
  106. public function toArray()
  107. {
  108. $result = [];
  109. foreach ($this->extensionManager->getExtensions($this->structureType) as $extension) {
  110. $result[$extension->getName()] = $this->offsetGet($extension->getName());
  111. }
  112. return $result;
  113. }
  114. }