diff options
author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/doctrine/orm/src/Cache/DefaultCollectionHydrator.php | |
parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/orm/src/Cache/DefaultCollectionHydrator.php')
-rw-r--r-- | vendor/doctrine/orm/src/Cache/DefaultCollectionHydrator.php | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Cache/DefaultCollectionHydrator.php b/vendor/doctrine/orm/src/Cache/DefaultCollectionHydrator.php new file mode 100644 index 0000000..249d48f --- /dev/null +++ b/vendor/doctrine/orm/src/Cache/DefaultCollectionHydrator.php | |||
@@ -0,0 +1,75 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Cache; | ||
6 | |||
7 | use Doctrine\Common\Collections\Collection; | ||
8 | use Doctrine\ORM\Cache\Persister\CachedPersister; | ||
9 | use Doctrine\ORM\EntityManagerInterface; | ||
10 | use Doctrine\ORM\Mapping\ClassMetadata; | ||
11 | use Doctrine\ORM\PersistentCollection; | ||
12 | use Doctrine\ORM\Query; | ||
13 | use Doctrine\ORM\UnitOfWork; | ||
14 | |||
15 | use function assert; | ||
16 | |||
17 | /** | ||
18 | * Default hydrator cache for collections | ||
19 | */ | ||
20 | class DefaultCollectionHydrator implements CollectionHydrator | ||
21 | { | ||
22 | private readonly UnitOfWork $uow; | ||
23 | |||
24 | /** @var array<string,mixed> */ | ||
25 | private static array $hints = [Query::HINT_CACHE_ENABLED => true]; | ||
26 | |||
27 | public function __construct( | ||
28 | private readonly EntityManagerInterface $em, | ||
29 | ) { | ||
30 | $this->uow = $em->getUnitOfWork(); | ||
31 | } | ||
32 | |||
33 | public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, array|Collection $collection): CollectionCacheEntry | ||
34 | { | ||
35 | $data = []; | ||
36 | |||
37 | foreach ($collection as $index => $entity) { | ||
38 | $data[$index] = new EntityCacheKey($metadata->rootEntityName, $this->uow->getEntityIdentifier($entity)); | ||
39 | } | ||
40 | |||
41 | return new CollectionCacheEntry($data); | ||
42 | } | ||
43 | |||
44 | public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection): array|null | ||
45 | { | ||
46 | $assoc = $metadata->associationMappings[$key->association]; | ||
47 | $targetPersister = $this->uow->getEntityPersister($assoc->targetEntity); | ||
48 | assert($targetPersister instanceof CachedPersister); | ||
49 | $targetRegion = $targetPersister->getCacheRegion(); | ||
50 | $list = []; | ||
51 | |||
52 | /** @var EntityCacheEntry[]|null $entityEntries */ | ||
53 | $entityEntries = $targetRegion->getMultiple($entry); | ||
54 | |||
55 | if ($entityEntries === null) { | ||
56 | return null; | ||
57 | } | ||
58 | |||
59 | foreach ($entityEntries as $index => $entityEntry) { | ||
60 | $entity = $this->uow->createEntity( | ||
61 | $entityEntry->class, | ||
62 | $entityEntry->resolveAssociationEntries($this->em), | ||
63 | self::$hints, | ||
64 | ); | ||
65 | |||
66 | $collection->hydrateSet($index, $entity); | ||
67 | |||
68 | $list[$index] = $entity; | ||
69 | } | ||
70 | |||
71 | $this->uow->hydrationComplete(); | ||
72 | |||
73 | return $list; | ||
74 | } | ||
75 | } | ||