|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Config |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Config\Loader; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Shared\Reader\Reader; |
|
18
|
|
|
use Phossa2\Config\Message\Message; |
|
19
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
|
20
|
|
|
use Phossa2\Config\Exception\InvalidArgumentException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* ConfigFileLoader |
|
24
|
|
|
* |
|
25
|
|
|
* Read configs from file. |
|
26
|
|
|
* |
|
27
|
|
|
* @package Phossa2\Config |
|
28
|
|
|
* @author Hong Zhang <[email protected]> |
|
29
|
|
|
* @version 2.0.8 |
|
30
|
|
|
* @since 2.0.0 added |
|
31
|
|
|
* @since 2.0.8 updated |
|
32
|
|
|
*/ |
|
33
|
|
|
class ConfigFileLoader extends ObjectAbstract implements ConfigLoaderInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Config root directory |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
* @access protected |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $root_dir; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* config file type |
|
45
|
|
|
* |
|
46
|
|
|
* @var string |
|
47
|
|
|
* @access protected |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $file_type; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* cached subdirs to load files |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
* @access protected |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $sub_dirs = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* default environment |
|
61
|
|
|
* |
|
62
|
|
|
* @var string |
|
63
|
|
|
* @access protected |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $environment; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Constructor |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $rootDir |
|
71
|
|
|
* @param string $environment |
|
72
|
|
|
* @param string $fileType |
|
73
|
|
|
* @throws InvalidArgumentException if any argument invalid |
|
74
|
|
|
* @access public |
|
75
|
|
|
* @api |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __construct( |
|
78
|
|
|
/*# string */ $rootDir, |
|
79
|
|
|
/*# string */ $environment = '', |
|
80
|
|
|
/*# string */ $fileType = 'php' |
|
81
|
|
|
) { |
|
82
|
|
|
$this |
|
83
|
|
|
->setRootDir($rootDir) |
|
84
|
|
|
->setFileType($fileType) |
|
85
|
|
|
->setEnvironment($environment); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritDoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function load( |
|
92
|
|
|
/*# string */ $group, |
|
93
|
|
|
/*# string */ $environment = '' |
|
94
|
|
|
)/*# : array */ { |
|
95
|
|
|
$data = []; |
|
96
|
|
|
$env = $environment ?: $this->environment; |
|
97
|
|
|
|
|
98
|
|
|
foreach ($this->globFiles($group, $env) as $file) { |
|
99
|
|
|
$grp = basename($file, '.' . $this->file_type); |
|
100
|
|
|
if (!isset($data[$grp])) { |
|
101
|
|
|
$data[$grp] = []; |
|
102
|
|
|
} |
|
103
|
|
|
$data[$grp] = array_replace_recursive( |
|
104
|
|
|
$data[$grp], |
|
105
|
|
|
(array) Reader::readFile($file) |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
return $data; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Set config file root directory |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $rootDir |
|
115
|
|
|
* @return $this |
|
116
|
|
|
* @throws InvalidArgumentException if root dir is unknown |
|
117
|
|
|
* @access public |
|
118
|
|
|
* @api |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setRootDir(/*# string */ $rootDir) |
|
121
|
|
|
{ |
|
122
|
|
|
$dir = realpath($rootDir); |
|
123
|
|
|
|
|
124
|
|
|
if (false === $dir) { |
|
125
|
|
|
throw new InvalidArgumentException( |
|
126
|
|
|
Message::get(Message::CONFIG_ROOT_INVALID, $rootDir), |
|
127
|
|
|
Message::CONFIG_ROOT_INVALID |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->root_dir = $dir . \DIRECTORY_SEPARATOR; |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Set config file type |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $fileType |
|
140
|
|
|
* @return $this |
|
141
|
|
|
* @throws InvalidArgumentException if unsupported file type |
|
142
|
|
|
* @access public |
|
143
|
|
|
* @api |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setFileType(/*# string */ $fileType) |
|
146
|
|
|
{ |
|
147
|
|
|
if (!Reader::isSupported($fileType)) { |
|
148
|
|
|
throw new InvalidArgumentException( |
|
149
|
|
|
Message::get(Message::CONFIG_FILE_TYPE_UNKNOWN, $fileType), |
|
150
|
|
|
Message::CONFIG_FILE_TYPE_UNKNOWN |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$this->file_type = $fileType; |
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Set default environment |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $environment |
|
162
|
|
|
* @return $this |
|
163
|
|
|
* @access public |
|
164
|
|
|
* @api |
|
165
|
|
|
*/ |
|
166
|
|
|
public function setEnvironment(/*# string */ $environment) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->environment = $environment; |
|
169
|
|
|
$this->getSearchDirs($environment); |
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Returns an array of files to read from |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $group |
|
177
|
|
|
* @param string $environment |
|
178
|
|
|
* @return array |
|
179
|
|
|
* @access protected |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function globFiles( |
|
182
|
|
|
/*# string */ $group, |
|
183
|
|
|
/*# string */ $environment |
|
184
|
|
|
)/*# : array */ { |
|
185
|
|
|
$files = []; |
|
186
|
|
|
$group = '' === $group ? '*' : $group; |
|
187
|
|
|
foreach ($this->getSearchDirs($environment) as $dir) { |
|
188
|
|
|
$file = $dir . $group . '.' . $this->file_type; |
|
189
|
|
|
$files = array_merge($files, glob($file)); |
|
190
|
|
|
} |
|
191
|
|
|
return $files; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Get the search directories |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $env |
|
198
|
|
|
* @return array |
|
199
|
|
|
* @access protected |
|
200
|
|
|
*/ |
|
201
|
|
|
protected function getSearchDirs(/*# string */ $env)/*# : array */ |
|
202
|
|
|
{ |
|
203
|
|
|
if (!isset($this->sub_dirs[$env])) { |
|
204
|
|
|
$this->sub_dirs[$env] = $this->buildSearchDirs($env); |
|
205
|
|
|
} |
|
206
|
|
|
return $this->sub_dirs[$env]; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Build search directories |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $env |
|
213
|
|
|
* @return array |
|
214
|
|
|
* @access protected |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function buildSearchDirs(/*# string */ $env)/*# : array */ |
|
217
|
|
|
{ |
|
218
|
|
|
$path = $this->root_dir; |
|
219
|
|
|
$part = preg_split( |
|
220
|
|
|
'/[\/\\\]/', |
|
221
|
|
|
trim($env, '/\\'), |
|
222
|
|
|
0, |
|
223
|
|
|
\PREG_SPLIT_NO_EMPTY |
|
224
|
|
|
); |
|
225
|
|
|
$subdirs = [$path]; |
|
226
|
|
|
foreach ($part as $dir) { |
|
227
|
|
|
$path .= $dir . \DIRECTORY_SEPARATOR; |
|
228
|
|
|
if (false === file_exists($path)) { |
|
229
|
|
|
trigger_error( |
|
230
|
|
|
Message::get(Message::CONFIG_ENV_UNKNOWN, $env), |
|
231
|
|
|
\E_USER_WARNING |
|
232
|
|
|
); |
|
233
|
|
|
break; |
|
234
|
|
|
} |
|
235
|
|
|
$subdirs[] = $path; |
|
236
|
|
|
} |
|
237
|
|
|
return $subdirs; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|