Completed
Push — master ( 8917e7...cbb910 )
by Dmitry
41s
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
7
trait BundleDependency
8
{
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
     * @return void
39
     */
40 1
    protected function registerBundleDependencies(ContainerBuilder $container)
41
    {
42 1
        if (true === $this->booted) {
43 1
            return;
44
        }
45
46 1
        $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...
47
48 1
        $this->createBundles($this->getBundleDependencies());
49
50 1
        $container->setParameter('kernel.bundles', $this->bundles);
51
52 1
        $this->initializeBundles($container);
53
54 1
        $this->booted = true;
55 1
    }
56
57
    /**
58
     * Creating the instances of bundle dependencies.
59
     *
60
     * @param array $dependencies
61
     *
62
     * @return void
63
     */
64 1
    protected function createBundles(array $dependencies)
65
    {
66 1
        foreach ($dependencies as $bundleClass) {
67 1
            $name = substr($bundleClass, strrpos($bundleClass, '\\') + 1);
68
69 1
            if (false === isset($this->bundles[$name])) {
70 1
                $bundle = new $bundleClass();
71 1
                $this->bundles[$name] = $bundleClass;
72 1
                $this->instances[$name] = $bundle;
73
74 1
                if ($bundle instanceof BundleDependencyInterface) {
75 1
                    $this->createBundles($bundle->getBundleDependencies());
76 1
                }
77 1
            }
78 1
        }
79 1
    }
80
81
    /**
82
     * @param ContainerBuilder $container
83
     *
84
     * @return void
85
     */
86 1
    protected function initializeBundles(ContainerBuilder $container)
87
    {
88 1
        foreach ($this->instances as $bundle) {
89 1
            if ($extension = $bundle->getContainerExtension()) {
90 1
                $container->registerExtension($extension);
91
92 1
                $extension->load([], $container);
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