diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Cache/DefaultEntityHydrator.php')
| -rw-r--r-- | vendor/doctrine/orm/src/Cache/DefaultEntityHydrator.php | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Cache/DefaultEntityHydrator.php b/vendor/doctrine/orm/src/Cache/DefaultEntityHydrator.php new file mode 100644 index 0000000..6bd1524 --- /dev/null +++ b/vendor/doctrine/orm/src/Cache/DefaultEntityHydrator.php | |||
| @@ -0,0 +1,176 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\ORM\Cache; | ||
| 6 | |||
| 7 | use Doctrine\ORM\EntityManagerInterface; | ||
| 8 | use Doctrine\ORM\Mapping\ClassMetadata; | ||
| 9 | use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; | ||
| 10 | use Doctrine\ORM\Query; | ||
| 11 | use Doctrine\ORM\UnitOfWork; | ||
| 12 | use Doctrine\ORM\Utility\IdentifierFlattener; | ||
| 13 | |||
| 14 | use function assert; | ||
| 15 | use function is_array; | ||
| 16 | use function is_object; | ||
| 17 | use function reset; | ||
| 18 | |||
| 19 | /** | ||
| 20 | * Default hydrator cache for entities | ||
| 21 | */ | ||
| 22 | class DefaultEntityHydrator implements EntityHydrator | ||
| 23 | { | ||
| 24 | private readonly UnitOfWork $uow; | ||
| 25 | private readonly IdentifierFlattener $identifierFlattener; | ||
| 26 | |||
| 27 | /** @var array<string,mixed> */ | ||
| 28 | private static array $hints = [Query::HINT_CACHE_ENABLED => true]; | ||
| 29 | |||
| 30 | public function __construct( | ||
| 31 | private readonly EntityManagerInterface $em, | ||
| 32 | ) { | ||
| 33 | $this->uow = $em->getUnitOfWork(); | ||
| 34 | $this->identifierFlattener = new IdentifierFlattener($em->getUnitOfWork(), $em->getMetadataFactory()); | ||
| 35 | } | ||
| 36 | |||
| 37 | public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, object $entity): EntityCacheEntry | ||
| 38 | { | ||
| 39 | $data = $this->uow->getOriginalEntityData($entity); | ||
| 40 | $data = [...$data, ...$metadata->getIdentifierValues($entity)]; // why update has no identifier values ? | ||
| 41 | |||
| 42 | if ($metadata->requiresFetchAfterChange) { | ||
| 43 | if ($metadata->isVersioned) { | ||
| 44 | assert($metadata->versionField !== null); | ||
| 45 | $data[$metadata->versionField] = $metadata->getFieldValue($entity, $metadata->versionField); | ||
| 46 | } | ||
| 47 | |||
| 48 | foreach ($metadata->fieldMappings as $name => $fieldMapping) { | ||
| 49 | if (isset($fieldMapping->generated)) { | ||
| 50 | $data[$name] = $metadata->getFieldValue($entity, $name); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | foreach ($metadata->associationMappings as $name => $assoc) { | ||
| 56 | if (! isset($data[$name])) { | ||
| 57 | continue; | ||
| 58 | } | ||
| 59 | |||
| 60 | if (! $assoc->isToOne()) { | ||
| 61 | unset($data[$name]); | ||
| 62 | |||
| 63 | continue; | ||
| 64 | } | ||
| 65 | |||
| 66 | if (! isset($assoc->cache)) { | ||
| 67 | $targetClassMetadata = $this->em->getClassMetadata($assoc->targetEntity); | ||
| 68 | $owningAssociation = $this->em->getMetadataFactory()->getOwningSide($assoc); | ||
| 69 | $associationIds = $this->identifierFlattener->flattenIdentifier( | ||
| 70 | $targetClassMetadata, | ||
| 71 | $targetClassMetadata->getIdentifierValues($data[$name]), | ||
| 72 | ); | ||
| 73 | |||
| 74 | unset($data[$name]); | ||
| 75 | |||
| 76 | foreach ($associationIds as $fieldName => $fieldValue) { | ||
| 77 | if (isset($targetClassMetadata->fieldMappings[$fieldName])) { | ||
| 78 | assert($owningAssociation->isToOneOwningSide()); | ||
| 79 | $fieldMapping = $targetClassMetadata->fieldMappings[$fieldName]; | ||
| 80 | |||
| 81 | $data[$owningAssociation->targetToSourceKeyColumns[$fieldMapping->columnName]] = $fieldValue; | ||
| 82 | |||
| 83 | continue; | ||
| 84 | } | ||
| 85 | |||
| 86 | $targetAssoc = $targetClassMetadata->associationMappings[$fieldName]; | ||
| 87 | |||
| 88 | assert($assoc->isToOneOwningSide()); | ||
| 89 | foreach ($assoc->targetToSourceKeyColumns as $referencedColumn => $localColumn) { | ||
| 90 | if (isset($targetAssoc->sourceToTargetKeyColumns[$referencedColumn])) { | ||
| 91 | $data[$localColumn] = $fieldValue; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | continue; | ||
| 97 | } | ||
| 98 | |||
| 99 | if (! isset($assoc->id)) { | ||
| 100 | $targetClass = DefaultProxyClassNameResolver::getClass($data[$name]); | ||
| 101 | $targetId = $this->uow->getEntityIdentifier($data[$name]); | ||
| 102 | $data[$name] = new AssociationCacheEntry($targetClass, $targetId); | ||
| 103 | |||
| 104 | continue; | ||
| 105 | } | ||
| 106 | |||
| 107 | // handle association identifier | ||
| 108 | $targetId = is_object($data[$name]) && $this->uow->isInIdentityMap($data[$name]) | ||
| 109 | ? $this->uow->getEntityIdentifier($data[$name]) | ||
| 110 | : $data[$name]; | ||
| 111 | |||
| 112 | // @TODO - fix it ! | ||
| 113 | // handle UnitOfWork#createEntity hash generation | ||
| 114 | if (! is_array($targetId)) { | ||
| 115 | assert($assoc->isToOneOwningSide()); | ||
| 116 | $data[reset($assoc->joinColumnFieldNames)] = $targetId; | ||
| 117 | |||
| 118 | $targetEntity = $this->em->getClassMetadata($assoc->targetEntity); | ||
| 119 | $targetId = [$targetEntity->identifier[0] => $targetId]; | ||
| 120 | } | ||
| 121 | |||
| 122 | $data[$name] = new AssociationCacheEntry($assoc->targetEntity, $targetId); | ||
| 123 | } | ||
| 124 | |||
| 125 | return new EntityCacheEntry($metadata->name, $data); | ||
| 126 | } | ||
| 127 | |||
| 128 | public function loadCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, EntityCacheEntry $entry, object|null $entity = null): object|null | ||
| 129 | { | ||
| 130 | $data = $entry->data; | ||
| 131 | $hints = self::$hints; | ||
| 132 | |||
| 133 | if ($entity !== null) { | ||
| 134 | $hints[Query::HINT_REFRESH] = true; | ||
| 135 | $hints[Query::HINT_REFRESH_ENTITY] = $entity; | ||
| 136 | } | ||
| 137 | |||
| 138 | foreach ($metadata->associationMappings as $name => $assoc) { | ||
| 139 | if (! isset($assoc->cache) || ! isset($data[$name])) { | ||
| 140 | continue; | ||
| 141 | } | ||
| 142 | |||
| 143 | $assocClass = $data[$name]->class; | ||
| 144 | $assocId = $data[$name]->identifier; | ||
| 145 | $isEagerLoad = ($assoc->fetch === ClassMetadata::FETCH_EAGER || ($assoc->isOneToOne() && ! $assoc->isOwningSide())); | ||
| 146 | |||
| 147 | if (! $isEagerLoad) { | ||
| 148 | $data[$name] = $this->em->getReference($assocClass, $assocId); | ||
| 149 | |||
| 150 | continue; | ||
| 151 | } | ||
| 152 | |||
| 153 | $assocMetadata = $this->em->getClassMetadata($assoc->targetEntity); | ||
| 154 | $assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocId); | ||
| 155 | $assocPersister = $this->uow->getEntityPersister($assoc->targetEntity); | ||
| 156 | $assocRegion = $assocPersister->getCacheRegion(); | ||
| 157 | $assocEntry = $assocRegion->get($assocKey); | ||
| 158 | |||
| 159 | if ($assocEntry === null) { | ||
| 160 | return null; | ||
| 161 | } | ||
| 162 | |||
| 163 | $data[$name] = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), $hints); | ||
| 164 | } | ||
| 165 | |||
| 166 | if ($entity !== null) { | ||
| 167 | $this->uow->registerManaged($entity, $key->identifier, $data); | ||
| 168 | } | ||
| 169 | |||
| 170 | $result = $this->uow->createEntity($entry->class, $data, $hints); | ||
| 171 | |||
| 172 | $this->uow->hydrationComplete(); | ||
| 173 | |||
| 174 | return $result; | ||
| 175 | } | ||
| 176 | } | ||
