浏览代码

[Kernel] Make resources overriding consistent across bundle directories and resource directories

Victor Berchet 14 年之前
父节点
当前提交
3cd3dd39ba

+ 10 - 11
src/Symfony/Component/HttpKernel/Kernel.php

@@ -234,20 +234,19 @@ abstract class Kernel implements KernelInterface
         }
 
         $name = substr($name, 1);
-        list($bundle, $path) = explode('/', $name, 2);
+        list($bundleName, $path) = explode('/', $name, 2);
 
-        $isResource = 0 === strpos($path, 'Resources');
-
-        $files = array();
-        if (true === $isResource && null !== $dir && file_exists($file = $dir.'/'.$bundle.'Bundle/'.substr($path, 10))) {
-            if ($first) {
-                return $file;
+        $isResource = 0 === strpos($path, 'Resources') && null !== $dir;
+        $overridePath = substr($path, 9);
+        
+        foreach ($this->getBundle($bundleName, false) as $bundle) {
+            if ($isResource && file_exists($file = $dir.'/'.$bundle->getName().'Bundle'.$overridePath)) {
+                if ($first) {
+                    return $file;
+                }
+                $files[] = $file;
             }
 
-            $files[] = $file;
-        }
-
-        foreach ($this->getBundle($bundle, false) as $bundle) {
             if (file_exists($file = $bundle->getPath().'/'.$path)) {
                 if ($first) {
                     return $file;

tests/Symfony/Tests/Component/HttpKernel/Fixtures/Bundle1/Resources/foo.txt → tests/Symfony/Tests/Component/HttpKernel/Fixtures/BaseBundle/Resources/foo.txt


tests/Symfony/Tests/Component/HttpKernel/Fixtures/Bundle1/foo.txt → tests/Symfony/Tests/Component/HttpKernel/Fixtures/Bundle1Bundle/Resources/foo.txt


tests/Symfony/Tests/Component/HttpKernel/Fixtures/Bundle1/bar.txt → tests/Symfony/Tests/Component/HttpKernel/Fixtures/Bundle1Bundle/bar.txt


tests/Symfony/Tests/Component/HttpKernel/Fixtures/Bundle2/Resources/foo.txt → tests/Symfony/Tests/Component/HttpKernel/Fixtures/Bundle1Bundle/foo.txt


tests/Symfony/Tests/Component/HttpKernel/Fixtures/Bundle2/foo.txt → tests/Symfony/Tests/Component/HttpKernel/Fixtures/Bundle2Bundle/foo.txt


tests/Symfony/Tests/Component/HttpKernel/Fixtures/fooBundle/foo.txt → tests/Symfony/Tests/Component/HttpKernel/Fixtures/ChildBundle/Resources/foo.txt


+ 0 - 0
tests/Symfony/Tests/Component/HttpKernel/Fixtures/Resources/Bundle1Bundle/foo.txt


+ 0 - 0
tests/Symfony/Tests/Component/HttpKernel/Fixtures/Resources/ChildBundle/foo.txt


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

@@ -333,7 +333,7 @@ EOF;
         $kernel
             ->expects($this->once())
             ->method('getBundle')
-            ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
+            ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
         ;
 
         $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/foo.txt', $kernel->locateResource('@Bundle1/foo.txt'));
@@ -341,12 +341,12 @@ EOF;
 
     public function testLocateResourceReturnsTheFirstThatMatchesWithParent()
     {
-        $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1', null, 'ParentAA');
-        $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2', 'ParentAA', 'ChildAA');
+        $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
+        $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
 
         $kernel = $this->getKernel();
         $kernel
-            ->expects($this->any())
+            ->expects($this->exactly(2))
             ->method('getBundle')
             ->will($this->returnValue(array($child, $parent)))
         ;
@@ -355,22 +355,22 @@ EOF;
         $this->assertEquals(__DIR__.'/Fixtures/Bundle1Bundle/bar.txt', $kernel->locateResource('@ParentAA/bar.txt'));
     }
 
-    public function testLocateResourceReturnsTheAllMatches()
+    public function testLocateResourceReturnsAllMatches()
     {
+        $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle');
+        $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2Bundle');
+
         $kernel = $this->getKernel();
         $kernel
             ->expects($this->once())
             ->method('getBundle')
-            ->will($this->returnValue(array(
-                $this->getBundle(__DIR__.'/Fixtures/Bundle1'),
-                $this->getBundle(__DIR__.'/Fixtures/Bundle2')
-            )))
+            ->will($this->returnValue(array($child, $parent)))
         ;
 
         $this->assertEquals(array(
-            __DIR__.'/Fixtures/Bundle1Bundle/foo.txt',
-            __DIR__.'/Fixtures/Bundle2Bundle/foo.txt'),
-            $kernel->locateResource('@Bundle2/foo.txt', null, false));
+            __DIR__.'/Fixtures/Bundle2Bundle/foo.txt',
+            __DIR__.'/Fixtures/Bundle1Bundle/foo.txt'),
+            $kernel->locateResource('@Bundle1/foo.txt', null, false));
     }
 
     public function testLocateResourceReturnsAllMatchesBis()
@@ -381,7 +381,7 @@ EOF;
             ->method('getBundle')
             ->will($this->returnValue(array(
                 $this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'),
-                $this->getBundle(__DIR__.'/foobar')
+                $this->getBundle(__DIR__.'/Foobar')
             )))
         ;
 
@@ -397,7 +397,7 @@ EOF;
         $kernel
             ->expects($this->once())
             ->method('getBundle')
-            ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
+            ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle'))))
         ;
 
         $this->assertEquals(
@@ -427,7 +427,7 @@ EOF;
         $kernel
             ->expects($this->once())
             ->method('getBundle')
-            ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
+            ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1'))))
         ;
 
         $this->assertEquals(array(
@@ -437,6 +437,27 @@ EOF;
         );
     }
 
+    public function testLocateResourceOverrideBundleAndResourcesFolders()
+    {
+        $parent = $this->getBundle(__DIR__.'/Fixtures/BaseBundle', null, 'Base', 'Base');
+        $child = $this->getBundle(__DIR__.'/Fixtures/ChildBundle', 'Parent', 'Child', 'Child');
+
+        $kernel = $this->getKernel();
+        $kernel
+            ->expects($this->once())
+            ->method('getBundle')
+            ->will($this->returnValue(array($child, $parent)))
+        ;
+
+        $this->assertEquals(array(
+            __DIR__.'/Fixtures/Resources/ChildBundle/foo.txt',
+            __DIR__.'/Fixtures/ChildBundle/Resources/foo.txt',
+            __DIR__.'/Fixtures/BaseBundle/Resources/foo.txt',
+            ),
+            $kernel->locateResource('@Child/Resources/foo.txt', __DIR__.'/Fixtures/Resources', false)
+        );
+    }
+
     public function testLocateResourceOnDirectories()
     {
         $kernel = $this->getKernel();
@@ -455,10 +476,11 @@ EOF;
             $kernel->locateResource('@Foo/Resources', __DIR__.'/Fixtures/Resources')
         );
 
+        $kernel = $this->getKernel();
         $kernel
-            ->expects($this->any())
+            ->expects($this->exactly(2))
             ->method('getBundle')
-            ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
+            ->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1Bundle', null, null, 'Bundle1'))))
         ;
 
         $this->assertEquals(