From c499bb9823ddc853170a9e41adbeacae7f0c536f Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Fri, 28 Jun 2019 10:44:36 +0900 Subject: [PATCH 1/5] Trigger the standard autoloader as the last resort As v2 isn't here yet, and looking at #232 it is unclear if will, and as we have to put up with annoying deprecated functions, here I propose to fall back to the standard autoloader if nothing else worked, and if there's no custom loader defined. Fixes #270 --- lib/Doctrine/Common/Annotations/AnnotationRegistry.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/Doctrine/Common/Annotations/AnnotationRegistry.php b/lib/Doctrine/Common/Annotations/AnnotationRegistry.php index 13abaf50d..e26a2117e 100644 --- a/lib/Doctrine/Common/Annotations/AnnotationRegistry.php +++ b/lib/Doctrine/Common/Annotations/AnnotationRegistry.php @@ -166,6 +166,10 @@ public static function loadAnnotationClass(string $class) : bool return true; } } + + if (self::$loaders === [] && self::$autoloadNamespaces === [] && \class_exists($class)) { + return true; + } self::$failedToAutoload[$class] = null; From 6c78d9afd9f6dfea5a2431928cd8bc58e38722db Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Thu, 2 Apr 2020 00:48:49 +0900 Subject: [PATCH 2/5] Do not use standard autoloader with registerFile() --- .../Common/Annotations/AnnotationRegistry.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/Common/Annotations/AnnotationRegistry.php b/lib/Doctrine/Common/Annotations/AnnotationRegistry.php index e26a2117e..be8353196 100644 --- a/lib/Doctrine/Common/Annotations/AnnotationRegistry.php +++ b/lib/Doctrine/Common/Annotations/AnnotationRegistry.php @@ -47,6 +47,13 @@ final class AnnotationRegistry */ static private $failedToAutoload = []; + /** + * Whenever registerFile() was used. Disables use of standard autoloader. + * + * @var bool + */ + static private $registerFileUsed = false; + public static function reset() : void { self::$autoloadNamespaces = []; @@ -63,6 +70,8 @@ public static function reset() : void */ public static function registerFile(string $file) : void { + self::$registerFileUsed = true; + require_once $file; } @@ -166,8 +175,8 @@ public static function loadAnnotationClass(string $class) : bool return true; } } - - if (self::$loaders === [] && self::$autoloadNamespaces === [] && \class_exists($class)) { + + if (self::$loaders === [] && self::$autoloadNamespaces === [] && self::$registerFileUsed === false && \class_exists($class)) { return true; } From 5755c8b3267247c3839db638a756c3e63b94d182 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 2 Apr 2020 07:56:24 +0200 Subject: [PATCH 3/5] Add tests for fallback autoloading --- .../Annotations/AnnotationRegistryTest.php | 25 +++++++++++++++++++ .../Fixtures/Annotation/CanBeAutoLoaded.php | 8 ++++++ .../Annotation/LoadedUsingRegisterFile.php | 8 ++++++ .../Annotation/ShouldNeverBeLoaded.php | 8 ++++++ 4 files changed, 49 insertions(+) create mode 100644 tests/Doctrine/Tests/Common/Annotations/Fixtures/Annotation/CanBeAutoLoaded.php create mode 100644 tests/Doctrine/Tests/Common/Annotations/Fixtures/Annotation/LoadedUsingRegisterFile.php create mode 100644 tests/Doctrine/Tests/Common/Annotations/Fixtures/Annotation/ShouldNeverBeLoaded.php diff --git a/tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php b/tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php index 4ee86442a..bbb64a53f 100644 --- a/tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php +++ b/tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php @@ -3,6 +3,9 @@ namespace Doctrine\Tests\Common\Annotations; use Doctrine\Common\Annotations\AnnotationRegistry; +use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\CanBeAutoLoaded; +use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\LoadedUsingRegisterFile; +use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\ShouldNeverBeLoaded; use PHPUnit\Framework\TestCase; class AnnotationRegistryTest extends TestCase @@ -183,4 +186,26 @@ public function testRegisterLoaderIfNotExistsOnlyRegisteresSameLoaderOnce() : vo AnnotationRegistry::loadAnnotationClass($className); AnnotationRegistry::loadAnnotationClass($className); } + + /** + * @runInSeparateProcess + */ + public function testClassExistsFallback() : void + { + AnnotationRegistry::reset(); + + self::assertTrue(AnnotationRegistry::loadAnnotationClass(CanBeAutoLoaded::class)); + } + + /** + * @runInSeparateProcess + */ + public function testClassExistsFallbackNotUsedWhenRegisterFileUsed() : void + { + AnnotationRegistry::reset(); + AnnotationRegistry::registerFile(__DIR__ . '/Fixtures/Annotation/LoadedUsingRegisterFile.php'); + + self::assertTrue(AnnotationRegistry::loadAnnotationClass(LoadedUsingRegisterFile::class)); + self::assertFalse(AnnotationRegistry::loadAnnotationClass(ShouldNeverBeLoaded::class)); + } } diff --git a/tests/Doctrine/Tests/Common/Annotations/Fixtures/Annotation/CanBeAutoLoaded.php b/tests/Doctrine/Tests/Common/Annotations/Fixtures/Annotation/CanBeAutoLoaded.php new file mode 100644 index 000000000..2867a903f --- /dev/null +++ b/tests/Doctrine/Tests/Common/Annotations/Fixtures/Annotation/CanBeAutoLoaded.php @@ -0,0 +1,8 @@ + Date: Thu, 2 Apr 2020 07:59:22 +0200 Subject: [PATCH 4/5] Update deprecation messages --- .../Common/Annotations/AnnotationRegistry.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/Doctrine/Common/Annotations/AnnotationRegistry.php b/lib/Doctrine/Common/Annotations/AnnotationRegistry.php index be8353196..341b911e9 100644 --- a/lib/Doctrine/Common/Annotations/AnnotationRegistry.php +++ b/lib/Doctrine/Common/Annotations/AnnotationRegistry.php @@ -64,9 +64,7 @@ public static function reset() : void /** * Registers file. * - * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0 - * autoloading should be deferred to the globally registered autoloader by then. For now, - * use @example AnnotationRegistry::registerLoader('class_exists') + * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. */ public static function registerFile(string $file) : void { @@ -83,9 +81,7 @@ public static function registerFile(string $file) : void * @param string $namespace * @param string|array|null $dirs * - * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0 - * autoloading should be deferred to the globally registered autoloader by then. For now, - * use @example AnnotationRegistry::registerLoader('class_exists') + * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. */ public static function registerAutoloadNamespace(string $namespace, $dirs = null) : void { @@ -99,9 +95,7 @@ public static function registerAutoloadNamespace(string $namespace, $dirs = null * * @param string[][]|string[]|null[] $namespaces indexed by namespace name * - * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0 - * autoloading should be deferred to the globally registered autoloader by then. For now, - * use @example AnnotationRegistry::registerLoader('class_exists') + * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. */ public static function registerAutoloadNamespaces(array $namespaces) : void { @@ -114,9 +108,7 @@ public static function registerAutoloadNamespaces(array $namespaces) : void * NOTE: These class loaders HAVE to be silent when a class was not found! * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class. * - * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0 - * autoloading should be deferred to the globally registered autoloader by then. For now, - * use @example AnnotationRegistry::registerLoader('class_exists') + * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. */ public static function registerLoader(callable $callable) : void { @@ -128,7 +120,7 @@ public static function registerLoader(callable $callable) : void /** * Registers an autoloading callable for annotations, if it is not already registered * - * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0 + * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. */ public static function registerUniqueLoader(callable $callable) : void { From e43aa90b83375e3b8a701c187fb922c8eec6d827 Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Thu, 2 Apr 2020 17:06:38 +0900 Subject: [PATCH 5/5] Update AnnotationRegistry.php --- lib/Doctrine/Common/Annotations/AnnotationRegistry.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Doctrine/Common/Annotations/AnnotationRegistry.php b/lib/Doctrine/Common/Annotations/AnnotationRegistry.php index 341b911e9..ceb7eb7e0 100644 --- a/lib/Doctrine/Common/Annotations/AnnotationRegistry.php +++ b/lib/Doctrine/Common/Annotations/AnnotationRegistry.php @@ -59,6 +59,7 @@ public static function reset() : void self::$autoloadNamespaces = []; self::$loaders = []; self::$failedToAutoload = []; + self::$registerFileUsed = false; } /**