Completed
Push — feature/fix-somescrutinizer-is... ( 840c34...8bd6e3 )
by Narcotic
15:02 queued 08:41
created

BundleLoader::addBundle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3
Metric Value
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Loads bundles from list of bundles implementing GravitonBundleInterface
4
 */
5
6
namespace Graviton\BundleBundle\Loader;
7
8
use Graviton\BundleBundle\GravitonBundleBundle;
9
use Graviton\BundleBundle\GravitonBundleInterface;
10
11
/**
12
 * BundleLoader
13
 *
14
 * This class loads additional bundles if a module implements
15
 * GravitonBundleInterface.
16
 * It does not check for circularity so we need to take care of only loading
17
 * GravitonBundles through this in the rare exception case.
18
 *
19
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
20
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
21
 * @link     http://swisscom.ch
22
 */
23
class BundleLoader
24
{
25
    /**
26
     * stack used during loading of bundles
27
     *
28
     * @var \Symfony\Component\HttpKernel\Bundle\Bundle[]
29
     */
30
    protected $bundleStack = array();
31
32
    /**
33
     * final compilation of bundles
34
     *
35
     * @var \Symfony\Component\HttpKernel\Bundle\Bundle[]
36
     */
37
    protected $finalBundles = array();
38
39
    /**
40
     * create and kickstart BundleLoader
41
     *
42
     * @param GravitonBundleBundle $bundleBundle inject kickstart bundle here
43
     */
44 7
    public function __construct(GravitonBundleBundle $bundleBundle)
45 1
    {
46 7
        $this->bundleStack = array($bundleBundle);
47 7
    }
48
49
    /**
50
     * load bundles
51
     *
52
     * @param \Symfony\Component\HttpKernel\Bundle\Bundle[] $bundles pre-loaded bundles
53
     *
54
     * @return \Symfony\Component\HttpKernel\Bundle\Bundle[]
55
     */
56 7
    public function load(array $bundles)
57 1
    {
58 7
        $this->finalBundles = $bundles;
59
60 7
        while (!empty($this->bundleStack)) {
61 7
            $bundle = array_shift($this->bundleStack);
62 7
            $this->addBundle($bundle);
63 7
        }
64
65 7
        return $this->finalBundles;
66
    }
67
68
    /**
69
     * add bundles
70
     *
71
     * adds all bundles to finalBundles.
72
     * adds the results of getBundles to bundleStack if GravitonBundleInterface
73
     * was implemented.
74
     *
75
     * @param mixed $bundle various flavours of bundles
76
     *
77
     * @return void
78
     */
79 7
    private function addBundle($bundle)
80
    {
81 7
        if (!in_array($bundle, $this->finalBundles)) {
82 7
            if ($bundle instanceof GravitonBundleInterface) {
83 7
                $this->bundleStack = array_merge(
84 7
                    $this->bundleStack,
85 7
                    $bundle->getBundles()
86 7
                );
87 7
            }
88
89 7
            $this->finalBundles[] = $bundle;
90 7
        }
91 7
    }
92
}
93