diff --git a/Tests/Mapping/Cache/DoctrineCacheTest.php b/Tests/Mapping/Cache/DoctrineCacheTest.php index e73b0d996..ec9b0920f 100644 --- a/Tests/Mapping/Cache/DoctrineCacheTest.php +++ b/Tests/Mapping/Cache/DoctrineCacheTest.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Validator\Tests\Mapping\Cache; use Doctrine\Common\Cache\ArrayCache; +use Doctrine\Common\Cache\Psr6\DoctrineProvider; +use Symfony\Component\Cache\Adapter\ArrayAdapter; use Symfony\Component\Validator\Mapping\Cache\DoctrineCache; /** @@ -21,6 +23,9 @@ class DoctrineCacheTest extends AbstractCacheTest { protected function setUp(): void { - $this->cache = new DoctrineCache(new ArrayCache()); + $this->cache = class_exists(DoctrineProvider::class) + ? new DoctrineCache(DoctrineProvider::wrap(new ArrayAdapter())) + : new DoctrineCache(new ArrayCache()) + ; } } diff --git a/ValidatorBuilder.php b/ValidatorBuilder.php index 292a55c98..e3fe807ff 100644 --- a/ValidatorBuilder.php +++ b/ValidatorBuilder.php @@ -13,12 +13,13 @@ use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\CachedReader; +use Doctrine\Common\Annotations\PsrCachedReader; use Doctrine\Common\Annotations\Reader; use Doctrine\Common\Cache\ArrayCache; -use Doctrine\Common\Cache\CacheProvider; +use Doctrine\Common\Cache\Psr6\DoctrineProvider; use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Cache\Adapter\ArrayAdapter; -use Symfony\Component\Cache\DoctrineProvider; +use Symfony\Component\Cache\DoctrineProvider as SymfonyDoctrineProvider; use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; use Symfony\Component\Validator\Context\ExecutionContextFactory; use Symfony\Component\Validator\Exception\LogicException; @@ -199,19 +200,7 @@ public function enableAnnotationMapping(Reader $annotationReader = null) throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.'); } - if (null === $annotationReader) { - if (!class_exists(AnnotationReader::class) || !class_exists(CacheProvider::class)) { - throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and doctrine/cache to be installed.'); - } - - if (class_exists(ArrayAdapter::class)) { - $annotationReader = new CachedReader(new AnnotationReader(), new DoctrineProvider(new ArrayAdapter())); - } else { - $annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache()); - } - } - - $this->annotationReader = $annotationReader; + $this->annotationReader = $annotationReader ?? $this->createAnnotationReader(); return $this; } @@ -386,4 +375,39 @@ public function getValidator() return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers); } + + private function createAnnotationReader(): Reader + { + if (!class_exists(AnnotationReader::class)) { + throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and symfony/cache to be installed.'); + } + + // Doctrine Annotation >= 1.13, Symfony Cache + if (class_exists(PsrCachedReader::class) && class_exists(ArrayAdapter::class)) { + return new PsrCachedReader(new AnnotationReader(), new ArrayAdapter()); + } + + // Doctrine Annotations < 1.13, Doctrine Cache >= 1.11, Symfony Cache + if (class_exists(CachedReader::class) && class_exists(DoctrineProvider::class) && class_exists(ArrayAdapter::class)) { + return new CachedReader(new AnnotationReader(), DoctrineProvider::wrap(new ArrayAdapter())); + } + + // Doctrine Annotations < 1.13, Doctrine Cache < 1.11, Symfony Cache + if (class_exists(CachedReader::class) && !class_exists(DoctrineProvider::class) && class_exists(ArrayAdapter::class)) { + return new CachedReader(new AnnotationReader(), new SymfonyDoctrineProvider(new ArrayAdapter())); + } + + // Doctrine Annotations < 1.13, Doctrine Cache < 1.11 + if (class_exists(CachedReader::class) && class_exists(ArrayCache::class)) { + return new CachedReader(new AnnotationReader(), new ArrayCache()); + } + + // Doctrine Annotation >= 1.13, Doctrine Cache >= 2, no Symfony Cache + if (class_exists(PsrCachedReader::class)) { + throw new LogicException('Enabling annotation based constraint mapping requires the package symfony/cache to be installed.'); + } + + // Doctrine Annotation (<1.13 || >2), no Doctrine Cache, no Symfony Cache + throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations (>=1.13) and symfony/cache to be installed.'); + } } diff --git a/composer.json b/composer.json index c9cbe689c..e32a72bcf 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "symfony/property-info": "^3.4|^4.0|^5.0", "symfony/translation": "^4.2", "doctrine/annotations": "^1.10.4", - "doctrine/cache": "~1.0", + "doctrine/cache": "^1.0|^2.0", "egulias/email-validator": "^2.1.10|^3" }, "conflict": {