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