Ver código fonte

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
matthew 15 anos atrás
pai
commit
173eaeea28

+ 4 - 6
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;
                 }
             }

+ 16 - 0
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);

+ 5 - 0
tests/Zend/Loader/_files/AutoloaderClosure.php

@@ -0,0 +1,5 @@
+<?php
+
+class AutoloaderTest_AutoloaderClosure
+{
+}