From ec51c21a9dccc222d09a2464d850ea2894f336b5 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Fri, 14 May 2021 22:01:37 +0200 Subject: [PATCH] Fix deprecations from Doctrine Annotations+Cache --- composer.json | 2 +- src/Symfony/Bridge/Doctrine/composer.json | 1 - .../CacheWarmer/AnnotationsCacheWarmer.php | 6 ++- .../FrameworkExtension.php | 13 +++-- .../Resources/config/annotations.xml | 15 +++--- .../AnnotationsCacheWarmerTest.php | 34 ++++++++---- .../FrameworkExtensionTest.php | 7 ++- .../Tests/Functional/AutowiringTypesTest.php | 7 ++- .../Bundle/FrameworkBundle/composer.json | 4 +- src/Symfony/Bundle/TwigBundle/composer.json | 2 +- src/Symfony/Component/Cache/composer.json | 2 +- .../Component/Serializer/composer.json | 4 +- .../Tests/Mapping/Cache/DoctrineCacheTest.php | 7 ++- .../Component/Validator/ValidatorBuilder.php | 54 +++++++++++++------ src/Symfony/Component/Validator/composer.json | 2 +- 15 files changed, 112 insertions(+), 48 deletions(-) diff --git a/composer.json b/composer.json index 737f767885..39a35f30f1 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Symfony/Bridge/Doctrine/composer.json b/src/Symfony/Bridge/Doctrine/composer.json index 68fbcef8c7..17a83e7d12 100644 --- a/src/Symfony/Bridge/Doctrine/composer.json +++ b/src/Symfony/Bridge/Doctrine/composer.json @@ -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", diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php index 7dd50aa31a..8ed3f618f9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php @@ -13,6 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\CacheWarmer; 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; @@ -61,7 +62,10 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer } $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)) { diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index b6fad07ad9..1e0ee70ccb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -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; @@ -1423,14 +1424,20 @@ class FrameworkExtension extends Extension } 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'); @@ -1447,7 +1454,7 @@ class FrameworkExtension extends Extension ->replaceArgument(2, $cacheDir) ; - $cacheService = 'annotations.filesystem_cache'; + $cacheService = class_exists(PsrCachedReader::class) ? 'annotations.filesystem_cache_adapter' : 'annotations.filesystem_cache'; } $container diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml index 2e56f1deb6..4420dfbf00 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml @@ -50,14 +50,15 @@ %kernel.debug% + + + %kernel.cache_dir%/annotations.php + + + + - - - - %kernel.cache_dir%/annotations.php - - - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php index 9b257a2bf1..9e353ad7bb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php @@ -4,6 +4,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer; 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; @@ -43,10 +44,16 @@ class AnnotationsCacheWarmerTest extends TestCase $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__)); @@ -61,12 +68,21 @@ class AnnotationsCacheWarmerTest extends TestCase $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__)); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 35977510bb..396a04e8b4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -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; @@ -999,7 +1000,11 @@ abstract class FrameworkExtensionTest extends TestCase $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() diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/AutowiringTypesTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/AutowiringTypesTest.php index f6149ea874..fdeaf98fb0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/AutowiringTypesTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/AutowiringTypesTest.php @@ -13,6 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; 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; @@ -35,7 +36,11 @@ class AutowiringTypesTest extends AbstractWebTestCase 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); + } } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index f26298324f..efa69d2be3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -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", @@ -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", diff --git a/src/Symfony/Bundle/TwigBundle/composer.json b/src/Symfony/Bundle/TwigBundle/composer.json index 57cf455107..0178473326 100644 --- a/src/Symfony/Bundle/TwigBundle/composer.json +++ b/src/Symfony/Bundle/TwigBundle/composer.json @@ -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", diff --git a/src/Symfony/Component/Cache/composer.json b/src/Symfony/Component/Cache/composer.json index 87a26071a6..209d758eed 100644 --- a/src/Symfony/Component/Cache/composer.json +++ b/src/Symfony/Component/Cache/composer.json @@ -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", diff --git a/src/Symfony/Component/Serializer/composer.json b/src/Symfony/Component/Serializer/composer.json index 2cf3920ac8..075896e478 100644 --- a/src/Symfony/Component/Serializer/composer.json +++ b/src/Symfony/Component/Serializer/composer.json @@ -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", @@ -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\\": "" }, diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php index e73b0d9966..ec9b0920f6 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php +++ b/src/Symfony/Component/Validator/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/src/Symfony/Component/Validator/ValidatorBuilder.php b/src/Symfony/Component/Validator/ValidatorBuilder.php index 292a55c985..e3fe807ff9 100644 --- a/src/Symfony/Component/Validator/ValidatorBuilder.php +++ b/src/Symfony/Component/Validator/ValidatorBuilder.php @@ -13,12 +13,13 @@ namespace Symfony\Component\Validator; 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 @@ class ValidatorBuilder implements ValidatorBuilderInterface 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 @@ class ValidatorBuilder implements ValidatorBuilderInterface 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/src/Symfony/Component/Validator/composer.json b/src/Symfony/Component/Validator/composer.json index c9cbe689ce..e32a72bcf9 100644 --- a/src/Symfony/Component/Validator/composer.json +++ b/src/Symfony/Component/Validator/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": {