ConfigurationProcessor::getResource()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Borobudur-Kernel package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Kernel\Processor;
12
13
use Borobudur\Config\Configuration;
14
use Borobudur\Config\FileLoader;
15
use Borobudur\DependencyInjection\ContainerBuilder;
16
use Borobudur\Kernel\Bundling\ExtensionManager;
17
use Borobudur\Kernel\Exception\RuntimeException;
18
use Borobudur\Kernel\KernelInterface;
19
use Borobudur\Kernel\KernelServiceLocator;
20
use Borobudur\Kernel\ResourceBuilder;
21
22
/**
23
 * @author      Iqbal Maulana <[email protected]>
24
 * @created     8/17/15
25
 */
26
class ConfigurationProcessor implements ProcessorInterface
27
{
28
    /**
29
     * @var ContainerBuilder
30
     */
31
    private $container;
32
33
    /**
34
     * @var KernelServiceLocator
35
     */
36
    private $serviceLocator;
37
38
    /**
39
     * @var array
40
     */
41
    private $envs = array();
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param array $envs
47
     */
48
    public function __construct(array $envs)
49
    {
50
        $this->envs = $envs;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function initialize(KernelInterface $kernel)
57
    {
58
        $this->container = $kernel->getContainer();
59
        $this->serviceLocator = $kernel->getServiceLocator();
60
        $env = $kernel->getEnvironmentManager()->getEnvInstance();
61
        $resolver = $this->container->getParameterBag()->getResolver();
62
63
        $configs = (array) $env->registerConfigurations();
64
        $configs = $this->getResource()->process(new FileLoader(), $configs);
65
66
        $this->loadParametersFromConfig($configs);
67
        $this->loadServicesFromConfig($configs);
68
69
        // Append configuration by extension
70
        foreach ($resolver->resolveValue($configs) as $alias => $config) {
71
            if (!$this->getExtension()->has($alias)) {
72
                throw new RuntimeException(sprintf('Extension with alias "%s" not found.', $alias));
73
            }
74
75
            $extension = $this->getExtension()->get($alias);
76
            $this->getConfiguration()->prepend($extension->getConfigDefinition(), array($config));
77
        }
78
    }
79
80
    /**
81
     * Load parameters from config.
82
     *
83
     * @param array $configs
84
     */
85 View Code Duplication
    private function loadParametersFromConfig(array &$configs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        if (isset($configs['parameters']) && is_array($configs['parameters'])) {
88
            $this->container->getParameterBag()->add(array_merge($configs['parameters'], $this->envs));
89
90
            unset($configs['parameters']);
91
        }
92
    }
93
94
    /**
95
     * Load services from config.
96
     *
97
     * @param array $configs
98
     */
99 View Code Duplication
    private function loadServicesFromConfig(array &$configs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        if (isset($configs['services']) && is_array($configs['services'])) {
102
            $this->container->load($configs['services']);
103
            unset($configs['services']);
104
        }
105
    }
106
107
    /**
108
     * @return ExtensionManager
109
     */
110
    private function getExtension()
111
    {
112
        return $this->serviceLocator->getExtension();
113
    }
114
115
    /**
116
     * @return ResourceBuilder
117
     */
118
    private function getResource()
119
    {
120
        return $this->serviceLocator->getResource();
121
    }
122
123
    /**
124
     * @return Configuration
125
     */
126
    private function getConfiguration()
127
    {
128
        return $this->serviceLocator->getConfiguration();
129
    }
130
}
131