Completed
Push — master ( 21914d...538c14 )
by Dmitry
44s
created

BundleDependency.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SymfonyBundles\BundleDependency;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
7
8
trait BundleDependency
9
{
10
11
    /**
12
     * @var bool
13
     */
14
    private $booted = false;
15
16
    /**
17
     * @var array
18
     */
19
    private $bundles = [];
20
21
    /**
22
     * @var array
23
     */
24
    private $instances = [];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function build(ContainerBuilder $container)
30
    {
31 2
        $this->registerBundleDependencies($container);
32 2
    }
33
34
    /**
35
     * Register the bundle dependencies.
36
     *
37
     * @param ContainerBuilder $container
38
     *
39
     * @return void
40
     */
41 2
    protected function registerBundleDependencies(ContainerBuilder $container)
42
    {
43 2
        if (true === $this->booted) {
44 2
            return;
45
        }
46
47 2
        $this->bundles = $container->getParameter('kernel.bundles');
0 ignored issues
show
Documentation Bug introduced by
It seems like $container->getParameter('kernel.bundles') of type * is incompatible with the declared type array of property $bundles.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
49 2
        $this->createBundles($this->getBundleDependencies());
50
51 2
        $container->setParameter('kernel.bundles', $this->bundles);
52
53 2
        $this->initializeBundles($container);
54
55 2
        $this->booted = true;
56 2
    }
57
58
    /**
59
     * Creating the instances of bundle dependencies.
60
     *
61
     * @param array $dependencies
62
     *
63
     * @return void
64
     */
65 2
    protected function createBundles(array $dependencies)
66
    {
67 2
        foreach ($dependencies as $bundleClass) {
68 2
            $name = substr($bundleClass, strrpos($bundleClass, '\\') + 1);
69
70 2
            if (false === isset($this->bundles[$name])) {
71 2
                $bundle = new $bundleClass();
72 2
                $this->bundles[$name] = $bundleClass;
73 2
                $this->instances[$name] = $bundle;
74
75 2
                if ($bundle instanceof BundleDependencyInterface) {
76 2
                    $this->createBundles($bundle->getBundleDependencies());
77 2
                }
78 2
            }
79 2
        }
80 2
    }
81
82
    /**
83
     * @param ContainerBuilder $container
84
     *
85
     * @return void
86
     */
87 2
    protected function initializeBundles(ContainerBuilder $container)
88
    {
89 2
        $extensions = [];
90
91 2
        foreach ($this->instances as $bundle) {
92 2
            if ($extension = $bundle->getContainerExtension()) {
93 2
                $container->registerExtension($bundle->getContainerExtension());
94 2
                $extensions[] = $extension->getAlias();
95 2
            }
96
97 2
            $bundle->build($container);
98 2
        }
99
100 2
        $container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));
101
102 2
        foreach ($this->instances as $bundle) {
103 2
            $bundle->setContainer($container);
104 2
            $bundle->boot();
105 2
        }
106 2
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    abstract public function getBundleDependencies();
112
}
113