Completed
Push — master ( 0e7da4...73a980 )
by Rougin
02:07
created

Kernel::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Zapheus\Bridge\Symfony;
4
5
use Symfony\Component\Config\Loader\LoaderInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
8
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
9
use Zapheus\Provider\ConfigurationInterface;
10
11
/**
12
 * Kernel
13
 *
14
 * @package Zapheus
15
 * @author  Rougin Royce Gutib <[email protected]>
16
 */
17
class Kernel extends HttpKernel
18
{
19
    /**
20
     * @var \Zapheus\Provider\ConfigurationInterface
21
     */
22
    protected $configuration;
23
24
    /**
25
     * @var \Symfony\Component\HttpKernel\Bundle\BundleInterface[]
26
     */
27
    protected $items = array();
28
29
    /**
30
     * Initializes the kernel instance.
31
     *
32
     * @param \Symfony\Component\HttpKernel\Bundle\BundleInterface[] $bundles
33
     * @param \Zapheus\Provider\ConfigurationInterface               $configuration
34
     */
35 3
    public function __construct(array $bundles, ConfigurationInterface $configuration)
36
    {
37 3
        $this->configuration = $configuration;
38
39 3
        $parameters = $configuration->get('symfony', array(), true);
40
41 3
        $parameters = $this->defaults((array) $parameters);
42
43 3
        $this->debug = (boolean) $parameters['kernel.debug'];
44
45 3
        $this->environment = $parameters['kernel.environment'];
46
47 3
        $this->rootDir = $parameters['kernel.root_dir'];
48
49 3
        $this->name = $parameters['kernel.name'];
50
51 3
        $this->debug && $this->startTime = microtime(true);
52
53 3
        $this->items = $bundles;
54 3
    }
55
56
    /**
57
     * Returns an array of bundles to register.
58
     *
59
     * @return \Symfony\Component\HttpKernel\Bundle\BundleInterface[]
60
     */
61 3
    public function registerBundles()
62
    {
63 3
        return (array) $this->items;
64
    }
65
66
    /**
67
     * Loads the container configuration.
68
     *
69
     * @param  \Symfony\Component\Config\Loader\LoaderInterface $loader
70
     * @return void
71
     */
72 3
    public function registerContainerConfiguration(LoaderInterface $loader)
73
    {
74 3
        $configuration = $this->configuration;
75
76 3
        $loader->load(function ($container) use ($configuration) {
77 3
            $items = $configuration->get('symfony', array(), true);
78
79 3
            foreach ((array) $items as $key => $value) {
80 3
                $exists = $container->hasParameter($key) === true;
81
82 3
                $exists || $container->setParameter($key, $value);
83 3
            }
84 3
        });
85 3
    }
86
87
    /**
88
     * Returns an array of default kernel parameters.
89
     *
90
     * @param  array $config
91
     * @return array
92
     */
93 3
    protected function defaults(array $config)
94
    {
95 3
        $items = array('kernel.debug' => true);
96
97 3
        $items['kernel.environment'] = 'dev';
98
99 3
        $items['kernel.root_dir'] = $this->getRootDir();
100
101 3
        $items['kernel.name'] = $this->getName();
102
103 3
        foreach ((array) $items as $key => $value) {
104 3
            $exists = isset($config[$key]);
105
106 3
            $exists || $config[$key] = $value;
107 3
        }
108
109 3
        return $config;
110
    }
111
}
112