Skip to content

Commit

Permalink
Fix deprecations from Doctrine Annotations+Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed May 16, 2021
1 parent 4665151 commit 0a947c6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
7 changes: 6 additions & 1 deletion Tests/Mapping/Cache/DoctrineCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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())
;
}
}
54 changes: 39 additions & 15 deletions ValidatorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.');
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 0a947c6

Please sign in to comment.