|
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\LogicException; |
|
21
|
|
|
use Phossa2\Config\Exception\InvalidArgumentException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* ConfigFileLoader |
|
25
|
|
|
* |
|
26
|
|
|
* Read configs from file. |
|
27
|
|
|
* |
|
28
|
|
|
* @package Phossa2\Config |
|
29
|
|
|
* @author Hong Zhang <[email protected]> |
|
30
|
|
|
* @version 2.0.0 |
|
31
|
|
|
* @since 2.0.0 added |
|
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
|
|
|
* Constructor |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $rootDir |
|
63
|
|
|
* @param string $environment |
|
64
|
|
|
* @param string $fileType |
|
65
|
|
|
* @throws InvalidArgumentException if any argument invalid |
|
66
|
|
|
* @access public |
|
67
|
|
|
* @api |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct( |
|
70
|
|
|
/*# string */ $rootDir, |
|
71
|
|
|
/*# string */ $environment = '', |
|
72
|
|
|
/*# string */ $fileType = 'php' |
|
73
|
|
|
) { |
|
74
|
|
|
$this |
|
75
|
|
|
->setRootDir($rootDir) |
|
76
|
|
|
->setFileType($fileType) |
|
77
|
|
|
->setEnvironment($environment); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritDoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public function load( |
|
84
|
|
|
/*# string */ $group, |
|
85
|
|
|
$environment = null |
|
86
|
|
|
)/*# : array */ { |
|
87
|
|
|
$data = []; |
|
88
|
|
|
foreach ($this->globFiles($group, $environment) as $file) { |
|
89
|
|
|
$grp = basename($file, '.' . $this->file_type); |
|
90
|
|
|
if (!isset($data[$grp])) { |
|
91
|
|
|
$data[$grp] = []; |
|
92
|
|
|
} |
|
93
|
|
|
try { |
|
94
|
|
|
$data[$grp] = array_replace_recursive( |
|
95
|
|
|
$data[$grp], |
|
96
|
|
|
(array) Reader::readFile($file) |
|
97
|
|
|
); |
|
98
|
|
|
} catch (\Exception $e) { |
|
99
|
|
|
throw new LogicException($e->getMessage(), $e->getCode()); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
return $data; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Set config file root directory |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $rootDir |
|
109
|
|
|
* @return $this |
|
110
|
|
|
* @throws InvalidArgumentException if dir is bad |
|
111
|
|
|
* @access public |
|
112
|
|
|
* @api |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setRootDir(/*# string */ $rootDir) |
|
115
|
|
|
{ |
|
116
|
|
|
$dir = realpath($rootDir); |
|
117
|
|
|
if (false === $dir) { |
|
118
|
|
|
throw new InvalidArgumentException( |
|
119
|
|
|
Message::get(Message::CONFIG_ROOT_INVALID, $rootDir), |
|
120
|
|
|
Message::CONFIG_ROOT_INVALID |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$this->root_dir = $dir . \DIRECTORY_SEPARATOR; |
|
125
|
|
|
|
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Set config file type |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $fileType |
|
133
|
|
|
* @return $this |
|
134
|
|
|
* @throws InvalidArgumentException if unsupported file type |
|
135
|
|
|
* @access public |
|
136
|
|
|
* @api |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setFileType(/*# string */ $fileType) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->file_type = $fileType; |
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Set environment |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $environment |
|
148
|
|
|
* @return $this |
|
149
|
|
|
* @access public |
|
150
|
|
|
* @api |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setEnvironment(/*# string */ $environment) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->sub_dirs = $this->buildSearchDirs($environment); |
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns an array of files to read from |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $group |
|
162
|
|
|
* @param null|string $environment |
|
163
|
|
|
* @return array |
|
164
|
|
|
* @access protected |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function globFiles( |
|
167
|
|
|
/*# string */ $group, |
|
168
|
|
|
$environment |
|
169
|
|
|
)/*# : array */ { |
|
170
|
|
|
$files = []; |
|
171
|
|
|
$group = '' === $group ? '*' : $group; |
|
172
|
|
|
foreach($this->buildSearchDirs($environment) as $dir) { |
|
173
|
|
|
$file = $dir . $group . '.' . $this->file_type; |
|
174
|
|
|
$files = array_merge( $files, glob($file)); |
|
175
|
|
|
} |
|
176
|
|
|
return $files; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Build search directories |
|
181
|
|
|
* |
|
182
|
|
|
* @param null|string $environment |
|
183
|
|
|
* @return array |
|
184
|
|
|
* @access protected |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function buildSearchDirs($environment)/*# : array */ |
|
187
|
|
|
{ |
|
188
|
|
|
if (null === $environment) { |
|
189
|
|
|
return $this->sub_dirs; |
|
190
|
|
|
} else { |
|
191
|
|
|
$path = $this->root_dir; |
|
192
|
|
|
$subdirs = [$path]; |
|
193
|
|
|
$subs = preg_split('/[\/\\\]/', trim($environment, '/\\'), 0, |
|
194
|
|
|
\PREG_SPLIT_NO_EMPTY); |
|
195
|
|
|
|
|
196
|
|
|
foreach($subs as $dir) { |
|
197
|
|
|
$path = $path . $dir . \DIRECTORY_SEPARATOR; |
|
198
|
|
|
$subdirs[] = $path; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $subdirs; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|