diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Cache/DefaultCacheFactory.php')
| -rw-r--r-- | vendor/doctrine/orm/src/Cache/DefaultCacheFactory.php | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Cache/DefaultCacheFactory.php b/vendor/doctrine/orm/src/Cache/DefaultCacheFactory.php new file mode 100644 index 0000000..84ea490 --- /dev/null +++ b/vendor/doctrine/orm/src/Cache/DefaultCacheFactory.php | |||
| @@ -0,0 +1,189 @@ | |||
| 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\Collection\CachedCollectionPersister; | ||
| 9 | use Doctrine\ORM\Cache\Persister\Collection\NonStrictReadWriteCachedCollectionPersister; | ||
| 10 | use Doctrine\ORM\Cache\Persister\Collection\ReadOnlyCachedCollectionPersister; | ||
| 11 | use Doctrine\ORM\Cache\Persister\Collection\ReadWriteCachedCollectionPersister; | ||
| 12 | use Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister; | ||
| 13 | use Doctrine\ORM\Cache\Persister\Entity\NonStrictReadWriteCachedEntityPersister; | ||
| 14 | use Doctrine\ORM\Cache\Persister\Entity\ReadOnlyCachedEntityPersister; | ||
| 15 | use Doctrine\ORM\Cache\Persister\Entity\ReadWriteCachedEntityPersister; | ||
| 16 | use Doctrine\ORM\Cache\Region\DefaultRegion; | ||
| 17 | use Doctrine\ORM\Cache\Region\FileLockRegion; | ||
| 18 | use Doctrine\ORM\Cache\Region\UpdateTimestampCache; | ||
| 19 | use Doctrine\ORM\EntityManagerInterface; | ||
| 20 | use Doctrine\ORM\Mapping\AssociationMapping; | ||
| 21 | use Doctrine\ORM\Mapping\ClassMetadata; | ||
| 22 | use Doctrine\ORM\Persisters\Collection\CollectionPersister; | ||
| 23 | use Doctrine\ORM\Persisters\Entity\EntityPersister; | ||
| 24 | use InvalidArgumentException; | ||
| 25 | use LogicException; | ||
| 26 | use Psr\Cache\CacheItemPoolInterface; | ||
| 27 | |||
| 28 | use function assert; | ||
| 29 | use function sprintf; | ||
| 30 | |||
| 31 | use const DIRECTORY_SEPARATOR; | ||
| 32 | |||
| 33 | class DefaultCacheFactory implements CacheFactory | ||
| 34 | { | ||
| 35 | private TimestampRegion|null $timestampRegion = null; | ||
| 36 | |||
| 37 | /** @var Region[] */ | ||
| 38 | private array $regions = []; | ||
| 39 | |||
| 40 | private string|null $fileLockRegionDirectory = null; | ||
| 41 | |||
| 42 | public function __construct(private readonly RegionsConfiguration $regionsConfig, private readonly CacheItemPoolInterface $cacheItemPool) | ||
| 43 | { | ||
| 44 | } | ||
| 45 | |||
| 46 | public function setFileLockRegionDirectory(string $fileLockRegionDirectory): void | ||
| 47 | { | ||
| 48 | $this->fileLockRegionDirectory = $fileLockRegionDirectory; | ||
| 49 | } | ||
| 50 | |||
| 51 | public function getFileLockRegionDirectory(): string|null | ||
| 52 | { | ||
| 53 | return $this->fileLockRegionDirectory; | ||
| 54 | } | ||
| 55 | |||
| 56 | public function setRegion(Region $region): void | ||
| 57 | { | ||
| 58 | $this->regions[$region->getName()] = $region; | ||
| 59 | } | ||
| 60 | |||
| 61 | public function setTimestampRegion(TimestampRegion $region): void | ||
| 62 | { | ||
| 63 | $this->timestampRegion = $region; | ||
| 64 | } | ||
| 65 | |||
| 66 | public function buildCachedEntityPersister(EntityManagerInterface $em, EntityPersister $persister, ClassMetadata $metadata): CachedEntityPersister | ||
| 67 | { | ||
| 68 | assert($metadata->cache !== null); | ||
| 69 | $region = $this->getRegion($metadata->cache); | ||
| 70 | $usage = $metadata->cache['usage']; | ||
| 71 | |||
| 72 | if ($usage === ClassMetadata::CACHE_USAGE_READ_ONLY) { | ||
| 73 | return new ReadOnlyCachedEntityPersister($persister, $region, $em, $metadata); | ||
| 74 | } | ||
| 75 | |||
| 76 | if ($usage === ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE) { | ||
| 77 | return new NonStrictReadWriteCachedEntityPersister($persister, $region, $em, $metadata); | ||
| 78 | } | ||
| 79 | |||
| 80 | if ($usage === ClassMetadata::CACHE_USAGE_READ_WRITE) { | ||
| 81 | if (! $region instanceof ConcurrentRegion) { | ||
| 82 | throw new InvalidArgumentException(sprintf('Unable to use access strategy type of [%s] without a ConcurrentRegion', $usage)); | ||
| 83 | } | ||
| 84 | |||
| 85 | return new ReadWriteCachedEntityPersister($persister, $region, $em, $metadata); | ||
| 86 | } | ||
| 87 | |||
| 88 | throw new InvalidArgumentException(sprintf('Unrecognized access strategy type [%s]', $usage)); | ||
| 89 | } | ||
| 90 | |||
| 91 | public function buildCachedCollectionPersister( | ||
| 92 | EntityManagerInterface $em, | ||
| 93 | CollectionPersister $persister, | ||
| 94 | AssociationMapping $mapping, | ||
| 95 | ): CachedCollectionPersister { | ||
| 96 | assert(isset($mapping->cache)); | ||
| 97 | $usage = $mapping->cache['usage']; | ||
| 98 | $region = $this->getRegion($mapping->cache); | ||
| 99 | |||
| 100 | if ($usage === ClassMetadata::CACHE_USAGE_READ_ONLY) { | ||
| 101 | return new ReadOnlyCachedCollectionPersister($persister, $region, $em, $mapping); | ||
| 102 | } | ||
| 103 | |||
| 104 | if ($usage === ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE) { | ||
| 105 | return new NonStrictReadWriteCachedCollectionPersister($persister, $region, $em, $mapping); | ||
| 106 | } | ||
| 107 | |||
| 108 | if ($usage === ClassMetadata::CACHE_USAGE_READ_WRITE) { | ||
| 109 | if (! $region instanceof ConcurrentRegion) { | ||
| 110 | throw new InvalidArgumentException(sprintf('Unable to use access strategy type of [%s] without a ConcurrentRegion', $usage)); | ||
| 111 | } | ||
| 112 | |||
| 113 | return new ReadWriteCachedCollectionPersister($persister, $region, $em, $mapping); | ||
| 114 | } | ||
| 115 | |||
| 116 | throw new InvalidArgumentException(sprintf('Unrecognized access strategy type [%s]', $usage)); | ||
| 117 | } | ||
| 118 | |||
| 119 | public function buildQueryCache(EntityManagerInterface $em, string|null $regionName = null): QueryCache | ||
| 120 | { | ||
| 121 | return new DefaultQueryCache( | ||
| 122 | $em, | ||
| 123 | $this->getRegion( | ||
| 124 | [ | ||
| 125 | 'region' => $regionName ?: Cache::DEFAULT_QUERY_REGION_NAME, | ||
| 126 | 'usage' => ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE, | ||
| 127 | ], | ||
| 128 | ), | ||
| 129 | ); | ||
| 130 | } | ||
| 131 | |||
| 132 | public function buildCollectionHydrator(EntityManagerInterface $em, AssociationMapping $mapping): CollectionHydrator | ||
| 133 | { | ||
| 134 | return new DefaultCollectionHydrator($em); | ||
| 135 | } | ||
| 136 | |||
| 137 | public function buildEntityHydrator(EntityManagerInterface $em, ClassMetadata $metadata): EntityHydrator | ||
| 138 | { | ||
| 139 | return new DefaultEntityHydrator($em); | ||
| 140 | } | ||
| 141 | |||
| 142 | /** | ||
| 143 | * {@inheritDoc} | ||
| 144 | */ | ||
| 145 | public function getRegion(array $cache): Region | ||
| 146 | { | ||
| 147 | if (isset($this->regions[$cache['region']])) { | ||
| 148 | return $this->regions[$cache['region']]; | ||
| 149 | } | ||
| 150 | |||
| 151 | $name = $cache['region']; | ||
| 152 | $lifetime = $this->regionsConfig->getLifetime($cache['region']); | ||
| 153 | $region = new DefaultRegion($name, $this->cacheItemPool, $lifetime); | ||
| 154 | |||
| 155 | if ($cache['usage'] === ClassMetadata::CACHE_USAGE_READ_WRITE) { | ||
| 156 | if ( | ||
| 157 | $this->fileLockRegionDirectory === '' || | ||
| 158 | $this->fileLockRegionDirectory === null | ||
| 159 | ) { | ||
| 160 | throw new LogicException( | ||
| 161 | 'If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" is required, ' . | ||
| 162 | 'The default implementation provided by doctrine is "Doctrine\ORM\Cache\Region\FileLockRegion" if you want to use it please provide a valid directory, DefaultCacheFactory#setFileLockRegionDirectory(). ', | ||
| 163 | ); | ||
| 164 | } | ||
| 165 | |||
| 166 | $directory = $this->fileLockRegionDirectory . DIRECTORY_SEPARATOR . $cache['region']; | ||
| 167 | $region = new FileLockRegion($region, $directory, (string) $this->regionsConfig->getLockLifetime($cache['region'])); | ||
| 168 | } | ||
| 169 | |||
| 170 | return $this->regions[$cache['region']] = $region; | ||
| 171 | } | ||
| 172 | |||
| 173 | public function getTimestampRegion(): TimestampRegion | ||
| 174 | { | ||
| 175 | if ($this->timestampRegion === null) { | ||
| 176 | $name = Cache::DEFAULT_TIMESTAMP_REGION_NAME; | ||
| 177 | $lifetime = $this->regionsConfig->getLifetime($name); | ||
| 178 | |||
| 179 | $this->timestampRegion = new UpdateTimestampCache($name, $this->cacheItemPool, $lifetime); | ||
| 180 | } | ||
| 181 | |||
| 182 | return $this->timestampRegion; | ||
| 183 | } | ||
| 184 | |||
| 185 | public function createCache(EntityManagerInterface $entityManager): Cache | ||
| 186 | { | ||
| 187 | return new DefaultCache($entityManager); | ||
| 188 | } | ||
| 189 | } | ||
