-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIni.php
287 lines (259 loc) · 5.74 KB
/
Ini.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
<?php
/**
* Simple INI Configuration Wrapper
*
* @author Daniel �ekan <[email protected]>
* @version 1.0
**/
class Ini
{
/**
@var array $_config
**/
private $_config = array();
/**
@var mixed instance of Configuration or NULL
**/
private static $_instance;
/**
Construct method of Configuration class
@param array $config
*/
public function __construct($config = array())
{
$this->import($config);
}
/**
Easter for get instance of INI section
@method string $name section name
@param string $var config var
@param mixed $default default value from config
@return mixed
**/
public function __call($name, $arguments)
{
$var = isset($arguments[0])? $arguments[0] : NULL;
$default = isset($arguments[1])? $arguments[1] : NULL;
return $this->getSection( $name, $var, $default );
}
/**
Easter for static get INI section
@param string $name section name
@param string $arguments config var
@param mixed $default default value from config
@return mixed
**/
public static function __callStatic($name, $arguments)
{
$var = isset($arguments[0])? $arguments[0] : NULL;
$default = isset($arguments[1])? $arguments[1] : NULL;
return self::getInstance()->getSection( $name, $var, $default );
}
/**
Get static instance of configuration
@return \Configuration $_instance
**/
public static function getInstance($config = array())
{
if( self::$_instance === NULL ) {
self::$_instance = new self($config);
}
return self::$_instance;
}
/**
Export config
@return array
**/
public function export()
{
return $this->_config;
}
/**
Import config
@param $config
@return void
**/
public function import($config)
{
if( is_string($config) ) {
$config = parse_ini_string($config, true);
}
if($config) {
$this->_config = $config;
}
}
/**
Set INI config file
@param string $file
@return void
**/
public function addFile($file)
{
if( file_exists($file) )
{
$array = parse_ini_file($file, true);
$section = array();
foreach($array as $key => $row) {
$section[$key] = $this->_ini2extend($row);
}
$this->_config = array_merge($this->_config, $section);
return true;
}
return false;
}
/**
Get INI section
@param string $name section name
@param string $var config var
@return mixed
**/
public function getSection($name, $var = NULL, $default = NULL)
{
if (isset( $this->_config[$name] ) )
{
$section = $this->_config[$name];
if( $var ) {
return isset( $section[$var] )? $section[$var] : $default;
}
if( is_array($default) && $default ) {
$section = array_merge($default, $section);
}
return $section;
} elseif( strpos($name, ':') !== false )
{
// možný alias
list($alias, $type) = explode(':', $name);
foreach( array_keys($this->_config) as $key ) {
if( preg_match('/^'.preg_quote($alias).':(.*)$/', $key, $result) ) {
return $this->getSection($result[1].':'.$type, $var, $default);
}
}
}
return $default;
}
/**
Set configuration section to array
@param string $var config var
@param string $value config value
@param string $section config section
@return void
**/
public function set($var, $value, $section = NULL)
{
if( $section && isset( $this->_config[$section] ) )
{
$this->_config[$section][$var] = $value;
} else {
$this->_config[$var] = $value;
}
}
/**
Write configuration array to file
@param string $var config var
@param string $value config value
@param string $section config section
@return void
**/
public function save($file)
{
if (!defined('PHP_EOL')) {
switch (strtoupper(substr(PHP_OS, 0, 3))) {
// Windows
case 'WIN':
define('PHP_EOL', "\r\n");
break;
// Mac
case 'DAR':
define('PHP_EOL', "\r");
break;
// Unix
default:
define('PHP_EOL', "\n");
}
}
return file_put_contents($file, $this->_section2ini($this->_config));
}
public function __toString()
{
return $this->_section2ini($this->_config);
}
private function _ini2extend(array $a)
{
$out = array();
foreach ($a as $k => $v) {
if (is_array($v)) {
$a[$k] = $this->_ini2extend($v);
}
$x = explode('.', $k);
if (!empty($x[1])) {
$x = array_reverse($x, true);
if (isset($out[$k])) {
unset($out[$k]);
}
if (!isset($out[$x[0]])) {
$out[$x[0]] = array();
}
$first = true;
foreach ($x as $xv) {
if ($first === true) {
$b = $a[$k];
$first = false;
}
$b = array($xv => $b);
}
$out[$x[0]] = array_merge_recursive($out[$x[0]], $b[$x[0]]);
} elseif( is_string($a[$k]) && substr(trim($a[$k]), 0, 3) == 'new' ) {
$clazz = substr(trim($a[$k]), 4);
$out[$k] = new $clazz;
} else {
$out[$k] = $a[$k];
}
}
return $out;
}
private function _section2ini(array $a, $parent = NULL)
{
$out = '';
foreach ($a as $s => $c) {
$out .= '[' . $s . ']' . PHP_EOL;
$out .= $this->_array2ini($c);
$out .= PHP_EOL;
}
return $out;
}
private function _array2ini(array $a, $parent = NULL)
{
$out = '';
foreach ($a as $k => $v)
{
if( $parent && $this->_isArrSeq($a) && count($a) > 1 ) {
$k = $parent . '[]';
} elseif( $parent ) {
$k = $parent.'.'.$k;
}
if (is_array($v))
{
$out .= $this->_array2ini($v, $k);
} else {
$out .= $k." = " ;
if (is_numeric($v) || is_float($v)) {
$out .= "$v";
} elseif (is_bool($v)) {
$out .= ($v===true) ? 1 : 0;
} elseif (is_string($v)) {
$out .= "'".addcslashes($v, "'")."'";
} elseif (get_class($v)) {
$out .= "new ".get_class($v);
} else {
$out .= "$v";
}
$out .= PHP_EOL;
}
}
return $out;
}
private function _isArrSeq($a)
{
return array_keys($a) == range(0, count($a) - 1);
}
}