|
13 | 13 |
|
14 | 14 | use Doctrine\Common\Annotations\AnnotationReader; |
15 | 15 | use Doctrine\Common\Annotations\CachedReader; |
| 16 | +use Doctrine\Common\Annotations\PsrCachedReader; |
16 | 17 | use Doctrine\Common\Annotations\Reader; |
17 | 18 | use Doctrine\Common\Cache\ArrayCache; |
18 | | -use Doctrine\Common\Cache\CacheProvider; |
| 19 | +use Doctrine\Common\Cache\Psr6\DoctrineProvider; |
19 | 20 | use Psr\Cache\CacheItemPoolInterface; |
20 | 21 | use Symfony\Component\Cache\Adapter\ArrayAdapter; |
21 | | -use Symfony\Component\Cache\DoctrineProvider; |
| 22 | +use Symfony\Component\Cache\DoctrineProvider as SymfonyDoctrineProvider; |
22 | 23 | use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; |
23 | 24 | use Symfony\Component\Validator\Context\ExecutionContextFactory; |
24 | 25 | use Symfony\Component\Validator\Exception\LogicException; |
@@ -199,19 +200,7 @@ public function enableAnnotationMapping(Reader $annotationReader = null) |
199 | 200 | throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.'); |
200 | 201 | } |
201 | 202 |
|
202 | | - if (null === $annotationReader) { |
203 | | - if (!class_exists(AnnotationReader::class) || !class_exists(CacheProvider::class)) { |
204 | | - throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and doctrine/cache to be installed.'); |
205 | | - } |
206 | | - |
207 | | - if (class_exists(ArrayAdapter::class)) { |
208 | | - $annotationReader = new CachedReader(new AnnotationReader(), new DoctrineProvider(new ArrayAdapter())); |
209 | | - } else { |
210 | | - $annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache()); |
211 | | - } |
212 | | - } |
213 | | - |
214 | | - $this->annotationReader = $annotationReader; |
| 203 | + $this->annotationReader = $annotationReader ?? $this->createAnnotationReader(); |
215 | 204 |
|
216 | 205 | return $this; |
217 | 206 | } |
@@ -386,4 +375,39 @@ public function getValidator() |
386 | 375 |
|
387 | 376 | return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers); |
388 | 377 | } |
| 378 | + |
| 379 | + private function createAnnotationReader(): Reader |
| 380 | + { |
| 381 | + if (!class_exists(AnnotationReader::class)) { |
| 382 | + throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and symfony/cache to be installed.'); |
| 383 | + } |
| 384 | + |
| 385 | + // Doctrine Annotation >= 1.13, Symfony Cache |
| 386 | + if (class_exists(PsrCachedReader::class) && class_exists(ArrayAdapter::class)) { |
| 387 | + return new PsrCachedReader(new AnnotationReader(), new ArrayAdapter()); |
| 388 | + } |
| 389 | + |
| 390 | + // Doctrine Annotations < 1.13, Doctrine Cache >= 1.11, Symfony Cache |
| 391 | + if (class_exists(CachedReader::class) && class_exists(DoctrineProvider::class) && class_exists(ArrayAdapter::class)) { |
| 392 | + return new CachedReader(new AnnotationReader(), DoctrineProvider::wrap(new ArrayAdapter())); |
| 393 | + } |
| 394 | + |
| 395 | + // Doctrine Annotations < 1.13, Doctrine Cache < 1.11, Symfony Cache |
| 396 | + if (class_exists(CachedReader::class) && !class_exists(DoctrineProvider::class) && class_exists(ArrayAdapter::class)) { |
| 397 | + return new CachedReader(new AnnotationReader(), new SymfonyDoctrineProvider(new ArrayAdapter())); |
| 398 | + } |
| 399 | + |
| 400 | + // Doctrine Annotations < 1.13, Doctrine Cache < 1.11 |
| 401 | + if (class_exists(CachedReader::class) && class_exists(ArrayCache::class)) { |
| 402 | + return new CachedReader(new AnnotationReader(), new ArrayCache()); |
| 403 | + } |
| 404 | + |
| 405 | + // Doctrine Annotation >= 1.13, Doctrine Cache >= 2, no Symfony Cache |
| 406 | + if (class_exists(PsrCachedReader::class)) { |
| 407 | + throw new LogicException('Enabling annotation based constraint mapping requires the package symfony/cache to be installed.'); |
| 408 | + } |
| 409 | + |
| 410 | + // Doctrine Annotation (<1.13 || >2), no Doctrine Cache, no Symfony Cache |
| 411 | + throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations (>=1.13) and symfony/cache to be installed.'); |
| 412 | + } |
389 | 413 | } |
0 commit comments