summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Cache/DefaultCacheFactory.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/orm/src/Cache/DefaultCacheFactory.php')
-rw-r--r--vendor/doctrine/orm/src/Cache/DefaultCacheFactory.php189
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
3declare(strict_types=1);
4
5namespace Doctrine\ORM\Cache;
6
7use Doctrine\ORM\Cache;
8use Doctrine\ORM\Cache\Persister\Collection\CachedCollectionPersister;
9use Doctrine\ORM\Cache\Persister\Collection\NonStrictReadWriteCachedCollectionPersister;
10use Doctrine\ORM\Cache\Persister\Collection\ReadOnlyCachedCollectionPersister;
11use Doctrine\ORM\Cache\Persister\Collection\ReadWriteCachedCollectionPersister;
12use Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister;
13use Doctrine\ORM\Cache\Persister\Entity\NonStrictReadWriteCachedEntityPersister;
14use Doctrine\ORM\Cache\Persister\Entity\ReadOnlyCachedEntityPersister;
15use Doctrine\ORM\Cache\Persister\Entity\ReadWriteCachedEntityPersister;
16use Doctrine\ORM\Cache\Region\DefaultRegion;
17use Doctrine\ORM\Cache\Region\FileLockRegion;
18use Doctrine\ORM\Cache\Region\UpdateTimestampCache;
19use Doctrine\ORM\EntityManagerInterface;
20use Doctrine\ORM\Mapping\AssociationMapping;
21use Doctrine\ORM\Mapping\ClassMetadata;
22use Doctrine\ORM\Persisters\Collection\CollectionPersister;
23use Doctrine\ORM\Persisters\Entity\EntityPersister;
24use InvalidArgumentException;
25use LogicException;
26use Psr\Cache\CacheItemPoolInterface;
27
28use function assert;
29use function sprintf;
30
31use const DIRECTORY_SEPARATOR;
32
33class 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}