diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Cache/DefaultCache.php')
| -rw-r--r-- | vendor/doctrine/orm/src/Cache/DefaultCache.php | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Cache/DefaultCache.php b/vendor/doctrine/orm/src/Cache/DefaultCache.php new file mode 100644 index 0000000..685181c --- /dev/null +++ b/vendor/doctrine/orm/src/Cache/DefaultCache.php | |||
| @@ -0,0 +1,245 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\ORM\Cache; | ||
| 6 | |||
| 7 | use Doctrine\ORM\Cache; | ||
| 8 | use Doctrine\ORM\Cache\Persister\CachedPersister; | ||
| 9 | use Doctrine\ORM\EntityManagerInterface; | ||
| 10 | use Doctrine\ORM\Mapping\ClassMetadata; | ||
| 11 | use Doctrine\ORM\ORMInvalidArgumentException; | ||
| 12 | use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; | ||
| 13 | use Doctrine\ORM\UnitOfWork; | ||
| 14 | |||
| 15 | use function is_array; | ||
| 16 | use function is_object; | ||
| 17 | |||
| 18 | /** | ||
| 19 | * Provides an API for querying/managing the second level cache regions. | ||
| 20 | */ | ||
| 21 | class DefaultCache implements Cache | ||
| 22 | { | ||
| 23 | private readonly UnitOfWork $uow; | ||
| 24 | private readonly CacheFactory $cacheFactory; | ||
| 25 | |||
| 26 | /** | ||
| 27 | * @var QueryCache[] | ||
| 28 | * @psalm-var array<string, QueryCache> | ||
| 29 | */ | ||
| 30 | private array $queryCaches = []; | ||
| 31 | |||
| 32 | private QueryCache|null $defaultQueryCache = null; | ||
| 33 | |||
| 34 | public function __construct( | ||
| 35 | private readonly EntityManagerInterface $em, | ||
| 36 | ) { | ||
| 37 | $this->uow = $em->getUnitOfWork(); | ||
| 38 | $this->cacheFactory = $em->getConfiguration() | ||
| 39 | ->getSecondLevelCacheConfiguration() | ||
| 40 | ->getCacheFactory(); | ||
| 41 | } | ||
| 42 | |||
| 43 | public function getEntityCacheRegion(string $className): Region|null | ||
| 44 | { | ||
| 45 | $metadata = $this->em->getClassMetadata($className); | ||
| 46 | $persister = $this->uow->getEntityPersister($metadata->rootEntityName); | ||
| 47 | |||
| 48 | if (! ($persister instanceof CachedPersister)) { | ||
| 49 | return null; | ||
| 50 | } | ||
| 51 | |||
| 52 | return $persister->getCacheRegion(); | ||
| 53 | } | ||
| 54 | |||
| 55 | public function getCollectionCacheRegion(string $className, string $association): Region|null | ||
| 56 | { | ||
| 57 | $metadata = $this->em->getClassMetadata($className); | ||
| 58 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); | ||
| 59 | |||
| 60 | if (! ($persister instanceof CachedPersister)) { | ||
| 61 | return null; | ||
| 62 | } | ||
| 63 | |||
| 64 | return $persister->getCacheRegion(); | ||
| 65 | } | ||
| 66 | |||
| 67 | public function containsEntity(string $className, mixed $identifier): bool | ||
| 68 | { | ||
| 69 | $metadata = $this->em->getClassMetadata($className); | ||
| 70 | $persister = $this->uow->getEntityPersister($metadata->rootEntityName); | ||
| 71 | |||
| 72 | if (! ($persister instanceof CachedPersister)) { | ||
| 73 | return false; | ||
| 74 | } | ||
| 75 | |||
| 76 | return $persister->getCacheRegion()->contains($this->buildEntityCacheKey($metadata, $identifier)); | ||
| 77 | } | ||
| 78 | |||
| 79 | public function evictEntity(string $className, mixed $identifier): void | ||
| 80 | { | ||
| 81 | $metadata = $this->em->getClassMetadata($className); | ||
| 82 | $persister = $this->uow->getEntityPersister($metadata->rootEntityName); | ||
| 83 | |||
| 84 | if (! ($persister instanceof CachedPersister)) { | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | |||
| 88 | $persister->getCacheRegion()->evict($this->buildEntityCacheKey($metadata, $identifier)); | ||
| 89 | } | ||
| 90 | |||
| 91 | public function evictEntityRegion(string $className): void | ||
| 92 | { | ||
| 93 | $metadata = $this->em->getClassMetadata($className); | ||
| 94 | $persister = $this->uow->getEntityPersister($metadata->rootEntityName); | ||
| 95 | |||
| 96 | if (! ($persister instanceof CachedPersister)) { | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | |||
| 100 | $persister->getCacheRegion()->evictAll(); | ||
| 101 | } | ||
| 102 | |||
| 103 | public function evictEntityRegions(): void | ||
| 104 | { | ||
| 105 | $metadatas = $this->em->getMetadataFactory()->getAllMetadata(); | ||
| 106 | |||
| 107 | foreach ($metadatas as $metadata) { | ||
| 108 | $persister = $this->uow->getEntityPersister($metadata->rootEntityName); | ||
| 109 | |||
| 110 | if (! ($persister instanceof CachedPersister)) { | ||
| 111 | continue; | ||
| 112 | } | ||
| 113 | |||
| 114 | $persister->getCacheRegion()->evictAll(); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | public function containsCollection(string $className, string $association, mixed $ownerIdentifier): bool | ||
| 119 | { | ||
| 120 | $metadata = $this->em->getClassMetadata($className); | ||
| 121 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); | ||
| 122 | |||
| 123 | if (! ($persister instanceof CachedPersister)) { | ||
| 124 | return false; | ||
| 125 | } | ||
| 126 | |||
| 127 | return $persister->getCacheRegion()->contains($this->buildCollectionCacheKey($metadata, $association, $ownerIdentifier)); | ||
| 128 | } | ||
| 129 | |||
| 130 | public function evictCollection(string $className, string $association, mixed $ownerIdentifier): void | ||
| 131 | { | ||
| 132 | $metadata = $this->em->getClassMetadata($className); | ||
| 133 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); | ||
| 134 | |||
| 135 | if (! ($persister instanceof CachedPersister)) { | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | $persister->getCacheRegion()->evict($this->buildCollectionCacheKey($metadata, $association, $ownerIdentifier)); | ||
| 140 | } | ||
| 141 | |||
| 142 | public function evictCollectionRegion(string $className, string $association): void | ||
| 143 | { | ||
| 144 | $metadata = $this->em->getClassMetadata($className); | ||
| 145 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); | ||
| 146 | |||
| 147 | if (! ($persister instanceof CachedPersister)) { | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | $persister->getCacheRegion()->evictAll(); | ||
| 152 | } | ||
| 153 | |||
| 154 | public function evictCollectionRegions(): void | ||
| 155 | { | ||
| 156 | $metadatas = $this->em->getMetadataFactory()->getAllMetadata(); | ||
| 157 | |||
| 158 | foreach ($metadatas as $metadata) { | ||
| 159 | foreach ($metadata->associationMappings as $association) { | ||
| 160 | if (! $association->isToMany()) { | ||
| 161 | continue; | ||
| 162 | } | ||
| 163 | |||
| 164 | $persister = $this->uow->getCollectionPersister($association); | ||
| 165 | |||
| 166 | if (! ($persister instanceof CachedPersister)) { | ||
| 167 | continue; | ||
| 168 | } | ||
| 169 | |||
| 170 | $persister->getCacheRegion()->evictAll(); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | public function containsQuery(string $regionName): bool | ||
| 176 | { | ||
| 177 | return isset($this->queryCaches[$regionName]); | ||
| 178 | } | ||
| 179 | |||
| 180 | public function evictQueryRegion(string|null $regionName = null): void | ||
| 181 | { | ||
| 182 | if ($regionName === null && $this->defaultQueryCache !== null) { | ||
| 183 | $this->defaultQueryCache->clear(); | ||
| 184 | |||
| 185 | return; | ||
| 186 | } | ||
| 187 | |||
| 188 | if (isset($this->queryCaches[$regionName])) { | ||
| 189 | $this->queryCaches[$regionName]->clear(); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | public function evictQueryRegions(): void | ||
| 194 | { | ||
| 195 | $this->getQueryCache()->clear(); | ||
| 196 | |||
| 197 | foreach ($this->queryCaches as $queryCache) { | ||
| 198 | $queryCache->clear(); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | public function getQueryCache(string|null $regionName = null): QueryCache | ||
| 203 | { | ||
| 204 | if ($regionName === null) { | ||
| 205 | return $this->defaultQueryCache ??= $this->cacheFactory->buildQueryCache($this->em); | ||
| 206 | } | ||
| 207 | |||
| 208 | return $this->queryCaches[$regionName] ??= $this->cacheFactory->buildQueryCache($this->em, $regionName); | ||
| 209 | } | ||
| 210 | |||
| 211 | private function buildEntityCacheKey(ClassMetadata $metadata, mixed $identifier): EntityCacheKey | ||
| 212 | { | ||
| 213 | if (! is_array($identifier)) { | ||
| 214 | $identifier = $this->toIdentifierArray($metadata, $identifier); | ||
| 215 | } | ||
| 216 | |||
| 217 | return new EntityCacheKey($metadata->rootEntityName, $identifier); | ||
| 218 | } | ||
| 219 | |||
| 220 | private function buildCollectionCacheKey( | ||
| 221 | ClassMetadata $metadata, | ||
| 222 | string $association, | ||
| 223 | mixed $ownerIdentifier, | ||
| 224 | ): CollectionCacheKey { | ||
| 225 | if (! is_array($ownerIdentifier)) { | ||
| 226 | $ownerIdentifier = $this->toIdentifierArray($metadata, $ownerIdentifier); | ||
| 227 | } | ||
| 228 | |||
| 229 | return new CollectionCacheKey($metadata->rootEntityName, $association, $ownerIdentifier); | ||
| 230 | } | ||
| 231 | |||
| 232 | /** @return array<string, mixed> */ | ||
| 233 | private function toIdentifierArray(ClassMetadata $metadata, mixed $identifier): array | ||
| 234 | { | ||
| 235 | if (is_object($identifier)) { | ||
| 236 | $class = DefaultProxyClassNameResolver::getClass($identifier); | ||
| 237 | if ($this->em->getMetadataFactory()->hasMetadataFor($class)) { | ||
| 238 | $identifier = $this->uow->getSingleIdentifierValue($identifier) | ||
| 239 | ?? throw ORMInvalidArgumentException::invalidIdentifierBindingEntity($class); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | return [$metadata->identifier[0] => $identifier]; | ||
| 244 | } | ||
| 245 | } | ||
