vendor/sulu/sulu/src/Sulu/Component/Content/Compat/StructureManager.php line 34

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\Compat;
  11. use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
  12. use Sulu\Component\Content\Compat\Structure\LegacyPropertyFactory;
  13. use Sulu\Component\Content\Metadata\Factory\Exception\StructureTypeNotFoundException;
  14. use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
  15. use Sulu\Component\Content\Metadata\StructureMetadata;
  16. /**
  17. * generates subclasses of structure to match template definitions.
  18. * this classes will be cached in Symfony cache.
  19. */
  20. class StructureManager implements StructureManagerInterface
  21. {
  22. public function __construct(
  23. private StructureMetadataFactoryInterface $structureFactory,
  24. private DocumentInspector $inspector,
  25. private LegacyPropertyFactory $propertyFactory,
  26. private array $typeMap,
  27. ) {
  28. }
  29. public function getStructure($key, $type = Structure::TYPE_PAGE)
  30. {
  31. try {
  32. $metadata = $this->structureFactory->getStructureMetadata($type, $key);
  33. } catch (StructureTypeNotFoundException $exception) {
  34. return;
  35. }
  36. return $this->wrapStructure($type, $metadata);
  37. }
  38. public function getStructures($type = Structure::TYPE_PAGE)
  39. {
  40. $wrappedStructures = [];
  41. $structures = $this->structureFactory->getStructures($type);
  42. foreach ($structures as $structure) {
  43. $wrappedStructures[] = $this->wrapStructure($type, $structure);
  44. }
  45. return $wrappedStructures;
  46. }
  47. public function wrapStructure($type, StructureMetadata $structure)
  48. {
  49. if (!isset($this->typeMap[$type])) {
  50. throw new \InvalidArgumentException(
  51. \sprintf(
  52. 'Invalid legacy type "%s", known types: "%s"',
  53. $type,
  54. \implode('", "', \array_keys($this->typeMap))
  55. )
  56. );
  57. }
  58. $class = $this->typeMap[$type];
  59. return new $class($structure, $this->inspector, $this->propertyFactory);
  60. }
  61. }