-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.php
378 lines (334 loc) · 8.69 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* Parse configuration files.
*
* @package BootstraPHP
* @subpackage Configuration
* @copyright 2012 Jan-Marten "Joh Man X" de Boer
*/
/**
* The configuration class.
*/
class Config {
/**
* An instance of Config.
*
* @var $_instance
*/
private static $_instance = null;
/**
* The configuration data.
*
* @var $_config
*/
private $_config = array();
/**
* The construct.
*/
private function __construct() {
$this->_doInternalCall();
$this->_config['config']['checkOnAddClass'] = false;
$this->_config['config']['non_constants'] = $this->peek();
}
/**
* Perform a magic check on the existence of a config.
*
* @param string $key the key to check
* @return boolean
*/
public function __isset($key) {
return $this->defined($key);
}
/**
* Do magical unsetting.
*
* @param string $key the key to unset
* @return void
*/
public function __unset($key) {
unset($this->_config[$key]);
}
/**
* Do magical declaration of data.
*
* @param string $key the key to set data to
* @param mixed $value the value to set
* @return void
*/
public function __set($key, $value) {
$data = array();
$data[$key] = $value;
$this->put($data);
}
/**
* Do magical getting of data.
*
* @param string $key the key to fetch data with.
* @return mixed
*/
public function __get($key) {
return $this->get($key);
}
/**
* Make the config available as a serialized string.
*
* @return string $config the config data.
*/
public function __toString() {
return serialize($this->get());
}
/**
* Restrict what part of the class can be serialized.
*
* @return array $config the config variable.
*/
public function __sleep() {
return array('_config');
}
/**
* Make sure the config is up to date.
*
* @return void
*/
public function __wakeup() {
$this->_doInternalCall();
}
/**
* Get an instance of self.
*
* Kudos to @doenietzomoeilijk
*
* @return object $_instance
*/
public static function getInstance() {
if (!isset(self::$_instance)) {
$classname = __CLASS__;
self::$_instance = new $classname;
}
return self::$_instance;
}
/**
* Checks if a file exists in the current path.
*
* @param string $file the file name
* @return boolean|string $file either the path or false
*/
public function fileInPath($file) {
if (file_exists($file)) {
return $file;
} else {
$paths = $this->get('include_path');
$globalPaths = explode(':', $paths['global_value']);
$localPaths = explode(':', $paths['local_value']);
$paths = array_unique(array_merge($globalPaths, $localPaths));
$paths = is_array($paths) ? $paths : array($paths);
foreach ($paths as $path) {
if (file_exists($path . $file)) {
return $path . $file;
}
}
}
return false;
}
/**
* Parse an INI file.
*
* @param string|resource $file the file name or handler or content
* @param boolean $return optional: return the parsed config
* @throws Exception on parse failure
* @return void|array
*/
public function parse($file, $return = false) {
if (is_resource($file)) {
@fseek($file, 0);
$tmp = @tempnam('/tmp', 'Config_parse_tmp_');
$stream = @fopen($tmp, 'w');
$realSize = @stream_copy_to_stream($file, $stream);
if ($realSize) {
$file = $tmp;
} else {
throw new Exception(
'Unknown error occured while trying to read given resource handler.'
);
}
}
$filename = $this->fileInPath($file);
if (is_string($file) && $filename) {
$config = parse_ini_file($filename, true, INI_SCANNER_NORMAL);
} elseif (is_string($file) && strrpos($file, ']')) {
$config = parse_ini_string($file, true, INI_SCANNER_NORMAL);
}
if (!isset($config) || $config === false || !is_array($config)) {
throw new Exception(
'Unknown error occured while parsing'
. ($filename !== false ? ' "' . $filename . '" as' : '') . ' INI.'
);
} else {
$this->put($config);
if ($filename !== false
&& !in_array($filename, $this->_config['config']['scanned_files'])
) {
$this->_config['config']['scanned_files'][] = $filename;
}
}
if ($return === true) {
return $config;
}
}
/**
* Do internal operations when a getter is called.
*
* @return void
*/
private function _doInternalCall() {
// Get ini settings.
if (count($this->_config) === 0) {
$this->_config = ini_get_all();
}
// Get loaded configs.
if (!isset($this->_config['config'])
|| !isset($this->_config['config']['scanned_files'])
) {
$this->_config['config']['scanned_files'] = array_merge(
array(trim(php_ini_loaded_file())),
array_map('trim', (array) explode(',', php_ini_scanned_files()))
);
}
// Get constants.
$this->_config['constants'] = get_defined_constants(true);
// Get extensions.
$this->_config['extensions'] = array_fill_keys(
get_loaded_extensions(),
array()
);
foreach ($this->_config['extensions'] as $ext => $funcs) {
$this->_config['extensions'][$ext] = get_extension_funcs($ext);
}
// Get included files.
// Note: Files included using the auto_prepend_file directive
// are not included in the returned array.
$this->_config['config']['included_files'] = get_included_files();
}
/**
* Read configuration data.
*
* @param mixed $key optional key or keys to read from the config
* @return mixed $config either an array or a scalar value
*/
public function get($key = array()) {
$this->_doInternalCall();
$config = array();
if (is_scalar($key) && $this->defined($key)) {
$config = $this->_config[$key];
} elseif (is_array($key) && count($key)) {
foreach ($key as $i => $k) {
if (is_string($i)) {
$config[$k] = $this->_config[$i][$k];
} else {
$config[$k] = $this->_config[$k];
}
}
} elseif (is_array($key)) {
$config = $this->_config;
}
return $config;
}
/**
* Put in specific configuration.
*
* @param array $config the configuration, expected in key => value pairs
* @return void
*/
public function put(array $config = array()) {
foreach ($config as $k => $v) {
if (!empty($k)) {
$cv = $this->get($k);
if (is_array($cv) && is_array($v)) {
$this->_config[$k] = array_merge($cv, $v);
} else {
$this->_config[$k] = $v;
}
}
}
}
/**
* Get all config or a specific set in JSON.
*
* @param mixed $key optional key or keys to read from the config
* @return string $config the config in JSON format
*/
public function getJSON($key = array()) {
return json_encode($this->get($key));
}
/**
* Check if a setting is defined.
*
* @param mixed $key the key or keys that will be checked
* @return boolean $defined
*/
public function defined($key) {
$this->_doInternalCall();
if (is_array($key)) {
foreach ($key as $k => $v) {
if (!array_key_exists($k, $this->_config)
|| !array_key_exists($v, $this->_config[$k])
) {
return false;
}
}
} else {
return array_key_exists($key, $this->_config);
}
return true;
}
/**
* Define any scalar config as a constant.
*
* @return void
*/
public function defineScalarConstants() {
$config = $this->get();
foreach ($config as $key => $value) {
if (is_scalar($value)
&& !defined($key)
&& !in_array($key, $this->_config['config']['non_constants'])
) {
define($key, $value);
}
}
}
/**
* List all the available config.
*
* @return array $config the defined config
*/
public function peek() {
$this->_doInternalCall();
$config = array_keys($this->_config);
return $config;
}
/**
* Add a class file to the known locations.
*
* @param string $name the name for the class
* @param string $file the file for the class
* @param boolean $checkfs wether to check if the file exists or not
* @return boolean $added when the file was added.
*/
public function addClass($name, $file, $checkfs = false) {
$args = func_get_args();
// Default value for $checkfs assumed.
if (!isset($args[2])) {
$checkfs = $this->checkOnAddClass;
}
if (!$checkfs || $this->fileInPath($file)) {
$this->put(
array(
'knownClassLocations' => array($name => $file)
)
);
return true;
}
return false;
}
}