Completed
Push — master ( 480535...7e0bd3 )
by Anton
12s
created

Config::setEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
     *
49
     * @return void
50
     * @throws ConfigException
51
     */
52 3
    public function setPath($path)
53
    {
54 3
        if (!is_dir($path)) {
55 1
            throw new ConfigException('Configuration directory is not exists');
56
        }
57 2
        $this->path = rtrim($path, '/');
58 2
    }
59
60
    /**
61
     * Set application environment
62
     *
63
     * @param  string $environment
64
     *
65
     * @return void
66
     */
67 1
    public function setEnvironment($environment)
68
    {
69 1
        $this->environment = $environment;
70 1
    }
71
72
    /**
73
     * Load configuration
74
     *
75
     * @return void
76
     * @throws ConfigException
77
     */
78 5
    public function init()
79
    {
80 5
        if (!$this->path) {
81 1
            throw new ConfigException('Configuration directory is not setup');
82
        }
83
84 4
        $this->config = $this->loadFiles($this->path . '/configs/default');
85
86 4
        if ($this->environment) {
87 3
            $customConfig = $this->loadFiles($this->path . '/configs/' . $this->environment);
88 2
            $this->config = array_replace_recursive($this->config, $customConfig);
89
        }
90 3
    }
91
92
    /**
93
     * Load configuration file
94
     *
95
     * @param  string $path
96
     *
97
     * @return array
98
     * @throws ConfigException
99
     */
100 1
    protected function loadFile($path)
101
    {
102 1
        if (!is_file($path) && !is_readable($path)) {
103
            throw new ConfigException("Configuration file `$path` not found");
104
        }
105 1
        return include $path;
106
    }
107
108
    /**
109
     * Load configuration files to array
110
     *
111
     * @param  string $path
112
     *
113
     * @return array
114
     * @throws ConfigException
115
     */
116 1
    protected function loadFiles($path)
117
    {
118 1
        $config = [];
119
120 1
        if (!is_dir($path)) {
121
            throw new ConfigException("Configuration directory `$path` not found");
122
        }
123
124 1
        $iterator = new \GlobIterator(
125 1
            $path . '/*.php',
126 1
            \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::CURRENT_AS_PATHNAME
127
        );
128
129 1
        foreach ($iterator as $name => $file) {
130 1
            $name = substr($name, 0, -4);
131 1
            $config[$name] = $this->loadFile($file);
132
        }
133 1
        return $config;
134
    }
135
136
    /**
137
     * Return configuration by key
138
     *
139
     * @param array $keys
140
     *
141
     * @return array|mixed
142
     * @throws ConfigException
143
     */
144 738
    public function getData(...$keys)
145
    {
146
        // configuration is missed
147 738
        if (is_null($this->config)) {
148 1
            throw new ConfigException('System configuration is missing');
149
        }
150
151 738
        if (!count($keys)) {
152 2
            return $this->config;
153
        }
154
155 738
        return Collection::get($this->config, ...$keys);
156
    }
157
158
    /**
159
     * Return module configuration by section
160
     *
161
     * @param  string $module
162
     * @param  string $section
163
     *
164
     * @return mixed
165
     * @throws \Bluz\Config\ConfigException
166
     */
167 5
    public function getModuleData($module, $section = null)
168
    {
169 5
        if (!isset($this->modules[$module])) {
170 5
            $this->modules[$module] = $this->loadFile(
171 5
                $this->path . '/modules/' . $module . '/config.php'
172
            );
173
174 4
            if (is_null($this->config)) {
175 4
                $this->init();
176
            }
177
178 4
            if (isset($this->config["module.$module"])) {
179 1
                $this->modules[$module] = array_replace_recursive(
180 1
                    $this->modules[$module],
181 1
                    $this->config["module.$module"]
182
                );
183
            }
184
        }
185
186 4
        if (!is_null($section)) {
187 3
            return $this->modules[$module][$section] ?? null;
188
        }
189
190 1
        return $this->modules[$module];
191
    }
192
}
193