소스 검색

[Kernel] Tweak bundle management

Victor Berchet 14 년 전
부모
커밋
65eb70d3b6
2개의 변경된 파일52개의 추가작업 그리고 29개의 파일을 삭제
  1. 17 7
      src/Symfony/Component/HttpKernel/Kernel.php
  2. 35 22
      tests/Symfony/Tests/Component/HttpKernel/KernelTest.php

+ 17 - 7
src/Symfony/Component/HttpKernel/Kernel.php

@@ -164,12 +164,12 @@ abstract class Kernel implements KernelInterface
     }
 
     /**
-     * Returns a bundle by its name.
+     * Returns a bundle and optionally its descendants by its name.
      *
      * @param string  $name  Bundle name
-     * @param Boolean $first Whether to return the first bundle or all bundles matching this name
+     * @param Boolean $first Whether to return the first bundle only or together with its descendants
      *
-     * @return BundleInterface A BundleInterface instance
+     * @return BundleInterface|Array A BundleInterface instance or an array of BundleInterface instances if $first is false
      *
      * @throws \InvalidArgumentException when the bundle is not enabled
      */
@@ -327,6 +327,16 @@ abstract class Kernel implements KernelInterface
         return $this->rootDir.'/logs';
     }
 
+    /**
+     * Initialize the data structures related to the bundle management:
+     *  - the bundle property maps a bundle name to a bundle instance,
+     *  - the bundleMap property maps a bundle name to the bundle inheritance hierarchy.
+     *
+     * @throws \LogicException if two bundles share a common name
+     * @throws \LogicException if a bundle tries to extend a non-existing bundle
+     * @throws \LogicException if two bundles extend the same ancestor
+     *
+     */
     protected function initializeBundles()
     {
         // init bundles
@@ -334,11 +344,11 @@ abstract class Kernel implements KernelInterface
         $this->bundleMap = array();
         foreach ($this->registerBundles() as $bundle) {
             $name = $bundle->getName();
+            if (isset($this->bundles[$name])) {
+                throw new \LogicException(sprintf('Trying to register two bundles with the same name "%s"', $name));
+            } 
             $this->bundles[$name] = $bundle;
-            if (!isset($this->bundleMap[$name])) {
-                $this->bundleMap[$name] = array();
-            }
-            $this->bundleMap[$name][] = $bundle;
+            $this->bundleMap[$name] = array($bundle);
         }
 
         // inheritance

+ 35 - 22
tests/Symfony/Tests/Component/HttpKernel/KernelTest.php

@@ -54,7 +54,7 @@ class KernelTest extends \PHPUnit_Framework_TestCase
 
     public function testLocateResourceReturnsTheFirstThatMatchesWithParent()
     {
-        $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1', '', 'ParentAABundle');
+        $parent = $this->getBundle(__DIR__.'/Fixtures/Bundle1', null, 'ParentAABundle');
         $child = $this->getBundle(__DIR__.'/Fixtures/Bundle2', 'ParentAABundle', 'ChildAABundle');
 
         $kernel = $this->getKernel();
@@ -142,7 +142,7 @@ class KernelTest extends \PHPUnit_Framework_TestCase
 
     public function testInitializeBundles()
     {
-        $parent = $this->getBundle(null, '', 'ParentABundle');
+        $parent = $this->getBundle(null, null, 'ParentABundle');
         $child = $this->getBundle(null, 'ParentABundle', 'ChildABundle');
 
         $kernel = $this->getKernel();
@@ -159,7 +159,7 @@ class KernelTest extends \PHPUnit_Framework_TestCase
 
     public function testInitializeBundlesSupportInheritanceCascade()
     {
-        $grandparent = $this->getBundle(null, '', 'GrandParentBBundle');
+        $grandparent = $this->getBundle(null, null, 'GrandParentBBundle');
         $parent = $this->getBundle(null, 'GrandParentBBundle', 'ParentBBundle');
         $child = $this->getBundle(null, 'ParentBBundle', 'ChildBBundle');
 
@@ -199,7 +199,7 @@ class KernelTest extends \PHPUnit_Framework_TestCase
      */
     public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtendedByTwoBundles()
     {
-        $parent = $this->getBundle(null, '', 'ParentCBundle');
+        $parent = $this->getBundle(null, null, 'ParentCBundle');
         $child1 = $this->getBundle(null, 'ParentCBundle', 'ChildC1Bundle');
         $child2 = $this->getBundle(null, 'ParentCBundle', 'ChildC2Bundle');
 
@@ -212,7 +212,24 @@ class KernelTest extends \PHPUnit_Framework_TestCase
         $kernel->initializeBundles();
     }
 
-    protected function getBundle($dir = null, $parent = null, $className = null)
+    /**
+     * @expectedException \LogicException
+     */
+    public function testInitializeBundleThrowsExceptionWhenRegisteringTwoBundlesWithTheSameName()
+    {
+        $fooBundle = $this->getBundle(null, null, 'FooBundle', 'DuplicateName');
+        $barBundle = $this->getBundle(null, null, 'BarBundle', 'DuplicateName');
+       
+        $kernel = $this->getKernel();
+        $kernel
+            ->expects($this->once())
+            ->method('registerBundles')
+            ->will($this->returnValue(array($fooBundle, $barBundle)))
+        ;
+        $kernel->initializeBundles();      
+    }
+
+    protected function getBundle($dir = null, $parent = null, $className = null, $bundleName = null)
     {
         $bundle = $this
             ->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
@@ -229,25 +246,21 @@ class KernelTest extends \PHPUnit_Framework_TestCase
         $bundle
             ->expects($this->any())
             ->method('getName')
-            ->will($this->returnValue(get_class($bundle)))
+            ->will($this->returnValue(is_null($bundleName) ? get_class($bundle) : $bundleName))
         ;
 
-        if (null !== $dir) {
-            $bundle
-                ->expects($this->any())
-                ->method('getPath')
-                ->will($this->returnValue($dir))
-            ;
-        }
-
-        if (null !== $parent) {
-            $bundle
-                ->expects($this->any())
-                ->method('getParent')
-                ->will($this->returnValue($parent))
-            ;
-        }
-
+        $bundle
+            ->expects($this->any())
+            ->method('getPath')
+            ->will($this->returnValue($dir))
+        ;
+        
+        $bundle
+            ->expects($this->any())
+            ->method('getParent')
+            ->will($this->returnValue($parent))
+        ;
+        
         return $bundle;
     }