1 | <?php |
||
46 | class DefaultCacheFactory implements CacheFactory |
||
47 | { |
||
48 | /** |
||
49 | * @var CacheAdapter |
||
50 | */ |
||
51 | private $cache; |
||
52 | |||
53 | /** |
||
54 | * @var \Doctrine\ORM\Cache\RegionsConfiguration |
||
55 | */ |
||
56 | private $regionsConfig; |
||
57 | |||
58 | /** |
||
59 | * @var \Doctrine\ORM\Cache\TimestampRegion|null |
||
60 | */ |
||
61 | private $timestampRegion; |
||
62 | |||
63 | /** |
||
64 | * @var \Doctrine\ORM\Cache\Region[] |
||
65 | */ |
||
66 | private $regions = []; |
||
67 | |||
68 | /** |
||
69 | * @var string|null |
||
70 | */ |
||
71 | private $fileLockRegionDirectory; |
||
72 | |||
73 | /** |
||
74 | * @param RegionsConfiguration $cacheConfig |
||
75 | * @param CacheAdapter $cache |
||
76 | */ |
||
77 | 285 | public function __construct(RegionsConfiguration $cacheConfig, CacheAdapter $cache) |
|
78 | { |
||
79 | 285 | $this->cache = $cache; |
|
80 | 285 | $this->regionsConfig = $cacheConfig; |
|
81 | 285 | } |
|
82 | |||
83 | /** |
||
84 | * @param string $fileLockRegionDirectory |
||
85 | */ |
||
86 | 1 | public function setFileLockRegionDirectory($fileLockRegionDirectory) |
|
87 | { |
||
88 | 1 | $this->fileLockRegionDirectory = (string) $fileLockRegionDirectory; |
|
89 | 1 | } |
|
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getFileLockRegionDirectory() |
||
95 | { |
||
96 | return $this->fileLockRegionDirectory; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param \Doctrine\ORM\Cache\Region $region |
||
101 | */ |
||
102 | public function setRegion(Region $region) |
||
103 | { |
||
104 | $this->regions[$region->getName()] = $region; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param \Doctrine\ORM\Cache\TimestampRegion $region |
||
109 | */ |
||
110 | public function setTimestampRegion(TimestampRegion $region) |
||
111 | { |
||
112 | $this->timestampRegion = $region; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 131 | public function buildCachedEntityPersister(EntityManagerInterface $em, EntityPersister $persister, ClassMetadata $metadata) |
|
119 | { |
||
120 | 131 | $region = $this->getRegion($metadata->cache); |
|
121 | 131 | $usage = $metadata->cache['usage']; |
|
122 | |||
123 | 131 | if ($usage === ClassMetadata::CACHE_USAGE_READ_ONLY) { |
|
124 | 120 | return new ReadOnlyCachedEntityPersister($persister, $region, $em, $metadata); |
|
125 | } |
||
126 | |||
127 | 94 | if ($usage === ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE) { |
|
128 | 92 | return new NonStrictReadWriteCachedEntityPersister($persister, $region, $em, $metadata); |
|
129 | } |
||
130 | |||
131 | 2 | if ($usage === ClassMetadata::CACHE_USAGE_READ_WRITE) { |
|
132 | 1 | return new ReadWriteCachedEntityPersister($persister, $region, $em, $metadata); |
|
|
|||
133 | } |
||
134 | |||
135 | 1 | throw new \InvalidArgumentException(sprintf("Unrecognized access strategy type [%s]", $usage)); |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 81 | public function buildCachedCollectionPersister(EntityManagerInterface $em, CollectionPersister $persister, array $mapping) |
|
142 | { |
||
143 | 81 | $usage = $mapping['cache']['usage']; |
|
144 | 81 | $region = $this->getRegion($mapping['cache']); |
|
145 | |||
146 | 81 | if ($usage === ClassMetadata::CACHE_USAGE_READ_ONLY) { |
|
147 | 59 | return new ReadOnlyCachedCollectionPersister($persister, $region, $em, $mapping); |
|
148 | } |
||
149 | |||
150 | 80 | if ($usage === ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE) { |
|
151 | 78 | return new NonStrictReadWriteCachedCollectionPersister($persister, $region, $em, $mapping); |
|
152 | } |
||
153 | |||
154 | 2 | if ($usage === ClassMetadata::CACHE_USAGE_READ_WRITE) { |
|
155 | 1 | return new ReadWriteCachedCollectionPersister($persister, $region, $em, $mapping); |
|
156 | } |
||
157 | |||
158 | 1 | throw new \InvalidArgumentException(sprintf("Unrecognized access strategy type [%s]", $usage)); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | 64 | public function buildQueryCache(EntityManagerInterface $em, $regionName = null) |
|
165 | { |
||
166 | 64 | return new DefaultQueryCache( |
|
167 | 64 | $em, |
|
168 | 64 | $this->getRegion( |
|
169 | [ |
||
170 | 64 | 'region' => $regionName ?: Cache::DEFAULT_QUERY_REGION_NAME, |
|
171 | 64 | 'usage' => ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE |
|
172 | ] |
||
173 | ) |
||
174 | ); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | 117 | public function buildCollectionHydrator(EntityManagerInterface $em, array $mapping) |
|
181 | { |
||
182 | 117 | return new DefaultCollectionHydrator($em); |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | 208 | public function buildEntityHydrator(EntityManagerInterface $em, ClassMetadata $metadata) |
|
189 | { |
||
190 | 208 | return new DefaultEntityHydrator($em); |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | 134 | public function getRegion(array $cache) |
|
197 | { |
||
198 | 134 | if (isset($this->regions[$cache['region']])) { |
|
199 | 26 | return $this->regions[$cache['region']]; |
|
200 | } |
||
201 | |||
202 | 134 | $name = $cache['region']; |
|
203 | 134 | $cacheAdapter = $this->createRegionCache($name); |
|
204 | 134 | $lifetime = $this->regionsConfig->getLifetime($cache['region']); |
|
205 | |||
206 | 134 | $region = ($cacheAdapter instanceof MultiGetCache) |
|
207 | 133 | ? new DefaultMultiGetRegion($name, $cacheAdapter, $lifetime) |
|
208 | 134 | : new DefaultRegion($name, $cacheAdapter, $lifetime); |
|
209 | |||
210 | 134 | if ($cache['usage'] === ClassMetadata::CACHE_USAGE_READ_WRITE) { |
|
211 | |||
212 | if ( |
||
213 | 2 | '' === $this->fileLockRegionDirectory || |
|
214 | 2 | null === $this->fileLockRegionDirectory |
|
215 | ) { |
||
216 | 2 | throw new \LogicException( |
|
217 | 'If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" is required, ' . |
||
218 | 2 | '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(). ' |
|
219 | ); |
||
220 | } |
||
221 | |||
222 | $directory = $this->fileLockRegionDirectory . DIRECTORY_SEPARATOR . $cache['region']; |
||
223 | $region = new FileLockRegion($region, $directory, $this->regionsConfig->getLockLifetime($cache['region'])); |
||
224 | } |
||
225 | |||
226 | 132 | return $this->regions[$cache['region']] = $region; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * @param string $name |
||
231 | * |
||
232 | * @return CacheAdapter |
||
233 | */ |
||
234 | 134 | private function createRegionCache($name) |
|
235 | { |
||
236 | 134 | $cacheAdapter = clone $this->cache; |
|
237 | |||
238 | 134 | if (!$cacheAdapter instanceof CacheProvider) { |
|
239 | 1 | return $cacheAdapter; |
|
240 | } |
||
241 | |||
242 | 133 | $namespace = $cacheAdapter->getNamespace(); |
|
243 | |||
244 | 133 | if ('' !== $namespace) { |
|
245 | 1 | $namespace .= ':'; |
|
246 | } |
||
247 | |||
248 | 133 | $cacheAdapter->setNamespace($namespace . $name); |
|
249 | |||
250 | 133 | return $cacheAdapter; |
|
251 | } |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | 222 | public function getTimestampRegion() |
|
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | 285 | public function createCache(EntityManagerInterface $em) |
|
275 | } |
||
276 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.