From 65eb70d3b6373ace7ba85df404e88795cf9b5fa7 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 28 Jan 2011 21:55:43 +0100 Subject: [PATCH] [Kernel] Tweak bundle management --- src/Symfony/Component/HttpKernel/Kernel.php | 24 +++++--- .../Tests/Component/HttpKernel/KernelTest.php | 57 ++++++++++++------- 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 0d133113570f..1ab343a2b503 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -164,12 +164,12 @@ public function isClassInActiveBundle($class) } /** - * 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 @@ public function getLogDir() 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 @@ protected function initializeBundles() $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 diff --git a/tests/Symfony/Tests/Component/HttpKernel/KernelTest.php b/tests/Symfony/Tests/Component/HttpKernel/KernelTest.php index 4ddbd08b5236..9a9e64b78072 100644 --- a/tests/Symfony/Tests/Component/HttpKernel/KernelTest.php +++ b/tests/Symfony/Tests/Component/HttpKernel/KernelTest.php @@ -54,7 +54,7 @@ public function testLocateResourceReturnsTheFirstThatMatches() 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 @@ public function testLocateResourceOnDirectories() 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 @@ public function testInitializeBundles() 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 @@ public function testInitializeBundlesThrowsExceptionWhenAParentDoesNotExists() */ 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 @@ public function testInitializeBundlesThrowsExceptionWhenABundleIsDirectlyExtende $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 @@ protected function getBundle($dir = null, $parent = null, $className = null) $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; }