Completed
Pull Request — master (#380)
by Anton
05:26
created

Config   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 66.67%

Importance

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

7 Methods

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