Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger the standard autoloader as the last resort #271

Merged
merged 5 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions lib/Doctrine/Common/Annotations/AnnotationRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Doctrine\Tests\Common\Annotations\Fixtures\Annotation;

/** @Annotation */
class CanBeAutoLoaded
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Doctrine\Tests\Common\Annotations\Fixtures\Annotation;

/** @Annotation */
class LoadedUsingRegisterFile
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Doctrine\Tests\Common\Annotations\Fixtures\Annotation;

/** @Annotation */
class ShouldNeverBeLoaded
{
}