vendor/sulu/sulu/src/Sulu/Component/DocumentManager/Metadata/BaseMetadataFactory.php line 189

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\DocumentManager\Metadata;
  11. use PHPCR\NodeInterface;
  12. use Sulu\Component\DocumentManager\ClassNameInflector;
  13. use Sulu\Component\DocumentManager\Event\MetadataLoadEvent;
  14. use Sulu\Component\DocumentManager\Events;
  15. use Sulu\Component\DocumentManager\Exception\MetadataNotFoundException;
  16. use Sulu\Component\DocumentManager\Metadata;
  17. use Sulu\Component\DocumentManager\MetadataFactoryInterface;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. /**
  20. * Simple metadata factory which uses an array map.
  21. *
  22. * Note that this class does not implement the getMetadataForPhpcrNode method
  23. * as that would require a circular dependency.
  24. */
  25. class BaseMetadataFactory implements MetadataFactoryInterface
  26. {
  27. /**
  28. * @var array
  29. */
  30. private $aliasMap = [];
  31. /**
  32. * @var array
  33. */
  34. private $classMap = [];
  35. /**
  36. * @var array
  37. */
  38. private $phpcrTypeMap = [];
  39. /**
  40. * @var Metadata[]
  41. */
  42. private $metadata = [];
  43. public function __construct(private EventDispatcherInterface $dispatcher, array $mapping)
  44. {
  45. foreach ($mapping as $map) {
  46. $this->aliasMap[$map['alias']] = $map;
  47. $this->classMap[$map['class']] = $map;
  48. $this->phpcrTypeMap[$map['phpcr_type']] = $map;
  49. }
  50. }
  51. public function getMetadataForAlias($alias)
  52. {
  53. if (!isset($this->aliasMap[$alias])) {
  54. throw new MetadataNotFoundException(\sprintf(
  55. 'Metadata with alias "%s" not found, known aliases: "%s"',
  56. $alias, \implode('", "', \array_keys($this->aliasMap))
  57. ));
  58. }
  59. $map = $this->aliasMap[$alias];
  60. return $this->loadMetadata($map);
  61. }
  62. public function getMetadataForPhpcrType($phpcrType)
  63. {
  64. if (!isset($this->phpcrTypeMap[$phpcrType])) {
  65. throw new MetadataNotFoundException(\sprintf(
  66. 'Metadata with phpcrType "%s" not found, known phpcrTypes: "%s"',
  67. $phpcrType, \implode('", "', \array_keys($this->phpcrTypeMap))
  68. ));
  69. }
  70. $map = $this->phpcrTypeMap[$phpcrType];
  71. return $this->loadMetadata($map);
  72. }
  73. public function hasMetadataForPhpcrType($phpcrType)
  74. {
  75. return isset($this->phpcrTypeMap[$phpcrType]);
  76. }
  77. public function hasMetadataForClass($class)
  78. {
  79. $class = ClassNameInflector::getUserClassName($class);
  80. return isset($this->classMap[$class]);
  81. }
  82. public function getMetadataForClass($class)
  83. {
  84. $class = ClassNameInflector::getUserClassName($class);
  85. if (!isset($this->classMap[$class])) {
  86. throw new MetadataNotFoundException(\sprintf(
  87. 'Metadata with class "%s" not found, known classes: "%s"',
  88. $class, \implode('", "', \array_keys($this->classMap))
  89. ));
  90. }
  91. $map = $this->classMap[$class];
  92. return $this->loadMetadata($map);
  93. }
  94. public function hasAlias($alias)
  95. {
  96. return isset($this->aliasMap[$alias]);
  97. }
  98. public function getAliases()
  99. {
  100. return \array_keys($this->aliasMap);
  101. }
  102. public function getMetadataForPhpcrNode(NodeInterface $node)
  103. {
  104. throw new \BadMethodCallException(
  105. 'The BaseMetadataFactory does not implement this method'
  106. );
  107. }
  108. /**
  109. * @return array
  110. */
  111. public function getPhpcrTypeMap()
  112. {
  113. return $this->phpcrTypeMap;
  114. }
  115. public function getAllMetadata()
  116. {
  117. $metadatas = [];
  118. foreach (\array_keys($this->aliasMap) as $alias) {
  119. $metadatas[] = $this->getMetadataForAlias($alias);
  120. }
  121. return $metadatas;
  122. }
  123. /**
  124. * @param array $mapping
  125. *
  126. * @return Metadata
  127. */
  128. private function loadMetadata($mapping)
  129. {
  130. $mapping = \array_merge([
  131. 'alias' => null,
  132. 'phpcr_type' => null,
  133. 'form_type' => null,
  134. 'class' => null,
  135. 'mapping' => [],
  136. 'sync_remove_live' => true,
  137. 'set_default_author' => true,
  138. ], $mapping);
  139. if (isset($this->metadata[$mapping['alias']])) {
  140. return $this->metadata[$mapping['alias']];
  141. }
  142. $metadata = new Metadata();
  143. $metadata->setAlias($mapping['alias']);
  144. $metadata->setPhpcrType($mapping['phpcr_type']);
  145. $metadata->setFormType($mapping['form_type']);
  146. $metadata->setClass($mapping['class']);
  147. $metadata->setSyncRemoveLive($mapping['sync_remove_live']);
  148. $metadata->setDefaultAuthor($mapping['set_default_author']);
  149. foreach ($mapping['mapping'] as $fieldName => $fieldMapping) {
  150. $fieldMapping = \array_merge([
  151. 'encoding' => 'content',
  152. 'property' => $fieldName,
  153. ], $fieldMapping);
  154. $metadata->addFieldMapping($fieldName, $fieldMapping);
  155. }
  156. $event = new MetadataLoadEvent($metadata);
  157. $this->dispatcher->dispatch($event, Events::METADATA_LOAD);
  158. $this->metadata[$mapping['alias']] = $metadata;
  159. return $metadata;
  160. }
  161. }