1: <?php
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: namespace Simpletools\Config;
41:
42: use Simpletools\Config;
43:
44: class Ini
45: {
46:
47: protected $_ini = '';
48: protected $_section = false;
49: protected $_params = false;
50: protected $_settings = null;
51:
52: protected static $__instance;
53:
54: public function __construct(array $settings=null)
55: {
56: $settings['uri_mvc_app_position'] = isset($settings['uri_mvc_app_position']) ? $settings['uri_mvc_app_position'] : 0;
57: $settings['section'] = isset($settings['section']) ? $settings['section'] : false;
58:
59: $this->_settings = $settings;
60:
61: if(isset($settings['pathToIniFile']))
62: {
63: $this->setIniLocation($settings['pathToIniFile'],$settings['section']);
64: }
65:
66:
67: elseif(isset($settings['path_to_file']))
68: {
69: $this->setIniLocation($settings['path_to_file'],$settings['section']);
70: }
71:
72: if (empty(self::$__instance))
73: {
74: self::$__instance = &$this;
75: }
76:
77: }
78:
79: public static function &settings(array $settings)
80: {
81: if(empty(self::$__instance))
82: {
83: new Ini($settings);
84: }
85:
86: $settings['uri_mvc_app_position'] = isset($settings['uri_mvc_app_position']) ? $settings['uri_mvc_app_position'] : 0;
87: $settings['section'] = isset($settings['section']) ? $settings['section'] : false;
88:
89: self::$__instance->_settings = $settings;
90:
91: if(isset($settings['pathToIniFile']))
92: {
93: self::$__instance->setIniLocation($settings['pathToIniFile'],$settings['section']);
94: }
95:
96:
97: elseif(isset($settings['path_to_file']))
98: {
99: self::$__instance->setIniLocation($settings['path_to_file'],$settings['section']);
100: }
101:
102: return self::$__instance;
103: }
104:
105: public static function &getInstance($settings=false)
106: {
107: if (empty(self::$__instance))
108: {
109: self::$__instance = new SimpleIni($settings);
110: }
111:
112: return self::$__instance;
113: }
114:
115: public function setIniLocation($iniDir,$section=false)
116: {
117: $this->_iniDir = $iniDir;
118:
119: if(!file_exists($iniDir))
120: {
121: throw new Exception('Wrong file location: '.$this->_iniDir);
122: }
123:
124: $this->_ini = @parse_ini_file($iniDir, true);
125: $this->_iniMvc = $this->_ini;
126:
127: foreach($this->_iniMvc as $section => $value)
128: {
129: $ns = 0;
130:
131: if(isset($this->_settings['activeRoutingNamespace']) && $this->_settings['activeRoutingNamespace'])
132: {
133: $ns = $this->_settings['activeRoutingNamespace'];
134: $ns = explode('\\',$ns);
135: $ns = count($ns);
136: }
137:
138: foreach($value as $field => $val)
139: {
140: if(stripos($field,'.')!==false)
141: {
142: $length = count(explode('.',$field));
143: if($length!=$ns+2)
144: {
145: unset($value[$field]);
146: }
147: }
148: else
149: {
150: $value[$field] = $val;
151: }
152: }
153:
154: $this->_iniMvc[$section] = $this->_parseIniSections($value);
155: }
156:
157: if($section)
158: {
159: $this->_section = $section;
160: }
161: }
162:
163: protected function _parseIniSections($fields)
164: {
165: $next = array(); $arr = array();
166:
167: foreach($fields as $field => $value)
168: {
169: if(stripos($field,'.')!==false)
170: {
171: $field = explode('.',$field);
172:
173: $f0 = $field[0];
174: unset($field[0]);
175:
176: if(count($field)<2)
177: {
178: $array = array(implode('.',$field) => $value);
179:
180: if(!isset($arr[$f0]))
181: {
182: $arr[$f0] = $this->_parseIniSections($array);
183: }
184: else
185: {
186: $arr[$f0] = array_merge($arr[$f0],$this->_parseIniSections($array));
187: }
188: }
189: else
190: {
191:
192: $next[$f0][implode('.',$field)] = $value;
193: }
194: }
195: else
196: {
197: $arr[$field] = $value;
198: }
199: }
200:
201: if(count($next))
202: {
203: foreach($next as $field => $fields)
204: {
205: if(!isset($arr[$field]))
206: {
207: $arr[$field] = $this->_parseIniSections($fields);
208: }
209: else
210: {
211: $arr[$field] = array_merge($arr[$field],$this->_parseIniSections($fields));
212: }
213: }
214: }
215:
216: return $arr;
217: }
218:
219: public function setSection($section)
220: {
221: $this->_section = $section;
222: }
223:
224: public function getTree($section=false)
225: {
226: if(!$section)
227: {
228: return $this->_ini;
229: }
230: else
231: {
232: if(!isset($this->_ini[$section]))
233: {
234: throw new Exception('There is no '.$section.' section in '.$this->_iniDir);
235: }
236: return $this->_ini[$section];
237: }
238: }
239:
240: public function getValue($name,$section=false)
241: {
242: if(!$section)
243: {
244: if(!$this->_section)
245: {
246: throw new Exception('Please define section first');
247: }
248:
249: $section = $this->_section;
250: }
251:
252: return isset($this->_ini[$section][$name]) ? $this->_ini[$section][$name] : null;
253: }
254:
255: public function setactiveRoutingNamespace($namespace)
256: {
257: $this->_settings['activeRoutingNamespace'] = $namespace;
258: }
259:
260: public function getMvcValue($section=false)
261: {
262: $params = $this->getMvcParams();
263:
264: if(is_array($section))
265: {
266: if(isset($section['section'])) $section = $section['section'];
267: else $section = $this->_section;
268: }
269: else if($section === false)
270: {
271: $section = $this->_section;
272: }
273:
274: $ini = $this->_iniMvc[$section];
275: $match = null;
276:
277: foreach($params as $param)
278: {
279: if(isset($ini[$param]) OR isset($ini['*']))
280: {
281: $match = isset($ini[$param]) ? $ini[$param] : $ini['*'];
282:
283: if(is_array($match))
284: {
285: $ini = $match;
286: }
287: elseif(!$match)
288: {
289: $match = null;
290: break;
291: }
292: else
293: {
294: $ini = array();
295: }
296: }
297: else
298: {
299: $match = null;
300: break;
301: }
302: }
303:
304: if(is_array($match)) $match = null;
305:
306: return $match;
307: }
308:
309: public function getMvcParams()
310: {
311: $ns = 0;
312: $params = array();
313:
314: if(!$this->_params)
315: {
316: if($_SERVER['REQUEST_URI'] != '/')
317: {
318: $params = explode('/',trim($_SERVER['REQUEST_URI'],'/'));
319:
320: if($this->_settings['uri_mvc_app_position'] > 0)
321: {
322: for($i=0; $i<$this->_settings['uri_mvc_app_position']; $i++)
323: {
324: array_shift($params);
325: }
326: }
327: elseif(isset($this->_settings['activeRoutingNamespace']) && $this->_settings['activeRoutingNamespace'])
328: {
329: $ns = $this->_settings['activeRoutingNamespace'];
330: $ns = explode('\\',$ns);
331: $ns = count($ns);
332: }
333:
334: if(count($params) > 0)
335: {
336: $index = count($params)-1;
337: if(($position = strpos($params[$index],'?')) !== false)
338: {
339: $params[$index] = substr($params[$index],0,$position);
340: if($params[$index] == '') $params[$index] = null;
341: }
342: }
343: }
344:
345: $params = array_slice($params,0,$ns+2);
346:
347: if(!isset($params[$ns])) {$params[$ns] = 'index'; $params[$ns+1] = 'index';}
348: else if(!isset($params[$ns+1])) {$params[$ns+1] = 'index';}
349:
350: $this->_params = $params;
351: }
352:
353: return $this->_params;
354: }
355:
356: }
357:
358: ?>