From 4a71cbba16005666177de3865e16f549ab39ced6 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 19 Dec 2022 19:14:44 +0100 Subject: [PATCH] Support doctrine/annotations 2 --- .github/workflows/continuous-integration.yml | 12 ++++++++++++ .github/workflows/static-analysis.yml | 6 ++++++ composer.json | 4 ++-- lib/Doctrine/ORM/Configuration.php | 14 ++++++++++++-- phpstan-baseline.neon | 5 ----- phpstan.neon | 5 +++++ tests/Doctrine/Tests/ORM/ConfigurationTest.php | 11 +++++++++++ tests/Doctrine/Tests/ORM/Tools/SetupTest.php | 6 +++--- 8 files changed, 51 insertions(+), 12 deletions(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index a33cd450e87..23fc3d7fb0a 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -78,6 +78,9 @@ jobs: run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update" if: "${{ matrix.dbal-version != 'default' }}" + - name: "Uninstall PHPBench" + run: "composer remove --no-update phpbench/phpbench" + - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v2" with: @@ -153,6 +156,9 @@ jobs: run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update" if: "${{ matrix.dbal-version != 'default' }}" + - name: "Uninstall PHPBench" + run: "composer remove --no-update phpbench/phpbench" + - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v2" with: @@ -214,6 +220,9 @@ jobs: run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update" if: "${{ matrix.dbal-version != 'default' }}" + - name: "Uninstall PHPBench" + run: "composer remove --no-update phpbench/phpbench" + - name: "Install PHP" uses: "shivammathur/setup-php@v2" with: @@ -291,6 +300,9 @@ jobs: run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update" if: "${{ matrix.dbal-version != 'default' }}" + - name: "Uninstall PHPBench" + run: "composer remove --no-update phpbench/phpbench" + - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v2" with: diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 2e9263b7a57..60e4d4e53f0 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -57,6 +57,9 @@ jobs: - name: "Require specific persistence version" run: "composer require doctrine/persistence ^$([ ${{ matrix.persistence-version }} = default ] && echo '3.1' || echo ${{ matrix.persistence-version }}) --no-update" + - name: "Uninstall PHPBench" + run: "composer remove --no-update phpbench/phpbench" + - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v2" with: @@ -94,6 +97,9 @@ jobs: - name: "Require specific persistence version" run: "composer require doctrine/persistence ^3.1 --no-update" + - name: "Uninstall PHPBench" + run: "composer remove --no-update phpbench/phpbench" + - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v2" with: diff --git a/composer.json b/composer.json index 28338e419bd..c4272859747 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "symfony/polyfill-php80": "^1.16" }, "require-dev": { - "doctrine/annotations": "^1.13", + "doctrine/annotations": "^1.13 || ^2@dev", "doctrine/coding-standard": "^9.0.2 || ^11.0", "phpbench/phpbench": "^0.16.10 || ^1.0", "phpstan/phpstan": "~1.4.10 || 1.9.4", @@ -52,7 +52,7 @@ "vimeo/psalm": "4.30.0 || 5.3.0" }, "conflict": { - "doctrine/annotations": "<1.13 || >= 2.0" + "doctrine/annotations": "<1.13 || >= 3.0" }, "suggest": { "ext-dom": "Provides support for XSD validation for XML mapping files", diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index e52d5d67413..5d8d62dcc07 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -4,6 +4,7 @@ namespace Doctrine\ORM; +use BadMethodCallException; use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\AnnotationRegistry; use Doctrine\Common\Annotations\CachedReader; @@ -182,9 +183,18 @@ public function newDefaultAnnotationDriver($paths = [], $useSimpleAnnotationRead ); } - AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php'); + if (method_exists(AnnotationRegistry::class, 'registerFile')) { + AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php'); + } if ($useSimpleAnnotationReader) { + if (! class_exists(SimpleAnnotationReader::class)) { + throw new BadMethodCallException( + 'SimpleAnnotationReader has been removed in doctrine/annotations 2.' + . ' Downgrade to version 1 or set $useSimpleAnnotationReader to false.' + ); + } + // Register the ORM Annotations in the AnnotationRegistry $reader = new SimpleAnnotationReader(); $reader->addNamespace('Doctrine\ORM\Mapping'); @@ -192,7 +202,7 @@ public function newDefaultAnnotationDriver($paths = [], $useSimpleAnnotationRead $reader = new AnnotationReader(); } - if (class_exists(ArrayCache::class)) { + if (class_exists(ArrayCache::class) && class_exists(CachedReader::class)) { $reader = new CachedReader($reader, new ArrayCache()); } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 06edcd9b719..e4d6f332302 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -110,11 +110,6 @@ parameters: count: 2 path: lib/Doctrine/ORM/Configuration.php - - - message: "#^Parameter \\#2 \\$cache of class Doctrine\\\\Common\\\\Annotations\\\\CachedReader constructor expects Doctrine\\\\Common\\\\Cache\\\\Cache, Doctrine\\\\Common\\\\Cache\\\\ArrayCache given\\.$#" - count: 1 - path: lib/Doctrine/ORM/Configuration.php - - message: "#^Method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:find\\(\\) invoked with 4 parameters, 2 required\\.$#" count: 1 diff --git a/phpstan.neon b/phpstan.neon index c80418eede6..358b48339ca 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -47,3 +47,8 @@ parameters: - message: '#^Call to method injectObjectManager\(\) on an unknown class Doctrine\\Persistence\\ObjectManagerAware\.$#' path: lib/Doctrine/ORM/UnitOfWork.php + + # Annotations 1 support + - + message: '#^Call to function method_exists\(\) with ''Doctrine\\\\Common\\\\Annotations\\\\AnnotationRegistry'' and ''registerFile'' will always evaluate to false\.$#' + path: lib/Doctrine/ORM/Configuration.php diff --git a/tests/Doctrine/Tests/ORM/ConfigurationTest.php b/tests/Doctrine/Tests/ORM/ConfigurationTest.php index e75721b9cdd..c4656649911 100644 --- a/tests/Doctrine/Tests/ORM/ConfigurationTest.php +++ b/tests/Doctrine/Tests/ORM/ConfigurationTest.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\ORM; +use Doctrine\Common\Annotations\SimpleAnnotationReader; use Doctrine\Common\Cache\ArrayCache; use Doctrine\Common\Cache\Cache; use Doctrine\Common\Cache\Psr6\CacheAdapter; @@ -105,6 +106,16 @@ public function testNewDefaultAnnotationDriver(): void AnnotationNamespace\PrePersist::class ); self::assertInstanceOf(AnnotationNamespace\PrePersist::class, $annotation); + } + + public function testNewDefaultAnnotationDriverWithSimpleAnnotationReader(): void + { + if (! class_exists(SimpleAnnotationReader::class)) { + self::markTestSkipped('Requires doctrine/annotations 1.x'); + } + + $paths = [__DIR__]; + $reflectionClass = new ReflectionClass(ConfigurationTestAnnotationReaderChecker::class); $annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths); $reader = $annotationDriver->getReader(); diff --git a/tests/Doctrine/Tests/ORM/Tools/SetupTest.php b/tests/Doctrine/Tests/ORM/Tools/SetupTest.php index dc5af62c3ef..df5b0141661 100644 --- a/tests/Doctrine/Tests/ORM/Tools/SetupTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/SetupTest.php @@ -61,7 +61,7 @@ public function testDirectoryAutoload(): void public function testAnnotationConfiguration(): void { - $config = Setup::createAnnotationMetadataConfiguration([], true); + $config = Setup::createAnnotationMetadataConfiguration([], true, null, null, false); self::assertInstanceOf(Configuration::class, $config); self::assertEquals(sys_get_temp_dir(), $config->getProxyDir()); @@ -135,7 +135,7 @@ public function testConfiguredCacheNamespaceShouldBeUsedAsPrefixOfGeneratedNames /** @group DDC-1350 */ public function testConfigureProxyDir(): void { - $config = Setup::createAnnotationMetadataConfiguration([], true, '/foo'); + $config = Setup::createAnnotationMetadataConfiguration([], true, '/foo', null, false); self::assertEquals('/foo', $config->getProxyDir()); } @@ -144,7 +144,7 @@ public function testConfigureCache(): void { $adapter = new ArrayAdapter(); $cache = DoctrineProvider::wrap($adapter); - $config = Setup::createAnnotationMetadataConfiguration([], true, null, $cache); + $config = Setup::createAnnotationMetadataConfiguration([], true, null, $cache, false); self::assertSame($adapter, $config->getResultCache()->getCache()->getPool()); self::assertSame($cache, $config->getResultCacheImpl());