|
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;
|
| 19 | +use Doctrine\Common\Cache\Psr6\DoctrineProvider; |
18 | 20 | use Psr\Cache\CacheItemPoolInterface;
|
19 | 21 | use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
20 |
| -use Symfony\Component\Cache\DoctrineProvider; |
| 22 | +use Symfony\Component\Cache\DoctrineProvider as SymfonyDoctrineProvider; |
21 | 23 | use Symfony\Component\Validator\Context\ExecutionContextFactory;
|
22 | 24 | use Symfony\Component\Validator\Exception\ValidatorException;
|
23 | 25 | use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
|
@@ -268,11 +270,7 @@ public function setDoctrineAnnotationReader(?Reader $reader): self
|
268 | 270 | */
|
269 | 271 | public function addDefaultDoctrineAnnotationReader(): self
|
270 | 272 | {
|
271 |
| - if (class_exists(ArrayAdapter::class)) { |
272 |
| - $this->annotationReader = new CachedReader(new AnnotationReader(), new DoctrineProvider(new ArrayAdapter())); |
273 |
| - } else { |
274 |
| - $this->annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache()); |
275 |
| - } |
| 273 | + $this->annotationReader = $this->createAnnotationReader(); |
276 | 274 |
|
277 | 275 | return $this;
|
278 | 276 | }
|
@@ -425,4 +423,39 @@ public function getValidator()
|
425 | 423 |
|
426 | 424 | return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
|
427 | 425 | }
|
| 426 | + |
| 427 | + private function createAnnotationReader(): Reader |
| 428 | + { |
| 429 | + if (!class_exists(AnnotationReader::class)) { |
| 430 | + throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and symfony/cache to be installed.'); |
| 431 | + } |
| 432 | + |
| 433 | + // Doctrine Annotation >= 1.13, Symfony Cache |
| 434 | + if (class_exists(PsrCachedReader::class) && class_exists(ArrayAdapter::class)) { |
| 435 | + return new PsrCachedReader(new AnnotationReader(), new ArrayAdapter()); |
| 436 | + } |
| 437 | + |
| 438 | + // Doctrine Annotations < 1.13, Doctrine Cache >= 1.11, Symfony Cache |
| 439 | + if (class_exists(CachedReader::class) && class_exists(DoctrineProvider::class) && class_exists(ArrayAdapter::class)) { |
| 440 | + return new CachedReader(new AnnotationReader(), DoctrineProvider::wrap(new ArrayAdapter())); |
| 441 | + } |
| 442 | + |
| 443 | + // Doctrine Annotations < 1.13, Doctrine Cache < 1.11, Symfony Cache |
| 444 | + if (class_exists(CachedReader::class) && !class_exists(DoctrineProvider::class) && class_exists(ArrayAdapter::class)) { |
| 445 | + return new CachedReader(new AnnotationReader(), new SymfonyDoctrineProvider(new ArrayAdapter())); |
| 446 | + } |
| 447 | + |
| 448 | + // Doctrine Annotations < 1.13, Doctrine Cache < 1.11 |
| 449 | + if (class_exists(CachedReader::class) && class_exists(ArrayCache::class)) { |
| 450 | + return new CachedReader(new AnnotationReader(), new ArrayCache()); |
| 451 | + } |
| 452 | + |
| 453 | + // Doctrine Annotation >= 1.13, Doctrine Cache >= 2, no Symfony Cache |
| 454 | + if (class_exists(PsrCachedReader::class)) { |
| 455 | + throw new LogicException('Enabling annotation based constraint mapping requires the package symfony/cache to be installed.'); |
| 456 | + } |
| 457 | + |
| 458 | + // Doctrine Annotation (<1.13 || >2), no Doctrine Cache, no Symfony Cache |
| 459 | + throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations (>=1.13) and symfony/cache to be installed.'); |
| 460 | + } |
428 | 461 | }
|
0 commit comments