Skip to content

Commit

Permalink
[Kernel] Tweak bundle management
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored and fabpot committed Jan 29, 2011
1 parent 96a0a7e commit 65eb70d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
24 changes: 17 additions & 7 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -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
*/
Expand Down Expand Up @@ -327,18 +327,28 @@ 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
$this->bundles = array();
$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
Expand Down
57 changes: 35 additions & 22 deletions tests/Symfony/Tests/Component/HttpKernel/KernelTest.php
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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');

Expand Down Expand Up @@ -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');

Expand All @@ -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')
Expand All @@ -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;
}

Expand Down

0 comments on commit 65eb70d

Please sign in to comment.