Skip to content

Commit 10a99cb

Browse files
committed
Merge pull request #5 from symfony-bundles/dev
Load extension if extension config is empty in container
2 parents cbb9109 + e1c8732 commit 10a99cb

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

BundleDependency.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SymfonyBundles\BundleDependency;
44

55
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use SymfonyBundles\BundleDependency\DependencyInjection\Compiler;
67

78
trait BundleDependency
89
{
@@ -45,11 +46,15 @@ protected function registerBundleDependencies(ContainerBuilder $container)
4546

4647
$this->bundles = $container->getParameter('kernel.bundles');
4748

48-
$this->createBundles($this->getBundleDependencies());
49+
if ($this->createBundles($this->getBundleDependencies())) {
50+
$container->setParameter('kernel.bundles', $this->bundles);
4951

50-
$container->setParameter('kernel.bundles', $this->bundles);
52+
$this->initializeBundles($container);
5153

52-
$this->initializeBundles($container);
54+
$pass = new Compiler\ExtensionLoadPass($this->instances);
55+
56+
$container->addCompilerPass($pass);
57+
}
5358

5459
$this->booted = true;
5560
}
@@ -59,7 +64,7 @@ protected function registerBundleDependencies(ContainerBuilder $container)
5964
*
6065
* @param array $dependencies
6166
*
62-
* @return void
67+
* @return bool Has new instances or not.
6368
*/
6469
protected function createBundles(array $dependencies)
6570
{
@@ -76,6 +81,8 @@ protected function createBundles(array $dependencies)
7681
}
7782
}
7883
}
84+
85+
return count($this->instances) > 0;
7986
}
8087

8188
/**
@@ -88,8 +95,6 @@ protected function initializeBundles(ContainerBuilder $container)
8895
foreach ($this->instances as $bundle) {
8996
if ($extension = $bundle->getContainerExtension()) {
9097
$container->registerExtension($extension);
91-
92-
$extension->load([], $container);
9398
}
9499

95100
$bundle->build($container);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace SymfonyBundles\BundleDependency\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
7+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8+
9+
class ExtensionLoadPass implements CompilerPassInterface
10+
{
11+
12+
/**
13+
* @var array
14+
*/
15+
private $bundles;
16+
17+
/**
18+
* @param array $bundles
19+
*/
20+
public function __construct(array $bundles)
21+
{
22+
$this->bundles = $bundles;
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function process(ContainerBuilder $container)
29+
{
30+
foreach ($this->bundles as $bundle) {
31+
if ($extension = $bundle->getContainerExtension()) {
32+
$this->loadExtension($extension, $container);
33+
}
34+
}
35+
}
36+
37+
/**
38+
* @param Extension $extension
39+
* @param ContainerBuilder $container
40+
*
41+
* @return void
42+
*/
43+
private function loadExtension(Extension $extension, ContainerBuilder $container)
44+
{
45+
if (!$container->getExtensionConfig($extension->getAlias())) {
46+
$extension->load([], $container);
47+
}
48+
}
49+
50+
}

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ If you want override a method `build`, call the method `registerBundleDependenci
5050
``` php
5151
public function build(ContainerBuilder $container)
5252
{
53-
$this->registerBundleDependencies($container);
53+
parent::build($container);
5454

5555
// ...
56+
57+
$this->registerBundleDependencies($container);
5658
}
5759
```
5860

0 commit comments

Comments
 (0)