From 9211cbc34af36f02435458ff1bd1ae4e6dfd5e8d Mon Sep 17 00:00:00 2001 From: DerManoMann Date: Wed, 4 Jan 2023 08:49:14 +1300 Subject: [PATCH] Allow and support doctrine/annotations ^2.0 --- composer.json | 2 +- src/Generator.php | 3 ++- tests/Analysers/DocBlockParserTest.php | 5 +++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index a68b5f4e6..d5e09024d 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,7 @@ "require": { "php": ">=7.2", "ext-json": "*", - "doctrine/annotations": "^1.7", + "doctrine/annotations": "^1.7 || ^2.0", "psr/log": "^1.1 || ^2.0 || ^3.0", "symfony/deprecation-contracts": "^2 || ^3", "symfony/finder": ">=2.2", diff --git a/src/Generator.php b/src/Generator.php index fbfc7b8df..1fdee5b7f 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -80,13 +80,14 @@ public function __construct(?LoggerInterface $logger = null) $this->setNamespaces(self::DEFAULT_NAMESPACES); // kinda config stack to stay BC... + // @deprecated Can be removed once doctrine/annotations 2.0 becomes mandatory $this->configStack = new class() { protected $generator; public function push(Generator $generator): void { $this->generator = $generator; - if (class_exists(AnnotationRegistry::class, true)) { + if (class_exists(AnnotationRegistry::class, true) && method_exists(AnnotationRegistry::class, 'registerLoader')) { // keeping track of &this->generator allows to 'disable' the loader after we are done; // no unload, unfortunately :/ $gref = &$this->generator; diff --git a/tests/Analysers/DocBlockParserTest.php b/tests/Analysers/DocBlockParserTest.php index ba5c94926..1413446d0 100644 --- a/tests/Analysers/DocBlockParserTest.php +++ b/tests/Analysers/DocBlockParserTest.php @@ -6,6 +6,7 @@ namespace OpenApi\Tests\Analysers; +use Doctrine\Common\Annotations\AnnotationRegistry; use OpenApi\Tests\OpenApiTestCase; class DocBlockParserTest extends OpenApiTestCase @@ -23,6 +24,10 @@ public function testParseContents(): void public function testDeprecatedAnnotationWarning(): void { + if (!class_exists(AnnotationRegistry::class, true) || !method_exists(AnnotationRegistry::class, 'registerLoader')) { + $this->markTestSkipped('Not supported in doctrine/annotations v2'); + } + $this->assertOpenApiLogEntryContains('The annotation @SWG\Definition() is deprecated.'); $this->annotationsFromDocBlockParser('@SWG\Definition()', self::SWG_ALIAS); }