From 024ec674d5e480c3ccccdb89122c2695060f4bbd Mon Sep 17 00:00:00 2001 From: Freek Gruntjes Date: Mon, 17 Sep 2012 09:55:16 +0200 Subject: [PATCH] Fix bug in classExists when using multiple autoloaders (i.e. ZF2) --- lib/Doctrine/Common/ClassLoader.php | 5 ++++- .../Doctrine/Tests/Common/ClassLoaderTest.php | 19 +++++++++++++++++++ .../Tests/Common/ClassLoaderTest/ClassE.php | 5 +++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/Common/ClassLoaderTest/ClassE.php diff --git a/lib/Doctrine/Common/ClassLoader.php b/lib/Doctrine/Common/ClassLoader.php index 45024e16c..6bd43c593 100644 --- a/lib/Doctrine/Common/ClassLoader.php +++ b/lib/Doctrine/Common/ClassLoader.php @@ -235,9 +235,12 @@ public static function classExists($className) } else if (is_string($loader) && $loader($className)) { // "MyClass::loadClass" return true; } + if (class_exists($className, false) || interface_exists($className, false)) { + return true; + } } - return class_exists($className, false) || interface_exists($className, false); + return false; } /** diff --git a/tests/Doctrine/Tests/Common/ClassLoaderTest.php b/tests/Doctrine/Tests/Common/ClassLoaderTest.php index 567cf9185..c925e46d6 100644 --- a/tests/Doctrine/Tests/Common/ClassLoaderTest.php +++ b/tests/Doctrine/Tests/Common/ClassLoaderTest.php @@ -33,7 +33,26 @@ public function testClassExists() $this->assertTrue(ClassLoader::classExists('ClassLoaderTest\ClassD')); spl_autoload_unregister($badLoader); } + + public function testClassExistsWithMultipleNonReturningAutoloaders() + { + $this->assertFalse(ClassLoader::classExists('ClassLoaderTest\ClassE')); + $nonReturnLoader = function($className) { + if (class_exists($className, false)) { + \PHPUnit_Framework_Assert::fail('Class load called twice for same class.'); + } + require __DIR__ . '/ClassLoaderTest/ClassE.php'; + }; + $nonReturnLoader2 = clone $nonReturnLoader; + spl_autoload_register($nonReturnLoader); + spl_autoload_register($nonReturnLoader2); + + $this->assertTrue(ClassLoader::classExists('ClassLoaderTest\ClassE')); + spl_autoload_unregister($nonReturnLoader); + spl_autoload_unregister($nonReturnLoader2); + } + public function testGetClassLoader() { $cl = new ClassLoader('ClassLoaderTest', __DIR__); diff --git a/tests/Doctrine/Tests/Common/ClassLoaderTest/ClassE.php b/tests/Doctrine/Tests/Common/ClassLoaderTest/ClassE.php new file mode 100644 index 000000000..f7a081130 --- /dev/null +++ b/tests/Doctrine/Tests/Common/ClassLoaderTest/ClassE.php @@ -0,0 +1,5 @@ +