Code

< 40 %
40-60 %
> 60 %
1
<?php
2
3
namespace SymfonyBundles\BundleDependency;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use SymfonyBundles\BundleDependency\DependencyInjection\Compiler;
7
8
trait BundleDependency
9
{
10
    /**
11
     * @var bool
12
     */
13
    private $booted = false;
14
15
    /**
16
     * @var array
17
     */
18
    private $bundles = [];
19
20
    /**
21
     * @var array
22
     */
23
    private $instances = [];
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 1
    public function build(ContainerBuilder $container)
29
    {
30 1
        $this->registerBundleDependencies($container);
31 1
    }
32
33
    /**
34
     * Register the bundle dependencies.
35
     *
36
     * @param ContainerBuilder $container
37
     */
38 1
    protected function registerBundleDependencies(ContainerBuilder $container)
39
    {
40 1
        if (true === $this->booted) {
41 1
            return;
42
        }
43
44 1
        $this->bundles = $container->getParameter('kernel.bundles');
45
46 1
        if ($this->createBundles($this->getBundleDependencies())) {
47 1
            $container->setParameter('kernel.bundles', $this->bundles);
48
49 1
            $this->initializeBundles($container);
50
51 1
            $pass = new Compiler\ExtensionLoadPass($this->instances);
52
53 1
            $container->addCompilerPass($pass);
54 1
        }
55
56 1
        $this->booted = true;
57 1
    }
58
59
    /**
60
     * Creating the instances of bundle dependencies.
61
     *
62
     * @param array $dependencies
63
     *
64
     * @return bool Has new instances or not.
65
     */
66 1
    protected function createBundles(array $dependencies)
67
    {
68 1
        foreach ($dependencies as $bundleClass) {
69 1
            $name = substr($bundleClass, strrpos($bundleClass, '\\') + 1);
70
71 1
            if (false === isset($this->bundles[$name])) {
72 1
                $bundle = new $bundleClass();
73 1
                $this->bundles[$name] = $bundleClass;
74 1
                $this->instances[$name] = $bundle;
75
76 1
                if ($bundle instanceof BundleDependencyInterface) {
77 1
                    $this->createBundles($bundle->getBundleDependencies());
78 1
                }
79 1
            }
80 1
        }
81
82 1
        return count($this->instances) > 0;
83
    }
84
85
    /**
86
     * @param ContainerBuilder $container
87
     */
88 1
    protected function initializeBundles(ContainerBuilder $container)
89
    {
90 1
        foreach ($this->instances as $bundle) {
91 1
            if ($extension = $bundle->getContainerExtension()) {
92 1
                $container->registerExtension($extension);
93 1
            }
94
95 1
            $bundle->build($container);
96 1
        }
97
98 1
        foreach ($this->instances as $bundle) {
99 1
            $bundle->setContainer($container);
100 1
            $bundle->boot();
101 1
        }
102 1
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    abstract public function getBundleDependencies();
108
}
109