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