Skip to content

Commit 8a3f447

Browse files
committed
Merge branch '4.4' into 5.2
* 4.4: Fix deprecations from Doctrine Annotations+Cache
2 parents 3abac5e + 0a947c6 commit 8a3f447

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

Tests/ValidatorBuilderTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Tests;
1313

1414
use Doctrine\Common\Annotations\CachedReader;
15+
use Doctrine\Common\Annotations\PsrCachedReader;
1516
use Doctrine\Common\Annotations\Reader;
1617
use PHPUnit\Framework\TestCase;
1718
use Psr\Cache\CacheItemPoolInterface;
@@ -99,7 +100,11 @@ public function testEnableAnnotationMapping()
99100
$r = new \ReflectionProperty(AnnotationLoader::class, 'reader');
100101
$r->setAccessible(true);
101102

102-
$this->assertInstanceOf(CachedReader::class, $r->getValue($loaders[0]));
103+
if (class_exists(PsrCachedReader::class)) {
104+
$this->assertInstanceOf(PsrCachedReader::class, $r->getValue($loaders[0]));
105+
} else {
106+
$this->assertInstanceOf(CachedReader::class, $r->getValue($loaders[0]));
107+
}
103108
}
104109

105110
public function testEnableAnnotationMappingWithDefaultDoctrineAnnotationReader()
@@ -114,7 +119,11 @@ public function testEnableAnnotationMappingWithDefaultDoctrineAnnotationReader()
114119
$r = new \ReflectionProperty(AnnotationLoader::class, 'reader');
115120
$r->setAccessible(true);
116121

117-
$this->assertInstanceOf(CachedReader::class, $r->getValue($loaders[0]));
122+
if (class_exists(PsrCachedReader::class)) {
123+
$this->assertInstanceOf(PsrCachedReader::class, $r->getValue($loaders[0]));
124+
} else {
125+
$this->assertInstanceOf(CachedReader::class, $r->getValue($loaders[0]));
126+
}
118127
}
119128

120129
/**

ValidatorBuilder.php

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +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;
19+
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
1820
use Psr\Cache\CacheItemPoolInterface;
1921
use Symfony\Component\Cache\Adapter\ArrayAdapter;
20-
use Symfony\Component\Cache\DoctrineProvider;
22+
use Symfony\Component\Cache\DoctrineProvider as SymfonyDoctrineProvider;
2123
use Symfony\Component\Validator\Context\ExecutionContextFactory;
2224
use Symfony\Component\Validator\Exception\ValidatorException;
2325
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
@@ -268,11 +270,7 @@ public function setDoctrineAnnotationReader(?Reader $reader): self
268270
*/
269271
public function addDefaultDoctrineAnnotationReader(): self
270272
{
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();
276274

277275
return $this;
278276
}
@@ -425,4 +423,39 @@ public function getValidator()
425423

426424
return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
427425
}
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+
}
428461
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"symfony/property-info": "^4.4|^5.0",
4242
"symfony/translation": "^4.4|^5.0",
4343
"doctrine/annotations": "^1.10.4",
44-
"doctrine/cache": "~1.0",
44+
"doctrine/cache": "^1.0|^2.0",
4545
"egulias/email-validator": "^2.1.10|^3"
4646
},
4747
"conflict": {

0 commit comments

Comments
 (0)