diff --git a/lib/Doctrine/Common/Annotations/AnnotationRegistry.php b/lib/Doctrine/Common/Annotations/AnnotationRegistry.php index 13abaf50d..ceb7eb7e0 100644 --- a/lib/Doctrine/Common/Annotations/AnnotationRegistry.php +++ b/lib/Doctrine/Common/Annotations/AnnotationRegistry.php @@ -47,22 +47,30 @@ 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 = []; self::$loaders = []; self::$failedToAutoload = []; + self::$registerFileUsed = false; } /** * 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 { + self::$registerFileUsed = true; + require_once $file; } @@ -74,9 +82,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 { @@ -90,9 +96,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 { @@ -105,9 +109,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 { @@ -119,7 +121,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 { @@ -167,6 +169,10 @@ public static function loadAnnotationClass(string $class) : bool } } + if (self::$loaders === [] && self::$autoloadNamespaces === [] && self::$registerFileUsed === false && \class_exists($class)) { + return true; + } + self::$failedToAutoload[$class] = null; return false; 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 @@ +