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

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 EventDispatcherInterface
  41. */
  42. private $dispatcher;
  43. /**
  44. * @var Metadata[]
  45. */
  46. private $metadata = [];
  47. public function __construct(EventDispatcherInterface $dispatcher, array $mapping)
  48. {
  49. $this->dispatcher = $dispatcher;
  50. foreach ($mapping as $map) {
  51. $this->aliasMap[$map['alias']] = $map;
  52. $this->classMap[$map['class']] = $map;
  53. $this->phpcrTypeMap[$map['phpcr_type']] = $map;
  54. }
  55. }
  56. public function getMetadataForAlias($alias)
  57. {
  58. if (!isset($this->aliasMap[$alias])) {
  59. throw new MetadataNotFoundException(\sprintf(
  60. 'Metadata with alias "%s" not found, known aliases: "%s"',
  61. $alias, \implode('", "', \array_keys($this->aliasMap))
  62. ));
  63. }
  64. $map = $this->aliasMap[$alias];
  65. return $this->loadMetadata($map);
  66. }
  67. public function getMetadataForPhpcrType($phpcrType)
  68. {
  69. if (!isset($this->phpcrTypeMap[$phpcrType])) {
  70. throw new MetadataNotFoundException(\sprintf(
  71. 'Metadata with phpcrType "%s" not found, known phpcrTypes: "%s"',
  72. $phpcrType, \implode('", "', \array_keys($this->phpcrTypeMap))
  73. ));
  74. }
  75. $map = $this->phpcrTypeMap[$phpcrType];
  76. return $this->loadMetadata($map);
  77. }
  78. public function hasMetadataForPhpcrType($phpcrType)
  79. {
  80. return isset($this->phpcrTypeMap[$phpcrType]);
  81. }
  82. public function hasMetadataForClass($class)
  83. {
  84. $class = ClassNameInflector::getUserClassName($class);
  85. return isset($this->classMap[$class]);
  86. }
  87. public function getMetadataForClass($class)
  88. {
  89. $class = ClassNameInflector::getUserClassName($class);
  90. if (!isset($this->classMap[$class])) {
  91. throw new MetadataNotFoundException(\sprintf(
  92. 'Metadata with class "%s" not found, known classes: "%s"',
  93. $class, \implode('", "', \array_keys($this->classMap))
  94. ));
  95. }
  96. $map = $this->classMap[$class];
  97. return $this->loadMetadata($map);
  98. }
  99. public function hasAlias($alias)
  100. {
  101. return isset($this->aliasMap[$alias]);
  102. }
  103. public function getAliases()
  104. {
  105. return \array_keys($this->aliasMap);
  106. }
  107. public function getMetadataForPhpcrNode(NodeInterface $node)
  108. {
  109. throw new \BadMethodCallException(
  110. 'The BaseMetadataFactory does not implement this method'
  111. );
  112. }
  113. /**
  114. * @return array
  115. */
  116. public function getPhpcrTypeMap()
  117. {
  118. return $this->phpcrTypeMap;
  119. }
  120. public function getAllMetadata()
  121. {
  122. $metadatas = [];
  123. foreach (\array_keys($this->aliasMap) as $alias) {
  124. $metadatas[] = $this->getMetadataForAlias($alias);
  125. }
  126. return $metadatas;
  127. }
  128. /**
  129. * @param array $mapping
  130. *
  131. * @return Metadata
  132. */
  133. private function loadMetadata($mapping)
  134. {
  135. $mapping = \array_merge([
  136. 'alias' => null,
  137. 'phpcr_type' => null,
  138. 'form_type' => null,
  139. 'class' => null,
  140. 'mapping' => [],
  141. 'sync_remove_live' => true,
  142. 'set_default_author' => true,
  143. ], $mapping);
  144. if (isset($this->metadata[$mapping['alias']])) {
  145. return $this->metadata[$mapping['alias']];
  146. }
  147. $metadata = new Metadata();
  148. $metadata->setAlias($mapping['alias']);
  149. $metadata->setPhpcrType($mapping['phpcr_type']);
  150. $metadata->setFormType($mapping['form_type']);
  151. $metadata->setClass($mapping['class']);
  152. $metadata->setSyncRemoveLive($mapping['sync_remove_live']);
  153. $metadata->setDefaultAuthor($mapping['set_default_author']);
  154. foreach ($mapping['mapping'] as $fieldName => $fieldMapping) {
  155. $fieldMapping = \array_merge([
  156. 'encoding' => 'content',
  157. 'property' => $fieldName,
  158. ], $fieldMapping);
  159. $metadata->addFieldMapping($fieldName, $fieldMapping);
  160. }
  161. $event = new MetadataLoadEvent($metadata);
  162. $this->dispatcher->dispatch($event, Events::METADATA_LOAD);
  163. $this->metadata[$mapping['alias']] = $metadata;
  164. return $metadata;
  165. }
  166. }