From f3e2cfb87f003d0bc9793da18b8669dda11debda Mon Sep 17 00:00:00 2001 From: matthew Date: Mon, 21 Jun 2010 17:38:05 +0000 Subject: [PATCH] ZF-10024: allow using closures as autoloaders - Backported r22480 to trunk. git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22481 44c647ce-9c0f-0410-b52a-842ac1e357ba --- library/Zend/Loader/Autoloader.php | 10 ++++------ tests/Zend/Loader/AutoloaderTest.php | 16 ++++++++++++++++ tests/Zend/Loader/_files/AutoloaderClosure.php | 5 +++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 tests/Zend/Loader/_files/AutoloaderClosure.php diff --git a/library/Zend/Loader/Autoloader.php b/library/Zend/Loader/Autoloader.php index d6c3546df..98e262aad 100644 --- a/library/Zend/Loader/Autoloader.php +++ b/library/Zend/Loader/Autoloader.php @@ -120,14 +120,12 @@ class Zend_Loader_Autoloader if ($autoloader->autoload($class)) { return true; } - } elseif (is_string($autoloader)) { - if ($autoloader($class)) { + } elseif (is_array($autoloader)) { + if (call_user_func($autoloader, $class)) { return true; } - } elseif (is_array($autoloader)) { - $object = array_shift($autoloader); - $method = array_shift($autoloader); - if (call_user_func(array($object, $method), $class)) { + } elseif (is_string($autoloader) || is_callable($autoloader)) { + if ($autoloader($class)) { return true; } } diff --git a/tests/Zend/Loader/AutoloaderTest.php b/tests/Zend/Loader/AutoloaderTest.php index 26adc5ddd..40a4fdd68 100644 --- a/tests/Zend/Loader/AutoloaderTest.php +++ b/tests/Zend/Loader/AutoloaderTest.php @@ -382,6 +382,22 @@ class Zend_Loader_AutoloaderTest extends PHPUnit_Framework_TestCase $this->assertFalse(class_exists($class, false)); } + /** + * @group ZF-10024 + */ + public function testClosuresRegisteredWithAutoloaderShouldBeUtilized() + { + if (version_compare(PHP_VERSION, '5.3.0', '<')) { + $this->markTestSkipped(__METHOD__ . ' requires PHP version 5.3.0 or greater'); + } + + $this->autoloader->pushAutoloader(function($class) { + require_once dirname(__FILE__) . '/_files/AutoloaderClosure.php'; + }); + $test = new AutoloaderTest_AutoloaderClosure(); + $this->assertTrue($test instanceof AutoloaderTest_AutoloaderClosure); + } + public function addTestIncludePath() { set_include_path(dirname(__FILE__) . '/_files/' . PATH_SEPARATOR . $this->includePath); diff --git a/tests/Zend/Loader/_files/AutoloaderClosure.php b/tests/Zend/Loader/_files/AutoloaderClosure.php new file mode 100644 index 000000000..1033b17be --- /dev/null +++ b/tests/Zend/Loader/_files/AutoloaderClosure.php @@ -0,0 +1,5 @@ +