mailpoet
/
vendor-prefixed
/
doctrine
/
orm
/
src
/
Cache
/
Persister
/
Collection
/
AbstractCollectionPersister.php
AbstractCollectionPersister.php
9 months ago
CachedCollectionPersister.php
9 months ago
NonStrictReadWriteCachedCollectionPersister.php
9 months ago
ReadOnlyCachedCollectionPersister.php
9 months ago
ReadWriteCachedCollectionPersister.php
9 months ago
index.php
9 months ago
AbstractCollectionPersister.php
159 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Cache\Persister\Collection; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\Common\Collections\Collection; |
| 6 | use MailPoetVendor\Doctrine\Common\Collections\Criteria; |
| 7 | use MailPoetVendor\Doctrine\Deprecations\Deprecation; |
| 8 | use MailPoetVendor\Doctrine\ORM\Cache\CollectionCacheKey; |
| 9 | use MailPoetVendor\Doctrine\ORM\Cache\CollectionHydrator; |
| 10 | use MailPoetVendor\Doctrine\ORM\Cache\EntityCacheKey; |
| 11 | use MailPoetVendor\Doctrine\ORM\Cache\Logging\CacheLogger; |
| 12 | use MailPoetVendor\Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister; |
| 13 | use MailPoetVendor\Doctrine\ORM\Cache\Region; |
| 14 | use MailPoetVendor\Doctrine\ORM\EntityManagerInterface; |
| 15 | use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadata; |
| 16 | use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadataFactory; |
| 17 | use MailPoetVendor\Doctrine\ORM\PersistentCollection; |
| 18 | use MailPoetVendor\Doctrine\ORM\Persisters\Collection\CollectionPersister; |
| 19 | use MailPoetVendor\Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; |
| 20 | use MailPoetVendor\Doctrine\ORM\Query\FilterCollection; |
| 21 | use MailPoetVendor\Doctrine\ORM\UnitOfWork; |
| 22 | use function array_values; |
| 23 | use function assert; |
| 24 | use function count; |
| 25 | abstract class AbstractCollectionPersister implements CachedCollectionPersister |
| 26 | { |
| 27 | protected $uow; |
| 28 | protected $metadataFactory; |
| 29 | protected $persister; |
| 30 | protected $sourceEntity; |
| 31 | protected $targetEntity; |
| 32 | protected $association; |
| 33 | protected $queuedCache = []; |
| 34 | protected $region; |
| 35 | protected $regionName; |
| 36 | protected $filters; |
| 37 | protected $hydrator; |
| 38 | protected $cacheLogger; |
| 39 | public function __construct(CollectionPersister $persister, Region $region, EntityManagerInterface $em, array $association) |
| 40 | { |
| 41 | $configuration = $em->getConfiguration(); |
| 42 | $cacheConfig = $configuration->getSecondLevelCacheConfiguration(); |
| 43 | $cacheFactory = $cacheConfig->getCacheFactory(); |
| 44 | $this->region = $region; |
| 45 | $this->persister = $persister; |
| 46 | $this->association = $association; |
| 47 | $this->filters = $em->getFilters(); |
| 48 | $this->regionName = $region->getName(); |
| 49 | $this->uow = $em->getUnitOfWork(); |
| 50 | $this->metadataFactory = $em->getMetadataFactory(); |
| 51 | $this->cacheLogger = $cacheConfig->getCacheLogger(); |
| 52 | $this->hydrator = $cacheFactory->buildCollectionHydrator($em, $association); |
| 53 | $this->sourceEntity = $em->getClassMetadata($association['sourceEntity']); |
| 54 | $this->targetEntity = $em->getClassMetadata($association['targetEntity']); |
| 55 | } |
| 56 | public function getCacheRegion() |
| 57 | { |
| 58 | return $this->region; |
| 59 | } |
| 60 | public function getSourceEntityMetadata() |
| 61 | { |
| 62 | return $this->sourceEntity; |
| 63 | } |
| 64 | public function getTargetEntityMetadata() |
| 65 | { |
| 66 | return $this->targetEntity; |
| 67 | } |
| 68 | public function loadCollectionCache(PersistentCollection $collection, CollectionCacheKey $key) |
| 69 | { |
| 70 | $cache = $this->region->get($key); |
| 71 | if ($cache === null) { |
| 72 | return null; |
| 73 | } |
| 74 | return $this->hydrator->loadCacheEntry($this->sourceEntity, $key, $cache, $collection); |
| 75 | } |
| 76 | public function storeCollectionCache(CollectionCacheKey $key, $elements) |
| 77 | { |
| 78 | $associationMapping = $this->sourceEntity->associationMappings[$key->association]; |
| 79 | $targetPersister = $this->uow->getEntityPersister($this->targetEntity->rootEntityName); |
| 80 | assert($targetPersister instanceof CachedEntityPersister); |
| 81 | $targetRegion = $targetPersister->getCacheRegion(); |
| 82 | $targetHydrator = $targetPersister->getEntityHydrator(); |
| 83 | // Only preserve ordering if association configured it |
| 84 | if (!(isset($associationMapping['indexBy']) && $associationMapping['indexBy'])) { |
| 85 | // Elements may be an array or a Collection |
| 86 | $elements = array_values($elements instanceof Collection ? $elements->getValues() : $elements); |
| 87 | } |
| 88 | $entry = $this->hydrator->buildCacheEntry($this->targetEntity, $key, $elements); |
| 89 | foreach ($entry->identifiers as $index => $entityKey) { |
| 90 | if ($targetRegion->contains($entityKey)) { |
| 91 | continue; |
| 92 | } |
| 93 | $class = $this->targetEntity; |
| 94 | $className = DefaultProxyClassNameResolver::getClass($elements[$index]); |
| 95 | if ($className !== $this->targetEntity->name) { |
| 96 | $class = $this->metadataFactory->getMetadataFor($className); |
| 97 | } |
| 98 | $entity = $elements[$index]; |
| 99 | $entityEntry = $targetHydrator->buildCacheEntry($class, $entityKey, $entity); |
| 100 | $targetRegion->put($entityKey, $entityEntry); |
| 101 | } |
| 102 | $cached = $this->region->put($key, $entry); |
| 103 | if ($this->cacheLogger && $cached) { |
| 104 | $this->cacheLogger->collectionCachePut($this->regionName, $key); |
| 105 | } |
| 106 | } |
| 107 | public function contains(PersistentCollection $collection, $element) |
| 108 | { |
| 109 | return $this->persister->contains($collection, $element); |
| 110 | } |
| 111 | public function containsKey(PersistentCollection $collection, $key) |
| 112 | { |
| 113 | return $this->persister->containsKey($collection, $key); |
| 114 | } |
| 115 | public function count(PersistentCollection $collection) |
| 116 | { |
| 117 | $ownerId = $this->uow->getEntityIdentifier($collection->getOwner()); |
| 118 | $key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $ownerId, $this->filters->getHash()); |
| 119 | $entry = $this->region->get($key); |
| 120 | if ($entry !== null) { |
| 121 | return count($entry->identifiers); |
| 122 | } |
| 123 | return $this->persister->count($collection); |
| 124 | } |
| 125 | public function get(PersistentCollection $collection, $index) |
| 126 | { |
| 127 | return $this->persister->get($collection, $index); |
| 128 | } |
| 129 | public function slice(PersistentCollection $collection, $offset, $length = null) |
| 130 | { |
| 131 | return $this->persister->slice($collection, $offset, $length); |
| 132 | } |
| 133 | public function loadCriteria(PersistentCollection $collection, Criteria $criteria) |
| 134 | { |
| 135 | return $this->persister->loadCriteria($collection, $criteria); |
| 136 | } |
| 137 | protected function evictCollectionCache(PersistentCollection $collection) |
| 138 | { |
| 139 | Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9512', 'The method %s() is deprecated and will be removed without replacement.'); |
| 140 | $key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $this->uow->getEntityIdentifier($collection->getOwner()), $this->filters->getHash()); |
| 141 | $this->region->evict($key); |
| 142 | if ($this->cacheLogger) { |
| 143 | $this->cacheLogger->collectionCachePut($this->regionName, $key); |
| 144 | } |
| 145 | } |
| 146 | protected function evictElementCache($targetEntity, $element) |
| 147 | { |
| 148 | Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9512', 'The method %s() is deprecated and will be removed without replacement.'); |
| 149 | $targetPersister = $this->uow->getEntityPersister($targetEntity); |
| 150 | assert($targetPersister instanceof CachedEntityPersister); |
| 151 | $targetRegion = $targetPersister->getCacheRegion(); |
| 152 | $key = new EntityCacheKey($targetEntity, $this->uow->getEntityIdentifier($element)); |
| 153 | $targetRegion->evict($key); |
| 154 | if ($this->cacheLogger) { |
| 155 | $this->cacheLogger->entityCachePut($targetRegion->getName(), $key); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 |