vendor/shopware/core/Framework/DataAbstractionLayer/Event/EntityLoadedEvent.php line 91

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Event;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Field\AssociationField;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Extension;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToOneAssociationField;
  11. use Shopware\Core\Framework\Event\GenericEvent;
  12. use Shopware\Core\Framework\Event\NestedEvent;
  13. use Shopware\Core\Framework\Event\NestedEventCollection;
  14. use Shopware\Core\Framework\Feature;
  15. use Shopware\Core\Framework\Log\Package;
  16. #[Package('core')]
  17. class EntityLoadedEvent extends NestedEvent implements GenericEvent
  18. {
  19. /**
  20. * @var Entity[]
  21. */
  22. protected $entities;
  23. /**
  24. * @var EntityDefinition
  25. */
  26. protected $definition;
  27. /**
  28. * @var Context
  29. */
  30. protected $context;
  31. /**
  32. * @var string
  33. */
  34. protected $name;
  35. /**
  36. * @deprecated tag:v6.5.0 (flag:FEATURE_NEXT_16155) - Remove nested parameter, parameter is no more used. Nested loaded events are generated over EntityLoadedEventFactory
  37. *
  38. * @var bool
  39. */
  40. protected $nested = true;
  41. public function __construct(EntityDefinition $definition, array $entities, Context $context, bool $nested = true)
  42. {
  43. $this->entities = $entities;
  44. $this->definition = $definition;
  45. $this->context = $context;
  46. $this->name = $this->definition->getEntityName() . '.loaded';
  47. //@deprecated tag:v6.5.0 (flag:FEATURE_NEXT_16155) - Remove nested parameter, parameter is no more used. Nested loaded events are generated over EntityLoadedEventFactory
  48. $this->nested = $nested;
  49. }
  50. public function getEntities(): array
  51. {
  52. return $this->entities;
  53. }
  54. public function getDefinition(): EntityDefinition
  55. {
  56. return $this->definition;
  57. }
  58. public function getContext(): Context
  59. {
  60. return $this->context;
  61. }
  62. public function getName(): string
  63. {
  64. return $this->name;
  65. }
  66. public function getEvents(): ?NestedEventCollection
  67. {
  68. if (!$this->nested) {
  69. return null;
  70. }
  71. //@deprecated tag:v6.5.0 (flag:FEATURE_NEXT_16155) - Remove all code below in this function and related internal functions. Nested loaded events are generated over EntityLoadedEventFactory.
  72. $associations = $this->extractAssociations($this->definition, $this->entities);
  73. $events = [];
  74. foreach ($associations as $association) {
  75. $events[] = $this->createNested($association['definition'], $association['entities']);
  76. }
  77. return new NestedEventCollection($events);
  78. }
  79. public function getIds(): array
  80. {
  81. $ids = [];
  82. /** @var Entity $entity */
  83. foreach ($this->getEntities() as $entity) {
  84. $ids[] = $entity->getUniqueIdentifier();
  85. }
  86. return $ids;
  87. }
  88. /**
  89. * @deprecated tag:v6.5.0 (flag:FEATURE_NEXT_16155) - Remove all code below in this function and related internal functions. Nested loaded events are generated over EntityLoadedEventFactory.
  90. */
  91. protected function extractAssociations(EntityDefinition $definition, iterable $entities): array
  92. {
  93. Feature::triggerDeprecationOrThrow(
  94. 'v6.5.0.0',
  95. Feature::deprecatedMethodMessage(__CLASS__, __METHOD__, 'v6.5.0.0', 'EntityLoadedEventFactory')
  96. );
  97. $events = $this->extractAssociationsInCurrentLevel($definition, $entities);
  98. $recursive = $this->loadRecursivelyNestedAssociations($events);
  99. $events = $this->mergeIntoEvents($recursive, $events);
  100. return $events;
  101. }
  102. /**
  103. * @deprecated tag:v6.5.0 (flag:FEATURE_NEXT_16155) - Remove all code below in this function and related internal functions. Nested loaded events are generated over EntityLoadedEventFactory.
  104. */
  105. protected function createNested(EntityDefinition $definition, array $entities): EntityLoadedEvent
  106. {
  107. Feature::triggerDeprecationOrThrow(
  108. 'v6.5.0.0',
  109. Feature::deprecatedMethodMessage(__CLASS__, __METHOD__, 'v6.5.0.0', 'EntityLoadedEventFactory')
  110. );
  111. return new EntityLoadedEvent($definition, $entities, $this->context, false);
  112. }
  113. /**
  114. * @deprecated tag:v6.5.0 (flag:FEATURE_NEXT_16155) - Remove all code below in this function and related internal functions. Nested loaded events are generated over EntityLoadedEventFactory.
  115. */
  116. private function extractAssociationsInCurrentLevel(EntityDefinition $definition, iterable $entities): array
  117. {
  118. $associations = $definition->getFields();
  119. $events = [];
  120. foreach ($associations as $association) {
  121. if (!$association instanceof AssociationField) {
  122. continue;
  123. }
  124. $isExtension = $association->is(Extension::class);
  125. if ($association instanceof ManyToOneAssociationField || $association instanceof OneToOneAssociationField) {
  126. /** @var Entity $entity */
  127. foreach ($entities as $entity) {
  128. try {
  129. if ($isExtension) {
  130. $reference = $entity->getExtension($association->getPropertyName());
  131. } else {
  132. $reference = $entity->get($association->getPropertyName());
  133. }
  134. } catch (\InvalidArgumentException $e) {
  135. continue;
  136. }
  137. if ($reference) {
  138. $associatedDefinition = $association->getReferenceDefinition();
  139. $associationClass = $associatedDefinition->getEntityName();
  140. if (!isset($events[$associationClass])) {
  141. $events[$associationClass] = [
  142. 'definition' => $associatedDefinition,
  143. 'entities' => [],
  144. ];
  145. }
  146. $events[$associationClass]['entities'][] = $reference;
  147. }
  148. }
  149. continue;
  150. }
  151. $referenceDefinition = $association->getReferenceDefinition();
  152. if ($association instanceof ManyToManyAssociationField) {
  153. $referenceDefinition = $association->getToManyReferenceDefinition();
  154. }
  155. foreach ($entities as $entity) {
  156. try {
  157. if ($isExtension) {
  158. $references = $entity->getExtension($association->getPropertyName());
  159. } else {
  160. $references = $entity->get($association->getPropertyName());
  161. }
  162. } catch (\InvalidArgumentException $e) {
  163. continue;
  164. }
  165. if (empty($references)) {
  166. continue;
  167. }
  168. $referenceDefinitionClass = $referenceDefinition->getEntityName();
  169. if (!isset($events[$referenceDefinitionClass])) {
  170. $events[$referenceDefinitionClass] = [
  171. 'definition' => $referenceDefinition,
  172. 'entities' => [],
  173. ];
  174. }
  175. foreach ($references as $reference) {
  176. $events[$referenceDefinitionClass]['entities'][] = $reference;
  177. }
  178. }
  179. }
  180. return $events;
  181. }
  182. /**
  183. * @deprecated tag:v6.5.0 (flag:FEATURE_NEXT_16155) - Remove all code below in this function and related internal functions. Nested loaded events are generated over EntityLoadedEventFactory.
  184. */
  185. private function loadRecursivelyNestedAssociations(array $events): array
  186. {
  187. $recursive = [];
  188. foreach ($events as $nested) {
  189. /*
  190. * contains now an array of arrays
  191. *
  192. * [
  193. * [
  194. * ProductManufacturerDefinition => ['definition' => $definition, 'entities' => [$entity,$entity,$entity,$entity,$entity]],
  195. * ProductPriceDefinition => ['definition' => $definition, 'entities' => [$entity,$entity,$entity,$entity,$entity]]
  196. * ]
  197. * ]
  198. */
  199. $recursive[] = $this->extractAssociations($nested['definition'], $nested['entities']);
  200. }
  201. return $recursive;
  202. }
  203. /**
  204. * @deprecated tag:v6.5.0 (flag:FEATURE_NEXT_16155) - Remove all code below in this function and related internal functions. Nested loaded events are generated over EntityLoadedEventFactory.
  205. */
  206. private function mergeIntoEvents(array $recursive, array $events): array
  207. {
  208. foreach ($recursive as $nested) {
  209. if (empty($nested)) {
  210. continue;
  211. }
  212. //iterate nested array of definitions and entities and merge them into root $events
  213. foreach ($nested as $nestedDefinition => $nestedCollection) {
  214. if (!isset($events[$nestedDefinition])) {
  215. $events[$nestedDefinition] = $nestedCollection;
  216. continue;
  217. }
  218. foreach ($nestedCollection['entities'] as $nestedEntity) {
  219. $events[$nestedDefinition]['entities'][] = $nestedEntity;
  220. }
  221. }
  222. }
  223. return $events;
  224. }
  225. }