Overview

Namespaces

  • Simpletools
    • Autoload
    • Config
    • Db
      • Mysql
    • Event
    • Http
    • Mvc
    • Page
    • Store

Classes

  • Simpletools\Autoload\Loader
  • Simpletools\Config\Ini
  • Simpletools\Db\Mysql\Client
  • Simpletools\Db\Mysql\Iterator
  • Simpletools\Db\Mysql\Model
  • Simpletools\Db\Mysql\QueryBuilder
  • Simpletools\Db\Mysql\Result
  • Simpletools\Db\Mysql\Sql
  • Simpletools\Event\Event
  • Simpletools\Http\Ssl
  • Simpletools\Mvc\Common
  • Simpletools\Mvc\Controller
  • Simpletools\Mvc\Model
  • Simpletools\Mvc\Router
  • Simpletools\Mvc\RoutingHook
  • Simpletools\Mvc\View
  • Simpletools\Page\Layout
  • Simpletools\Store\Cookie
  • Simpletools\Store\Flash
  • Simpletools\Store\Session
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /*
  3:  * Simpletools Framework.
  4:  * Copyright (c) 2009, Marcin Rosinski. (http://www.getsimpletools.com)
  5:  * All rights reserved.
  6:  * 
  7:  * LICENCE
  8:  *
  9:  * Redistribution and use in source and binary forms, with or without modification, 
 10:  * are permitted provided that the following conditions are met:
 11:  *
 12:  * -    Redistributions of source code must retain the above copyright notice, 
 13:  *      this list of conditions and the following disclaimer.
 14:  * 
 15:  * -    Redistributions in binary form must reproduce the above copyright notice, 
 16:  *      this list of conditions and the following disclaimer in the documentation and/or other 
 17:  *      materials provided with the distribution.
 18:  * 
 19:  * -    Neither the name of the Simpletools nor the names of its contributors may be used to 
 20:  *      endorse or promote products derived from this software without specific prior written permission.
 21:  * 
 22:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
 23:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 24:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 25:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 27:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
 28:  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 29:  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 30:  * 
 31:  * @framework       Simpletools
 32:  * @packages        Registry
 33:  * @description     Simple ini files interpreter.
 34:  * @copyright       Copyright (c) 2009 Marcin Rosinski. (http://www.getsimpletools.com)
 35:  * @license         http://www.opensource.org/licenses/bsd-license.php - BSD
 36:  * @version         Ver: 2.0.6 2014-11-22 10:30
 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:             //depracted
 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:             //depracted
 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: ?>
API documentation generated by ApiGen