浏览代码

[HttpKernel] changed Kernel::locateResource() to also work with directories

Fabien Potencier 14 年之前
父节点
当前提交
bd6bc4db62
共有 2 个文件被更改,包括 21 次插入2 次删除
  1. 4 2
      src/Symfony/Component/HttpKernel/Kernel.php
  2. 17 0
      tests/Symfony/Tests/Component/HttpKernel/KernelTest.php

+ 4 - 2
src/Symfony/Component/HttpKernel/Kernel.php

@@ -259,6 +259,8 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
     /**
      * Returns the file path for a given resource.
      *
+     * A Resource can be a file or a directory.
+     *
      * The resource name must follow the following pattern:
      *
      *     @BundleName/path/to/a/file.something
@@ -296,7 +298,7 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
         $isResource = 0 === strpos($path, 'Resources');
 
         $files = array();
-        if (true === $isResource && null !== $dir && is_file($file = $dir.'/'.$bundle.'/'.substr($path, 10))) {
+        if (true === $isResource && null !== $dir && file_exists($file = $dir.'/'.$bundle.'/'.substr($path, 10))) {
             if ($first) {
                 return $file;
             }
@@ -305,7 +307,7 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
         }
 
         foreach ($this->getBundle($bundle, false) as $bundle) {
-            if (is_file($file = $bundle->getPath().'/'.$path)) {
+            if (file_exists($file = $bundle->getPath().'/'.$path)) {
                 if ($first) {
                     return $file;
                 }

+ 17 - 0
tests/Symfony/Tests/Component/HttpKernel/KernelTest.php

@@ -130,6 +130,23 @@ class KernelTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(array(__DIR__.'/Fixtures/foo/foo.txt', __DIR__.'/Fixtures/Bundle1/Resources/foo.txt'), $kernel->locateResource('@foo/Resources/foo.txt', __DIR__.'/Fixtures', false));
     }
 
+    public function testLocateResourceOnDirectories()
+    {
+        $kernel = $this->getKernel();
+
+        $this->assertEquals(__DIR__.'/Fixtures/foo/', $kernel->locateResource('@foo/Resources/', __DIR__.'/Fixtures'));
+        $this->assertEquals(__DIR__.'/Fixtures/foo/', $kernel->locateResource('@foo/Resources', __DIR__.'/Fixtures'));
+
+        $kernel
+            ->expects($this->any())
+            ->method('getBundle')
+            ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
+        ;
+
+        $this->assertEquals(__DIR__.'/Fixtures/Bundle1/Resources/', $kernel->locateResource('@foo/Resources/'));
+        $this->assertEquals(__DIR__.'/Fixtures/Bundle1/Resources/', $kernel->locateResource('@foo/Resources/'));
+    }
+
     public function testInitializeBundles()
     {
         $parent = $this->getBundle(null, '', 'ParentABundle');