Completed
Push — master ( f4415a...651f7b )
by Anton
12s
created

ConfigLoader::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
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 ConfigLoader
23
{
24
    /**
25
     * @var array configuration data
26
     */
27
    protected $config;
28
29
    /**
30
     * @var string path to configuration files
31
     */
32
    protected $path;
33
34
    /**
35
     * @var string environment
36
     */
37
    protected $environment;
38
39
    /**
40
     * @return array
41
     */
42 604
    public function getConfig() : array
43
    {
44 604
        return $this->config;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getPath() : string
51
    {
52
        return $this->path;
53
    }
54
55
    /**
56
     * Set path to configuration files
57
     *
58
     * @param  string $path
59
     *
60
     * @return void
61
     * @throws ConfigException
62
     */
63 606
    public function setPath($path) : void
64
    {
65 606
        if (!is_dir($path)) {
66 1
            throw new ConfigException('Configuration directory is not exists');
67
        }
68 605
        $this->path = rtrim($path, '/');
69 605
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getEnvironment() : string
75
    {
76
        return $this->environment;
77
    }
78
79
    /**
80
     * Set application environment
81
     *
82
     * @param  string $environment
83
     *
84
     * @return void
85
     */
86 604
    public function setEnvironment($environment) : void
87
    {
88 604
        $this->environment = $environment;
89 604
    }
90
91
    /**
92
     * Load configuration
93
     *
94
     * @return void
95
     * @throws ConfigException
96
     */
97 608
    public function load() : void
98
    {
99 608
        if (!$this->path) {
100 1
            throw new ConfigException('Configuration directory is not setup');
101
        }
102
103 607
        $this->config = $this->loadFiles($this->path . '/configs/default');
104
105 607
        if ($this->environment) {
106 606
            $customConfig = $this->loadFiles($this->path . '/configs/' . $this->environment);
107 605
            $this->config = array_replace_recursive($this->config, $customConfig);
108
        }
109 606
    }
110
111
    /**
112
     * Load configuration file
113
     *
114
     * @param  string $path
115
     *
116
     * @return mixed
117
     * @throws ConfigException
118
     */
119 604
    protected function loadFile($path)
120
    {
121 604
        if (!is_file($path) && !is_readable($path)) {
122
            throw new ConfigException("Configuration file `$path` not found");
123
        }
124 604
        return include $path;
125
    }
126
127
    /**
128
     * Load configuration files to array
129
     *
130
     * @param  string $path
131
     *
132
     * @return array
133
     * @throws ConfigException
134
     */
135 604
    protected function loadFiles($path) : array
136
    {
137 604
        $config = [];
138
139 604
        if (!is_dir($path)) {
140
            throw new ConfigException("Configuration directory `$path` not found");
141
        }
142
143 604
        $iterator = new \GlobIterator(
144 604
            $path . '/*.php',
145 604
            \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::CURRENT_AS_PATHNAME
146
        );
147
148 604
        foreach ($iterator as $name => $file) {
149 604
            $name = substr($name, 0, -4);
150 604
            $config[$name] = $this->loadFile($file);
151
        }
152 604
        return $config;
153
    }
154
}
155