Skip to content

Commit

Permalink
Support doctrine/annotations 2
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Dec 19, 2022
1 parent 82a4063 commit 4a71cbb
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 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 --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 --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 --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 --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 --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 --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@dev",
"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
14 changes: 12 additions & 2 deletions 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\AnnotationRegistry;
use Doctrine\Common\Annotations\CachedReader;
Expand Down Expand Up @@ -182,17 +183,26 @@ 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');
} 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.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 4a71cbb

Please sign in to comment.