Skip to content

Commit 0a947c6

Browse files
committed
Fix deprecations from Doctrine Annotations+Cache
1 parent 4665151 commit 0a947c6

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

Tests/Mapping/Cache/DoctrineCacheTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Validator\Tests\Mapping\Cache;
1313

1414
use Doctrine\Common\Cache\ArrayCache;
15+
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
16+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1517
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache;
1618

1719
/**
@@ -21,6 +23,9 @@ class DoctrineCacheTest extends AbstractCacheTest
2123
{
2224
protected function setUp(): void
2325
{
24-
$this->cache = new DoctrineCache(new ArrayCache());
26+
$this->cache = class_exists(DoctrineProvider::class)
27+
? new DoctrineCache(DoctrineProvider::wrap(new ArrayAdapter()))
28+
: new DoctrineCache(new ArrayCache())
29+
;
2530
}
2631
}

ValidatorBuilder.php

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use Doctrine\Common\Annotations\CachedReader;
16+
use Doctrine\Common\Annotations\PsrCachedReader;
1617
use Doctrine\Common\Annotations\Reader;
1718
use Doctrine\Common\Cache\ArrayCache;
18-
use Doctrine\Common\Cache\CacheProvider;
19+
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
1920
use Psr\Cache\CacheItemPoolInterface;
2021
use Symfony\Component\Cache\Adapter\ArrayAdapter;
21-
use Symfony\Component\Cache\DoctrineProvider;
22+
use Symfony\Component\Cache\DoctrineProvider as SymfonyDoctrineProvider;
2223
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
2324
use Symfony\Component\Validator\Context\ExecutionContextFactory;
2425
use Symfony\Component\Validator\Exception\LogicException;
@@ -199,19 +200,7 @@ public function enableAnnotationMapping(Reader $annotationReader = null)
199200
throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.');
200201
}
201202

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();
215204

216205
return $this;
217206
}
@@ -386,4 +375,39 @@ public function getValidator()
386375

387376
return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
388377
}
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+
}
389413
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"symfony/property-info": "^3.4|^4.0|^5.0",
3737
"symfony/translation": "^4.2",
3838
"doctrine/annotations": "^1.10.4",
39-
"doctrine/cache": "~1.0",
39+
"doctrine/cache": "^1.0|^2.0",
4040
"egulias/email-validator": "^2.1.10|^3"
4141
},
4242
"conflict": {

0 commit comments

Comments
 (0)