-
Notifications
You must be signed in to change notification settings - Fork 235
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
} | ||
|
||
/** | ||
|
@@ -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(); | ||
self::$unloadable = array(); | ||
self::$loaders[] = $callable; | ||
} | ||
|
||
|
@@ -122,18 +141,26 @@ static public function registerLoader($callable) | |
*/ | ||
static public function loadAnnotationClass($class) | ||
{ | ||
if (isset(self::$loaded[$class])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
|
@@ -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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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')); | ||
} | ||
|
||
/** | ||
|
@@ -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')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
} | ||
} |
There was a problem hiding this comment.
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