Completed
Push — master ( fd5d94...a7d085 )
by Anton
10s
created

Config   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 62.96%

Importance

Changes 0
Metric Value
dl 0
loc 176
ccs 34
cts 54
cp 0.6296
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setEnvironment() 0 4 1
A init() 0 13 3
A loadFile() 0 7 3
A loadFiles() 0 19 3
B getData() 0 24 5
B getModuleData() 0 25 5
A setPath() 0 7 2
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Config;
12
13
/**
14
 * Config
15
 *
16
 * @package  Bluz\Config
17
 * @author   Anton Shevchuk
18
 * @link     https://github.com/bluzphp/framework/wiki/Config
19
 */
20
class Config
21
{
22
    /**
23
     * @var array configuration data
24
     */
25
    protected $config;
26
27
    /**
28
     * @var array modules configuration data
29
     */
30
    protected $modules;
31
32
    /**
33
     * @var string path to configuration files
34
     */
35
    protected $path;
36
37
    /**
38
     * @var string environment
39
     */
40
    protected $environment;
41
42
    /**
43
     * Set path to configuration files
44
     *
45
     * @param  string $path
46
     * @return void
47
     * @throws ConfigException
48
     */
49 1
    public function setPath($path)
50
    {
51 1
        if (!is_dir($path)) {
52 1
            throw new ConfigException('Configuration directory is not exists');
53
        }
54
        $this->path = rtrim($path, '/');
55
    }
56
57
    /**
58
     * Set application environment
59
     *
60
     * @param  string $environment
61
     * @return void
62
     */
63
    public function setEnvironment($environment)
64
    {
65
        $this->environment = $environment;
66
    }
67
68
    /**
69
     * Load configuration
70
     *
71
     * @return void
72
     * @throws ConfigException
73
     */
74 3
    public function init()
75
    {
76 3
        if (!$this->path) {
77 1
            throw new ConfigException('Configuration directory is not setup');
78
        }
79
80 2
        $this->config = $this->loadFiles($this->path .'/configs/default');
81
82 2
        if ($this->environment) {
83 2
            $customConfig = $this->loadFiles($this->path . '/configs/' . $this->environment);
84 1
            $this->config = array_replace_recursive($this->config, $customConfig);
85
        }
86 1
    }
87
88
    /**
89
     * Load configuration file
90
     *
91
     * @param  string $path
92
     * @return array
93
     * @throws ConfigException
94
     */
95
    protected function loadFile($path)
96
    {
97
        if (!is_file($path) && !is_readable($path)) {
98
            throw new ConfigException("Configuration file `$path` not found");
99
        }
100
        return include $path;
101
    }
102
103
    /**
104
     * Load configuration files to array
105
     *
106
     * @param  string $path
107
     * @return array
108
     * @throws ConfigException
109
     */
110
    protected function loadFiles($path)
111
    {
112
        $config = [];
113
114
        if (!is_dir($path)) {
115
            throw new ConfigException("Configuration directory `$path` not found");
116
        }
117
118
        $iterator = new \GlobIterator(
119
            $path .'/*.php',
120
            \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::CURRENT_AS_PATHNAME
121
        );
122
123
        foreach ($iterator as $name => $file) {
124
            $name = substr($name, 0, -4);
125
            $config[$name] = $this->loadFile($file);
126
        }
127
        return $config;
128
    }
129
130
    /**
131
     * Return configuration by key
132
     *
133
     * @param  string|null $key     Key of config
134
     * @param  string|null $section Section of config
135
     * @return array|mixed
136
     * @throws ConfigException
137
     */
138 661
    public function getData($key = null, $section = null)
139
    {
140
        // configuration is missed
141 661
        if (is_null($this->config)) {
142 1
            throw new ConfigException('System configuration is missing');
143
        }
144
145
        // return all configuration
146 661
        if (is_null($key)) {
147 2
            return $this->config;
148
        }
149
150
        // return part of configuration
151 661
        if (isset($this->config[$key])) {
152
            // return section of configuration
153 661
            if (!is_null($section)) {
154 3
                return $this->config[$key][$section] ?? null;
155
            } else {
156 661
                return $this->config[$key];
157
            }
158
        } else {
159 2
            return null;
160
        }
161
    }
162
163
    /**
164
     * Return module configuration by section
165
     *
166
     * @param  string $module
167
     * @param  string $section
168
     * @return mixed
169
     */
170 5
    public function getModuleData($module, $section = null)
171
    {
172 5
        if (!isset($this->modules[$module])) {
173 5
            $this->modules[$module] = $this->loadFile(
174 5
                $this->path .'/modules/'. $module .'/config.php'
175
            );
176
177 4
            if (is_null($this->config)) {
178 4
                $this->init();
179
            }
180
181 4
            if (isset($this->config["module.$module"])) {
182 1
                $this->modules[$module] = array_replace_recursive(
183 1
                    $this->modules[$module],
184 1
                    $this->config["module.$module"]
185
                );
186
            }
187
        }
188
189 4
        if (!is_null($section)) {
190 3
            return $this->modules[$module][$section] ?? null;
191
        } else {
192 1
            return $this->modules[$module];
193
        }
194
    }
195
}
196