Sfoglia il codice sorgente

ZF-6727: fixed double slashes in path, restructured method and added test case

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19084 44c647ce-9c0f-0410-b52a-842ac1e357ba
bate 16 anni fa
parent
commit
b831c989b4

+ 17 - 6
library/Zend/Loader/Autoloader/Resource.php

@@ -132,12 +132,12 @@ class Zend_Loader_Autoloader_Resource implements Zend_Loader_Autoloader_Interfac
     }
 
     /**
-     * Attempt to autoload a class
+     * Helper method to calculate the correct class path
      *
-     * @param  string $class
-     * @return mixed False if not matched, otherwise result if include operation
+     * @param string $class
+     * @return False if not matched other wise the correct path
      */
-    public function autoload($class)
+    public function getClassPath($class)
     {
         $segments          = explode('_', $class);
         $namespaceTopLevel = $this->getNamespace();
@@ -171,9 +171,20 @@ class Zend_Loader_Autoloader_Resource implements Zend_Loader_Autoloader_Interfac
             return false;
         }
 
-        $final = substr($class, strlen($lastMatch));
+        $final = substr($class, strlen($lastMatch) + 1);
         $path = $this->_components[$lastMatch];
-        return include $path . '/' . str_replace('_', '/', $final) . '.php';
+        return $path . '/' . str_replace('_', '/', $final) . '.php';
+    }
+
+    /**
+     * Attempt to autoload a class
+     *
+     * @param  string $class
+     * @return mixed False if not matched, otherwise result if include operation
+     */
+    public function autoload($class)
+    {
+        return include $this->getClassPath($class);
     }
 
     /**

+ 13 - 0
tests/Zend/Loader/Autoloader/ResourceTest.php

@@ -399,6 +399,19 @@ class Zend_Loader_Autoloader_ResourceTest extends PHPUnit_Framework_TestCase
         $this->assertEquals($this->loader->getBasePath() . '/models', $resources['models']['path']);
     }
 
+    /**
+     * @group ZF-6727
+     */
+    public function testAutoloaderResourceGetClassPath()
+    {
+        //print_r(get_class_methods($this->loader));
+        $this->loader->addResourceTypes(array(
+            'model' => array('path' => 'models', 'namespace' => 'Model'),
+        ));
+        $path = $this->loader->getClassPath('FooBar_Model_Class_Model');
+        // if true we have // in path
+        $this->assertFalse(strpos($path, '//'));
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Loader_Autoloader_ResourceTest::main') {