Test Failed
Pull Request — master (#6)
by Dmitry
02:57
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 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
    public function build(ContainerBuilder $container)
29 1
    {
30
        $this->registerBundleDependencies($container);
31 1
    }
32 1
33
    /**
34
     * Register the bundle dependencies.
35
     *
36
     * @param ContainerBuilder $container
37
     */
38
    protected function registerBundleDependencies(ContainerBuilder $container)
39
    {
40
        if (true === $this->booted) {
41 1
            return;
42
        }
43 1
44 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...
45
46
        if ($this->createBundles($this->getBundleDependencies())) {
47 1
            $container->setParameter('kernel.bundles', $this->bundles);
48
49 1
            $this->initializeBundles($container);
50 1
51
            $pass = new Compiler\ExtensionLoadPass($this->instances);
52 1
53
            $container->addCompilerPass($pass);
54 1
        }
55
56 1
        $this->booted = true;
57 1
    }
58
59 1
    /**
60 1
     * Creating the instances of bundle dependencies.
61
     *
62
     * @param array $dependencies
63
     *
64
     * @return bool Has new instances or not.
65
     */
66
    protected function createBundles(array $dependencies)
67
    {
68
        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
                $this->bundles[$name] = $bundleClass;
74 1
                $this->instances[$name] = $bundle;
75 1
76 1
                if ($bundle instanceof BundleDependencyInterface) {
77 1
                    $this->createBundles($bundle->getBundleDependencies());
78
                }
79 1
            }
80 1
        }
81 1
82 1
        return count($this->instances) > 0;
83 1
    }
84
85 1
    /**
86
     * @param ContainerBuilder $container
87
     */
88
    protected function initializeBundles(ContainerBuilder $container)
89
    {
90
        foreach ($this->instances as $bundle) {
91
            if ($extension = $bundle->getContainerExtension()) {
92
                $container->registerExtension($extension);
93 1
            }
94
95 1
            $bundle->build($container);
96 1
        }
97 1
98 1
        foreach ($this->instances as $bundle) {
99
            $bundle->setContainer($container);
100 1
            $bundle->boot();
101 1
        }
102
    }
103 1
104 1
    /**
105 1
     * {@inheritdoc}
106 1
     */
107 1
    abstract public function getBundleDependencies();
108
}
109