8000 [FrameworkBundle][Validator] Fix deprecations from Doctrine Annotations+Cache by derrabus · Pull Request #41230 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Framewor 8000 kBundle][Validator] Fix deprecations from Doctrine Annotations+Cache #41230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"cache/integration-tests": "dev-master",
"composer/package-versions-deprecated": "^1.8",
"doctrine/annotations": "^1.10.4",
"doctrine/cache": "~1.6",
"doctrine/cache": "^1.6|^2.0",
"doctrine/collections": "~1.0",
"doctrine/data-fixtures": "^1.1",
"doctrine/dbal": "^2.6|^3.0",
Expand Down
1 change: 0 additions & 1 deletion src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"symfony/var-dumper": "^3.4|^4.0|^5.0",
"symfony/translation": "^3.4|^4.0|^5.0",
"doctrine/annotations": "^1.10.4",
"doctrine/cache": "~1.6",
"doctrine/collections": "~1.0",
"doctrine/data-fixtures": "^1.1",
"doctrine/dbal": "^2.6|^3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\Common\Annotations\Reader;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
Expand Down Expand Up @@ -61,7 +62,10 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
}

$annotatedClasses = include $annotatedClassPatterns;
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter), $this->debug);
$reader = class_exists(PsrCachedReader::class)
? new PsrCachedReader($this->annotationReader, $arrayAdapter, $this->debug)
: new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter), $this->debug)
;

foreach ($annotatedClasses as $class) {
if (null !== $this->excludeRegexp && preg_match($this->excludeRegexp, $class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\Common\Annotations\Reader;
use Http\Client\HttpClient;
use Psr\Cache\CacheItemPoolInterface;
Expand Down Expand Up @@ -1423,14 +1424,20 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
}

if ('none' !== $config['cache']) {
if (!class_exists(\Doctrine\Common\Cache\CacheProvider::class)) {
if (class_exists(PsrCachedReader::class)) {
$container
->getDefinition('annotations.cached_reader')
->setClass(PsrCachedReader::class)
->replaceArgument(1, new Definition(ArrayAdapter::class))
;
} elseif (!class_exists(\Doctrine\Common\Cache\CacheProvider::class)) {
throw new LogicException('Annotations cannot be enabled as the Doctrine Cache library is not installed.');
}

$cacheService = $config['cache'];

if ('php_array' === $config['cache']) {
$cacheService = 'annotations.cache';
$cacheService = class_exists(PsrCachedReader::class) ? 'annotations.cache_adapter' : 'annotations.cache';

// Enable warmer only if PHP array is used for cache
$definition = $container->findDefinition('annotations.cache_warmer');
Expand All @@ -1447,7 +1454,7 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
->replaceArgument(2, $cacheDir)
;

$cacheService = 'annotations.filesystem_cache';
$cacheService = class_exists(PsrCachedReader::class) ? 'annotations.filesystem_cache_adapter' : 'annotations.filesystem_cache';
}

$container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@
<argument>%kernel.debug%</argument>
</service>

<service id="annotations.cache_adapter" class="Symfony\Component\Cache\Adapter\PhpArrayAdapter">
<factory class="Symfony\Component\Cache\Adapter\PhpArrayAdapter" method="create" />
<argument>%kernel.cache_dir%/annotations.php</argument>
<argument type="service" id="cache.annotations" />
<tag name="container.hot_path" />
</service>

<service id="annotations.cache" class="Symfony\Component\Cache\DoctrineProvider">
<argument type="service">
<service class="Symfony\Component\Cache\Adapter\PhpArrayAdapter">
<factory class="Symfony\Component\Cache\Adapter\PhpArrayAdapter" method="create" />
<argument>%kernel.cache_dir%/annotations.php</argument>
<argument type="service" id="cache.annotations" />
</service>
</argument>
<argument type="service" id="annotations.cache_adapter" />
<tag name="container.hot_path" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\Common\Annotations\Reader;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer;
Expand Down Expand Up @@ -43,10 +44,16 @@ public function testAnnotationsCacheWarmerWithDebugDisabled()
$this->assertFileExists($cacheFile);

// Assert cache is valid
$reader = new CachedReader(
$this->getReadOnlyReader(),
new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter()))
);
$reader = class_exists(PsrCachedReader::class)
? new PsrCachedReader(
$this->getReadOnlyReader(),
new PhpArrayAdapter($cacheFile, new NullAdapter())
)
: new CachedReader(
$this->getReadOnlyReader(),
new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter()))
)
;
$refClass = new \ReflectionClass($this);
$reader->getClassAnnotations($refClass);
$reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__));
Expand All @@ -61,12 +68,21 @@ public function testAnnotationsCacheWarmerWithDebugEnabled()
$warmer = new AnnotationsCacheWarmer($reader, $cacheFile, null, true);
$warmer->warmUp($this->cacheDir);
$this->assertFileExists($cacheFile);

// Assert cache is valid
$reader = new CachedReader(
$this->getReadOnlyReader(),
new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter())),
true
);
$phpArrayAdapter = new PhpArrayAdapter($cacheFile, new NullAdapter());
$reader = class_exists(PsrCachedReader::class)
? new PsrCachedReader(
$this->getReadOnlyReader(),
$phpArrayAdapter,
true
)
: new CachedReader(
$this->getReadOnlyReader(),
new DoctrineProvider($phpArrayAdapter),
true
)
;
$refClass = new \ReflectionClass($this);
$reader->getClassAnnotations($refClass);
$reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;

use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Annotations\PsrCachedReader;
use Psr\Log\LoggerAwareInterface;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
Expand Down Expand Up @@ -999,7 +1000,11 @@ public function testAnnotations()
$container->compile();

$this->assertEquals($container->getParameter('kernel.cache_dir').'/annotations', $container->getDefinition('annotations.filesystem_cache_adapter')->getArgument(2));
$this->assertSame('annotations.filesystem_cache', (string) $container->getDefinition('annotation_reader')->getArgument(1));
if (class_exists(PsrCachedReader::class)) {
$this->assertSame('annotations.filesystem_cache_adapter', (string) $container->getDefinition('annotation_reader')->getArgument(1));
} else {
$this->assertSame('annotations.filesystem_cache', (string) $container->getDefinition('annotation_reader')->getArgument(1));
}
}

public function testFileLinkFormat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\PsrCachedReader;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as FrameworkBundleEngineInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand All @@ -35,7 +36,11 @@ public function testCachedAnnotationReaderAutowiring()
static::bootKernel();

$annotationReader = static::$container->get('test.autowiring_types.autowired_services')->getAnnotationReader();
$this->assertInstanceOf(CachedReader::class, $annotationReader);
if (class_exists(PsrCachedReader::class)) {
$this->assertInstanceOf(PsrCachedReader::class, $annotationReader);
} else {
$this->assertInstanceOf(CachedReader::class, $annotationReader);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": ">=7.1.3",
"ext-xml": "*",
"symfony/cache": "^4.4|^5.0",
"symfony/config": "^4.3.4|^5.0",
"symfony/config": "^4.4.11|~5.0.11|^5.1.3",
"symfony/dependency-injection": "^4.4.1|^5.0.1",
"symfony/error-handler": "^4.4.1|^5.0.1",
"symfony/http-foundation": "^4.4|^5.0",
Expand All @@ -31,7 +31,7 @@
},
"require-dev": {
"doctrine/annotations": "^1.10.4",
"doctrine/cache": "~1.0",
"doctrine/cache": "^1.0|^2.0",
"doctrine/persistence": "^1.3|^2.0",
"paragonie/sodium_compat": "^1.8",
"symfony/asset": "^3.4|^4.0|^5.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"symfony/framework-bundle": "^4.4|^5.0",
"symfony/web-link": "^3.4|^4.0|^5.0",
"doctrine/annotations": "^1.10.4",
"doctrine/cache": "~1.0"
"doctrine/cache": "^1.0|^2.0"
},
"conflict": {
"symfony/dependency-injection": "<4.1",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"require-dev": {
"cache/integration-tests": "dev-master",
"doctrine/cache": "^1.6",
"doctrine/cache": "^1.6|^2.0",
"doctrine/dbal": "^2.6|^3.0",
"predis/predis": "^1.1",
"psr/simple-cache": "^1.0",
Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Component/Serializer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
},
"require-dev": {
"doctrine/annotations": "^1.10.4",
"doctrine/cache": "~1.0",
"phpdocumentor/reflection-docblock": "^3.2|^4.0|^5.0",
"symfony/cache": "^3.4|^4.0|^5.0",
"symfony/config": "^3.4|^4.0|^5.0",
Expand Down Expand Up @@ -49,8 +48,7 @@
"symfony/config": "For using the XML mapping loader.",
"symfony/property-access": "For using the ObjectNormalizer.",
"symfony/http-foundation": "For using a MIME type guesser within the DataUriNormalizer.",
"doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.",
"doctrine/cache": "For using the default cached annotation reader and metadata cache."
"doctrine/annotations": "For using the annotation mapping."
},
"autoload": {
"psr-4": { "Symfony\\Component\\Serializer\\": "" },
Expand Down
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 src/Symfony/Component/Validator/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 src/Symfony/Component/Validator/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