MetadataPool.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\EntityManager;
  7. use Magento\Framework\EntityManager\Sequence\SequenceFactory;
  8. use Magento\Framework\ObjectManagerInterface;
  9. /**
  10. * Class MetadataPool
  11. *
  12. * @api
  13. * @since 100.1.0
  14. */
  15. class MetadataPool
  16. {
  17. /**
  18. * @var ObjectManagerInterface
  19. * @since 100.1.0
  20. */
  21. protected $objectManager;
  22. /**
  23. * @var array
  24. * @since 100.1.0
  25. */
  26. protected $metadata;
  27. /**
  28. * @var \Magento\Framework\EntityManager\EntityMetadata[]
  29. * @since 100.1.0
  30. */
  31. protected $registry;
  32. /**
  33. * @var SequenceFactory
  34. * @since 100.1.0
  35. */
  36. protected $sequenceFactory;
  37. /**
  38. * MetadataPool constructor.
  39. * @param ObjectManagerInterface $objectManager
  40. * @param SequenceFactory $sequenceFactory
  41. * @param array $metadata
  42. */
  43. public function __construct(
  44. ObjectManagerInterface $objectManager,
  45. SequenceFactory $sequenceFactory,
  46. array $metadata
  47. ) {
  48. $this->objectManager = $objectManager;
  49. $this->sequenceFactory = $sequenceFactory;
  50. $this->metadata = $metadata;
  51. }
  52. /**
  53. * @param string $entityType
  54. * @return EntityMetadataInterface
  55. */
  56. private function createMetadata($entityType)
  57. {
  58. //@todo: use ID as default if , check is type has EAV attributes
  59. $connectionName = isset($this->metadata[$entityType]['connectionName'])
  60. ? $this->metadata[$entityType]['connectionName']
  61. : 'default';
  62. $eavEntityType = isset($this->metadata[$entityType]['eavEntityType'])
  63. ? $this->metadata[$entityType]['eavEntityType']
  64. : null;
  65. $entityContext = isset($this->metadata[$entityType]['entityContext'])
  66. ? $this->metadata[$entityType]['entityContext']
  67. : [];
  68. return $this->objectManager->create(
  69. EntityMetadataInterface::class,
  70. [
  71. 'entityTableName' => $this->metadata[$entityType]['entityTableName'],
  72. 'eavEntityType' => $eavEntityType,
  73. 'connectionName' => $connectionName,
  74. 'identifierField' => $this->metadata[$entityType]['identifierField'],
  75. 'sequence' => $this->sequenceFactory->create($entityType, $this->metadata),
  76. 'entityContext' => $entityContext
  77. ]
  78. );
  79. }
  80. /**
  81. * @param string $entityType
  82. * @return EntityMetadataInterface
  83. * @throws \Exception
  84. * @since 100.1.0
  85. */
  86. public function getMetadata($entityType)
  87. {
  88. if (!isset($this->metadata[$entityType])) {
  89. throw new \Exception(sprintf('Unknown entity type: %s requested', $entityType));
  90. }
  91. if (!isset($this->registry[$entityType])) {
  92. $this->registry[$entityType] = $this->createMetadata($entityType);
  93. }
  94. return $this->registry[$entityType];
  95. }
  96. /**
  97. * @param string $entityType
  98. * @return HydratorInterface
  99. * @deprecated 100.1.0
  100. * @since 100.1.0
  101. */
  102. public function getHydrator($entityType)
  103. {
  104. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  105. return $objectManager->get(HydratorPool::class)->getHydrator($entityType);
  106. }
  107. /**
  108. * Check if entity type configuration was set to metadata
  109. *
  110. * @param string $entityType
  111. * @return bool
  112. * @since 100.1.0
  113. */
  114. public function hasConfiguration($entityType)
  115. {
  116. return isset($this->metadata[$entityType]);
  117. }
  118. }