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

Fix bug in classExists when using multiple autoloaders (i.e. ZF2) #183

Closed
wants to merge 1 commit into from
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
5 changes: 4 additions & 1 deletion lib/Doctrine/Common/ClassLoader.php
Expand Up @@ -235,9 +235,12 @@ public static function classExists($className)
} else if (is_string($loader) && $loader($className)) { // "MyClass::loadClass" } else if (is_string($loader) && $loader($className)) { // "MyClass::loadClass"
return true; return true;
} }
if (class_exists($className, false) || interface_exists($className, false)) {
return true;
}
} }


return class_exists($className, false) || interface_exists($className, false); return false;
} }


/** /**
Expand Down
19 changes: 19 additions & 0 deletions tests/Doctrine/Tests/Common/ClassLoaderTest.php
Expand Up @@ -33,7 +33,26 @@ public function testClassExists()
$this->assertTrue(ClassLoader::classExists('ClassLoaderTest\ClassD')); $this->assertTrue(ClassLoader::classExists('ClassLoaderTest\ClassD'));
spl_autoload_unregister($badLoader); 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() public function testGetClassLoader()
{ {
$cl = new ClassLoader('ClassLoaderTest', __DIR__); $cl = new ClassLoader('ClassLoaderTest', __DIR__);
Expand Down
5 changes: 5 additions & 0 deletions tests/Doctrine/Tests/Common/ClassLoaderTest/ClassE.php
@@ -0,0 +1,5 @@
<?php

namespace ClassLoaderTest;

class ClassE {}