Skip to content

Commit

Permalink
Support doctrine/annotations 2 (#10320)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Dec 19, 2022
1 parent c4835cc commit f82485e
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 13 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 --dev --no-update phpbench/phpbench"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"
with:
Expand Down Expand Up @@ -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 --dev --no-update phpbench/phpbench"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"
with:
Expand Down Expand Up @@ -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 --dev --no-update phpbench/phpbench"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand Down Expand Up @@ -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 --dev --no-update phpbench/phpbench"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"
with:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 --dev --no-update phpbench/phpbench"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"
with:
Expand Down Expand Up @@ -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 --dev --no-update phpbench/phpbench"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v2"
with:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"symfony/polyfill-php80": "^1.16"
},
"require-dev": {
"doctrine/annotations": "^1.13",
"doctrine/annotations": "^1.13 || ^2",
"doctrine/coding-standard": "^9.0.2 || ^11.0",
"phpbench/phpbench": "^0.16.10 || ^1.0",
"phpstan/phpstan": "~1.4.10 || 1.9.4",
Expand All @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion lib/Doctrine/ORM/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ORM;

use BadMethodCallException;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\SimpleAnnotationReader;
Expand Down Expand Up @@ -182,14 +183,21 @@ public function newDefaultAnnotationDriver($paths = [], $useSimpleAnnotationRead
}

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');
} else {
$reader = new AnnotationReader();
}

if (class_exists(ArrayCache::class)) {
if (class_exists(ArrayCache::class) && class_exists(CachedReader::class)) {
$reader = new CachedReader($reader, new ArrayCache());
}

Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions phpstan-persistence2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ parameters:

# Symfony cache supports passing a key prefix to the clear method.
- '/^Method Psr\\Cache\\CacheItemPoolInterface\:\:clear\(\) invoked with 1 parameter, 0 required\.$/'

# Cache 1 compatibility
-
message: '~^Parameter #2 \$cache of class Doctrine\\Common\\Annotations\\CachedReader constructor expects Doctrine\\Common\\Cache\\Cache, Doctrine\\Common\\Cache\\ArrayCache given\.~'
path: lib/Doctrine/ORM/Configuration.php
3 changes: 1 addition & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@
<code>new CachedReader($reader, new ArrayCache())</code>
<code>new SimpleAnnotationReader()</code>
</DeprecatedClass>
<DeprecatedMethod occurrences="3">
<code>AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php')</code>
<DeprecatedMethod occurrences="2">
<code>getMetadataCacheImpl</code>
<code>getQueryCacheImpl</code>
</DeprecatedMethod>
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@

<!-- Persistence 2 compatibility -->
<referencedClass name="Doctrine\Persistence\ObjectManagerAware"/>

<!-- Annotations 1 compatibility -->
<referencedClass name="Doctrine\Common\Annotations\CachedReader"/>
</errorLevel>
</UndefinedClass>
<UndefinedDocblockClass>
Expand Down
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/ORM/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions tests/Doctrine/Tests/ORM/Tools/SetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}

Expand All @@ -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());
Expand Down

0 comments on commit f82485e

Please sign in to comment.