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