Completed
Pull Request — master (#410)
by Anton
07:16
created

Config   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 64%

Importance

Changes 0
Metric Value
dl 0
loc 165
ccs 32
cts 50
cp 0.64
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 2

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