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

Prevent the same loader from being registered twice #135

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/Doctrine/Common/Annotations/AnnotationRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,29 @@ final class AnnotationRegistry
*/
static private $loaders = array();

/**
* An array of loaded classes
*
* @var array
*/
static private $loaded = array();

/**
* An array of classes which cannot be found
*
* @var array
*/
static private $unloadable = array();

/**
* @return void
*/
static public function reset()
{
self::$autoloadNamespaces = array();
self::$loaders = array();
self::$loaded = array();
self::$unloadable = array();
}

/**
Expand Down Expand Up @@ -110,6 +126,9 @@ static public function registerLoader($callable)
if (!is_callable($callable)) {
throw new \InvalidArgumentException("A callable is expected in AnnotationRegistry::registerLoader().");
}
// Reset our static cache now that we have a new loader to work with
self::$loaded = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loaded classes do not need to be reset - there is nothing that we can reset anyway

self::$unloadable = array();
self::$loaders[] = $callable;
}

Expand All @@ -122,18 +141,26 @@ static public function registerLoader($callable)
*/
static public function loadAnnotationClass($class)
{
if (isset(self::$loaded[$class])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looking good 👍

return true;
}
if (isset(self::$unloadable[$class])) {
return false;
}
foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
if (strpos($class, $namespace) === 0) {
$file = str_replace("\\", DIRECTORY_SEPARATOR, $class) . ".php";
if ($dirs === null) {
if ($path = stream_resolve_include_path($file)) {
require $path;
self::$loaded[$class] = true;
return true;
}
} else {
foreach((array)$dirs AS $dir) {
if (is_file($dir . DIRECTORY_SEPARATOR . $file)) {
require $dir . DIRECTORY_SEPARATOR . $file;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Urgh, why do we even have our own autoloading garbage in here? :S

self::$loaded[$class] = true;
return true;
}
}
Expand All @@ -143,9 +170,11 @@ static public function loadAnnotationClass($class)

foreach (self::$loaders AS $loader) {
if (call_user_func($loader, $class) === true) {
self::$loaded[$class] = true;
return true;
}
}
self::$unloadable[$class] = true;
return false;
}
}
43 changes: 43 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function testReset()

self::assertEmpty($this->getStaticField($this->class, 'autoloadNamespaces'));
self::assertEmpty($this->getStaticField($this->class, 'loaders'));
self::assertEmpty($this->getStaticField($this->class, 'loaded'));
self::assertEmpty($this->getStaticField($this->class, 'unloadable'));
}

/**
Expand Down Expand Up @@ -65,4 +67,45 @@ protected function getStaticField($class, $field)

return $reflection->getValue();
}

public function testStopCallingLoadersIfClassIsNotFound()
{
AnnotationRegistry::reset();
$i = 0;
$autoLoader = function($annotation) use (&$i) {
$i++;
return false;
};
AnnotationRegistry::registerLoader($autoLoader);
AnnotationRegistry::loadAnnotationClass('unloadableClass');
AnnotationRegistry::loadAnnotationClass('unloadableClass');
AnnotationRegistry::loadAnnotationClass('unloadableClass');
$this->assertEquals(1, $i, 'Autoloader should only be called once');
}

public function testStopCallingLoadersAfterClassIsFound()
{
AnnotationRegistry::reset();
$i = 0;
$autoLoader = function($annotation) use (&$i) {
$i++;
return true;
};
AnnotationRegistry::registerLoader($autoLoader);
AnnotationRegistry::loadAnnotationClass(self::class);
AnnotationRegistry::loadAnnotationClass(self::class);
AnnotationRegistry::loadAnnotationClass(self::class);
$this->assertEquals(1, $i, 'Autoloader should only be called once');
}

public function testAddingANewLoaderClearsTheCache()
{
AnnotationRegistry::reset();
AnnotationRegistry::registerLoader('class_exists');
AnnotationRegistry::loadAnnotationClass(self::class);
AnnotationRegistry::loadAnnotationClass('unloadableClass');
AnnotationRegistry::registerLoader('class_exists');
self::assertEmpty($this->getStaticField($this->class, 'loaded'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be checked with an external loader, verifying that it will be called again. Please refrain from reading private state in a test

self::assertEmpty($this->getStaticField($this->class, 'unloadable'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

}
}