Skip to content

Commit 233e366

Browse files
sanmaialcaeus
andauthored
Trigger the standard autoloader as the last resort (#271)
* 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 * Do not use standard autoloader with registerFile() * Add tests for fallback autoloading * Update deprecation messages * Update AnnotationRegistry.php Co-authored-by: Andreas Braun <[email protected]>
1 parent 3d31fc1 commit 233e366

File tree

5 files changed

+68
-13
lines changed

5 files changed

+68
-13
lines changed

lib/Doctrine/Common/Annotations/AnnotationRegistry.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,30 @@ final class AnnotationRegistry
4747
*/
4848
static private $failedToAutoload = [];
4949

50+
/**
51+
* Whenever registerFile() was used. Disables use of standard autoloader.
52+
*
53+
* @var bool
54+
*/
55+
static private $registerFileUsed = false;
56+
5057
public static function reset() : void
5158
{
5259
self::$autoloadNamespaces = [];
5360
self::$loaders = [];
5461
self::$failedToAutoload = [];
62+
self::$registerFileUsed = false;
5563
}
5664

5765
/**
5866
* Registers file.
5967
*
60-
* @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
61-
* autoloading should be deferred to the globally registered autoloader by then. For now,
62-
* use @example AnnotationRegistry::registerLoader('class_exists')
68+
* @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
6369
*/
6470
public static function registerFile(string $file) : void
6571
{
72+
self::$registerFileUsed = true;
73+
6674
require_once $file;
6775
}
6876

@@ -74,9 +82,7 @@ public static function registerFile(string $file) : void
7482
* @param string $namespace
7583
* @param string|array|null $dirs
7684
*
77-
* @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
78-
* autoloading should be deferred to the globally registered autoloader by then. For now,
79-
* use @example AnnotationRegistry::registerLoader('class_exists')
85+
* @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
8086
*/
8187
public static function registerAutoloadNamespace(string $namespace, $dirs = null) : void
8288
{
@@ -90,9 +96,7 @@ public static function registerAutoloadNamespace(string $namespace, $dirs = null
9096
*
9197
* @param string[][]|string[]|null[] $namespaces indexed by namespace name
9298
*
93-
* @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
94-
* autoloading should be deferred to the globally registered autoloader by then. For now,
95-
* use @example AnnotationRegistry::registerLoader('class_exists')
99+
* @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
96100
*/
97101
public static function registerAutoloadNamespaces(array $namespaces) : void
98102
{
@@ -105,9 +109,7 @@ public static function registerAutoloadNamespaces(array $namespaces) : void
105109
* NOTE: These class loaders HAVE to be silent when a class was not found!
106110
* IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
107111
*
108-
* @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
109-
* autoloading should be deferred to the globally registered autoloader by then. For now,
110-
* use @example AnnotationRegistry::registerLoader('class_exists')
112+
* @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
111113
*/
112114
public static function registerLoader(callable $callable) : void
113115
{
@@ -119,7 +121,7 @@ public static function registerLoader(callable $callable) : void
119121
/**
120122
* Registers an autoloading callable for annotations, if it is not already registered
121123
*
122-
* @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
124+
* @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
123125
*/
124126
public static function registerUniqueLoader(callable $callable) : void
125127
{
@@ -167,6 +169,10 @@ public static function loadAnnotationClass(string $class) : bool
167169
}
168170
}
169171

172+
if (self::$loaders === [] && self::$autoloadNamespaces === [] && self::$registerFileUsed === false && \class_exists($class)) {
173+
return true;
174+
}
175+
170176
self::$failedToAutoload[$class] = null;
171177

172178
return false;

tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Doctrine\Tests\Common\Annotations;
44

55
use Doctrine\Common\Annotations\AnnotationRegistry;
6+
use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\CanBeAutoLoaded;
7+
use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\LoadedUsingRegisterFile;
8+
use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\ShouldNeverBeLoaded;
69
use PHPUnit\Framework\TestCase;
710

811
class AnnotationRegistryTest extends TestCase
@@ -183,4 +186,26 @@ public function testRegisterLoaderIfNotExistsOnlyRegisteresSameLoaderOnce() : vo
183186
AnnotationRegistry::loadAnnotationClass($className);
184187
AnnotationRegistry::loadAnnotationClass($className);
185188
}
189+
190+
/**
191+
* @runInSeparateProcess
192+
*/
193+
public function testClassExistsFallback() : void
194+
{
195+
AnnotationRegistry::reset();
196+
197+
self::assertTrue(AnnotationRegistry::loadAnnotationClass(CanBeAutoLoaded::class));
198+
}
199+
200+
/**
201+
* @runInSeparateProcess
202+
*/
203+
public function testClassExistsFallbackNotUsedWhenRegisterFileUsed() : void
204+
{
205+
AnnotationRegistry::reset();
206+
AnnotationRegistry::registerFile(__DIR__ . '/Fixtures/Annotation/LoadedUsingRegisterFile.php');
207+
208+
self::assertTrue(AnnotationRegistry::loadAnnotationClass(LoadedUsingRegisterFile::class));
209+
self::assertFalse(AnnotationRegistry::loadAnnotationClass(ShouldNeverBeLoaded::class));
210+
}
186211
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\Common\Annotations\Fixtures\Annotation;
4+
5+
/** @Annotation */
6+
class CanBeAutoLoaded
7+
{
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\Common\Annotations\Fixtures\Annotation;
4+
5+
/** @Annotation */
6+
class LoadedUsingRegisterFile
7+
{
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\Common\Annotations\Fixtures\Annotation;
4+
5+
/** @Annotation */
6+
class ShouldNeverBeLoaded
7+
{
8+
}

0 commit comments

Comments
 (0)