8000 Merge remote branch 'kriswallsmith/kernel/bundle-extension' · beenalee/symfony@40d256e · GitHub
[go: up one dir, main page]

Skip to content

Commit 40d256e

Browse files
committed
Merge remote branch 'kriswallsmith/kernel/bundle-extension'
* kriswallsmith/kernel/bundle-extension: [HttpKernel] added check of default extension alias convention [AsseticBundle] coding standard and comment tweaks [HttpKernel] added BundleInterface::getContainerExtension() which is implicitly loaded
2 parents 4fb1035 + 97f66e9 commit 40d256e

File tree

5 files changed

+70
-53
lines changed

5 files changed

+70
-53
lines changed

src/Symfony/Component/HttpKernel/Bundle/Bundle.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class Bundle extends ContainerAware implements BundleInterface
2727
{
2828
protected $name;
2929
protected $reflected;
30+
protected $extension;
3031

3132
/**
3233
* Boots the Bundle.
@@ -47,29 +48,46 @@ public function shutdown()
4748
*
4849
* It is only ever called once when the cache is empty.
4950
*
50-
* The default implementation automatically registers a DIC extension
51-
* if its name is the same as the bundle name after replacing the
52-
* Bundle suffix by Extension (DependencyInjection\SensioBlogExtension
53-
* for a SensioBlogBundle for instance). In such a case, the alias
54-
* is forced to be the underscore version of the bundle name
55-
* (sensio_blog for a SensioBlogBundle for instance).
56-
*
5751
* This method can be overridden to register compilation passes,
5852
* other extensions, ...
5953
*
6054
* @param ContainerBuilder $container A ContainerBuilder instance
6155
*/
6256
public function build(ContainerBuilder $container)
6357
{
64-
$class = $this->getNamespace().'\\DependencyInjection\\'.str_replace('Bundle', 'Extension', $this->getName());
65-
if (class_exists($class)) {
66-
$extension = new $class();
67-
$alias = Container::underscore(str_replace('Bundle', '', $this->getName()));
68-
if ($alias !== $extension->getAlias()) {
69-
throw new \LogicException(sprintf('The extension alias for the default extension of a bundle must be the underscored version of the bundle name ("%s" vs "%s")', $alias, $extension->getAlias()));
58+
}
59+
60+
/**
61+
* Returns the bundle's container extension.
62+
*
63+
* @return ExtensionInterface|null The container extension
64+
*/
65+
public function getContainerExtension()
66+
{
67+
if (null === $this->extension) {
68+
$class = $this->getNamespace().'\\DependencyInjection\\'.str_replace('Bundle', 'Extension', $this->getName());
69+
if (class_exists($class)) {
70+
$extension = new $class();
71+
72+
// check naming convention
73+
$expectedAlias = Container::underscore(str_replace('Bundle', '', $this->getName()));
74+
if ($expectedAlias != $extension->getAlias()) {
75+
throw new \LogicException(sprintf(
76+
'The extension alias for the default extension of a '.
77+
'bundle must be the underscored version of the '.
78+
'bundle name ("%s" vs "%s")',
79+
$expectedAlias, $extension->getAlias()
80+
10000 ));
81+
}
82+
83+
$this->extension = $extension;
84+
} else {
85+
$this->extension = false;
7086
}
87+
}
7188

72-
$container->registerExtension($extension);
89+
if ($this->extension) {
90+
return $this->extension;
7391
}
7492
}
7593

src/Symfony/Component/HttpKernel/Bundle/BundleInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ function shutdown();
3939
*/
4040
function build(ContainerBuilder $container);
4141

42+
/**
43+
* Returns the container extension that should be implicitly loaded.
44+
*
45+
* @return ExtensionInterface|null The default extension or null if there is none
46+
*/
47+
function getContainerExtension();
48+
4249
/**
4350
* Returns the bundle parent name.
4451
*

src/Symfony/Component/HttpKernel/DependencyInjection/MergeExtensionConfigurationPass.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,24 @@
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616

1717
/**
18-
* Handles automatically loading each bundle's default extension.
18+
* Ensures certain extensions are always loaded.
1919
*
20-
* @author Kris Wallsmith <kris.wallsmith@symfony.com>
20+
* @author Kris Wallsmith <kris@symfony.com>
2121
*/
2222
class MergeExtensionConfigurationPass extends BaseMergeExtensionConfigurationPass
2323
{
24-
public function process(ContainerBuilder $container)
25-
{
26-
foreach ($container->getParameter('kernel.bundles') as $bundleName => $bundleClass) {
27-
$bundleRefl = new \ReflectionClass($bundleClass);
28-
$extClass = $bundleRefl->getNamespaceName().'\\DependencyInjection\\'.substr($bundleName, 0, -6).'Extension';
24+
private $extensions;
2925

30-
if (class_exists($extClass)) {
31-
$ext = new $extClass();
32-
$alias = $ext->getAlias();
26+
public function __construct(array $extensions)
27+
{
28+
$this->extensions = $extensions;
29+
}
3330

34-
// ensure all "main" extensions are loaded
35-
if (!count($container->getExtensionConfig($alias))) {
36-
$container->loadFromExtension($alias, array());
37-
}
31+
public function process(ContainerBuilder $container)
32+
{
33+
foreach ($this->extensions as $extension) {
34+
if (!count($container->getExtensionConfig($extension))) {
35+
$container->loadFromExtension($extension, array());
3836
}
3937
}
4038

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,19 +529,25 @@ protected function getEnvParameters()
529529
*/
530530
protected function buildContainer()
531531
{
532-
$parameterBag = new ParameterBag($this->getKernelParameters());
533-
534-
$container = new ContainerBuilder($parameterBag);
535-
$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass());
532+
$container = new ContainerBuilder(new ParameterBag($this->getKernelParameters()));
533+
$extensions = array();
536534
foreach ($this->bundles as $bundle) {
537535
$bundle->build($container);
538536

537+
if ($extension = $bundle->getContainerExtension()) {
538+
$container->registerExtension($extension);
539+
$extensions[] = $extension->getAlias();
540+
}
541+
539542
if ($this->debug) {
540543
$container->addObjectResource($bundle);
541544
}
542545
}
543546
$container->addObjectResource($this);
544547

548+
// ensure these extensions are implicitly loaded
549+
$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));
550+
545551
if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) {
546552
$container->merge($cont);
547553
}

tests/Symfony/Tests/Component/HttpKernel/DependencyInjection/MergeExtensionConfigurationPassTest.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,20 @@ class MergeExtensionConfigurationPassTest extends \PHPUnit_Framework_TestCase
1717
{
1818
public function testAutoloadMainExtension()
1919
{
20-
$bundles = array(
21-
'ExtensionAbsentBundle' => 'Symfony\\Tests\\Component\\HttpKernel\\Fixtures\\ExtensionAbsentBundle\\ExtensionAbsentBundle',
22-
'ExtensionLoadedBundle' => 'Symfony\\Tests\\Component\\HttpKernel\\Fixtures\\ExtensionLoadedBundle\\ExtensionLoadedBundle',
23-
'ExtensionPresentBundle' => 'Symfony\\Tests\\Component\\HttpKernel\\Fixtures\\ExtensionPresentBundle\\ExtensionPresentBundle',
24-
);
25-
2620
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerBuilder');
2721
$params = $this->getMock('Symfony\\Component\\DependencyInjection\\ParameterBag\\ParameterBag');
2822

29-
$container->expects($this->once())
30-
->method('getParameter')
31-
->with('kernel.bundles')
32-
->will($this->returnValue($bundles));
33-
$container->expects($this->exactly(2))
23+
$container->expects($this->at(0))
3424
->method('getExtensionConfig')
35-
->will($this->returnCallback(function($name) {
36-
switch ($name) {
37-
case 'extension_present':
38-
return array();
39-
case 'extension_loaded':
40-
return array(array());
41-
}
42-
}));
25+
->with('loaded')
26+
->will($this->returnValue(array(array())));
27+
$container->expects($this->at(1))
28+
->method('getExtensionConfig')
29+
->with('notloaded')
30+
->will($this->returnValue(array()));
4331
$container->expects($this->once())
4432
->method('loadFromExtension')
45-
->with('extension_present', array());
33+
->with('notloaded', array());
4634

4735
$container->expects($this->any())
4836
->method('getParameterBag')
@@ -60,7 +48,7 @@ public function testAutoloadMainExtension()
6048
->method('getExtensions')
6149
->will($this->returnValue(array()));
6250

63-
$configPass = new MergeExtensionConfigurationPass();
51+
$configPass = new MergeExtensionConfigurationPass(array('loaded', 'notloaded'));
6452
$configPass->process($container);
6553
}
6654
}

0 commit comments

Comments
 (0)
0