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

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. * @param string $structureType
  28. * @param string $locale
  29. * @param string $prefix
  30. * @param string $internalPrefix
  31. * @param string $webspaceName
  32. */
  33. public function __construct(
  34. private $structureType,
  35. private ExtensionManagerInterface $extensionManager,
  36. private NodeInterface $node,
  37. private $locale,
  38. private $prefix,
  39. private $internalPrefix,
  40. private $webspaceName
  41. ) {
  42. parent::__construct();
  43. }
  44. /**
  45. * Lazily evaluate the value for the given extension.
  46. *
  47. * @param string $extensionName
  48. */
  49. #[\ReturnTypeWillChange]
  50. public function offsetGet($extensionName)
  51. {
  52. if (isset($this->data[$extensionName])) {
  53. return $this->data[$extensionName];
  54. }
  55. $extension = $this->extensionManager->getExtension($this->structureType, $extensionName);
  56. // TODO: should not pass namespace here.
  57. // and indeed this call should be removed and the extension should be
  58. // passed the document.
  59. $extension->setLanguageCode($this->locale, $this->prefix, $this->internalPrefix);
  60. // passing the webspace and locale would also be unnecessary if we passed the
  61. // document
  62. $data = $extension->load($this->node, $this->webspaceName, $this->locale);
  63. $this->data[$extensionName] = $data;
  64. return $data;
  65. }
  66. #[\ReturnTypeWillChange]
  67. public function offsetExists($extensionName)
  68. {
  69. return $this->extensionManager->hasExtension($this->structureType, $extensionName);
  70. }
  71. public function toArray()
  72. {
  73. $result = [];
  74. foreach ($this->extensionManager->getExtensions($this->structureType) as $extension) {
  75. $result[$extension->getName()] = $this->offsetGet($extension->getName());
  76. }
  77. return $result;
  78. }
  79. }