| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 lmb_require('limb/fs/src/exception/lmbFileNotFoundException.class.php'); 10 lmb_require('limb/core/src/lmbSet.class.php'); 11 12 /** 13 * class lmbIni. 14 * 15 * @package config 16 * @version $Id: lmbIni.class.php 5945 2007-06-06 08:31:43Z pachanga $ 17 */ 18 class lmbIni extends lmbSet 19 { 20 protected $file_path; 21 22 function __construct($file) 23 { 24 $this->file_path = $file; 25 $this->_load(); 26 } 27 28 function getOverrideFile() 29 { 30 $file_name = substr($this->file_path, 0, strpos($this->file_path, '.ini')); 31 $override_file_name = $file_name . '.override' . '.ini'; 32 33 if(file_exists($override_file_name)) 34 return $override_file_name; 35 else 36 return false; 37 } 38 39 // returns the file_path 40 function getOriginalFile() 41 { 42 return $this->file_path; 43 } 44 45 protected function _load() 46 { 47 if(!file_exists($this->file_path)) 48 throw new lmbFileNotFoundException($this->file_path, 'ini file not found'); 49 $this->_parse($this->file_path); 50 } 51 52 protected function _parse() 53 { 54 $this->_parseFileContents($this->file_path); 55 56 if($override_file = $this->getOverrideFile()) 57 $this->_parseFileContents($override_file); 58 } 59 60 protected function _parseFileContents($file_path) 61 { 62 $this->_parseLines(file($file_path)); 63 } 64 65 protected function _parseLines($lines) 66 { 67 if($lines === false) 68 throw new lmbException('lmbIni file is not found or could not be loaded', array('path' => $this->file_path)); 69 70 $current_group = null; 71 72 if(count($lines) == 0) 73 return false; 74 75 foreach($lines as $line) 76 { 77 if(($line = trim($line)) == '') 78 continue; 79 // removing comments after #, not after # inside "" 80 81 $line = preg_replace('/([^"#]+|"(.*?)")|(#[^#]*)/', "\\1", $line); 82 // check for new group 83 if(preg_match("#^\[(.+)\]\s*$#", $line, $new_group_name_array)) 84 { 85 $new_group_name = trim($new_group_name_array[1]); 86 $current_group = $this->_parseConstants($new_group_name); 87 continue; 88 } 89 // check for variable 90 if(preg_match("#^([a-zA-Z0-9_-]+)(\[([a-zA-Z0-9_-]*)\]){0,1}(\s*)=(.*)$#", $line, $value_array)) 91 { 92 $var_name = trim($value_array[1]); 93 94 $var_value = trim($value_array[5]); 95 96 if(preg_match('/^"(.*)"$/', $var_value, $m)) 97 $var_value = $m[1]; 98 99 $var_value = $this->_parseConstants($var_value); 100 101 if($value_array[2])//check for array [] 102 { 103 if($value_array[3]) //check for hashed array, e.g ['test'] 104 { 105 $key_name = $value_array[3]; 106 $this->_addArrayIniValue($var_name, $var_value, $key_name, $current_group); 107 } 108 else 109 $this->_addArrayIniValue($var_name, $var_value, null, $current_group); 110 } 111 else 112 $this->_addIniValue($var_name, $var_value, $current_group); 113 } 114 } 115 } 116 117 protected function _addIniValue($name, $value, $group = null) 118 { 119 if($group) 120 { 121 if(!$this->has($group)) 122 $this->set($group, array()); 123 124 $group_array = $this->get($group); 125 $group_array[$name] = $value; 126 $this->set($group, $group_array); 127 } 128 else 129 $this->set($name, $value); 130 } 131 132 protected function _addArrayIniValue($name, $value, $index = null, $group = null) 133 { 134 if($group) 135 { 136 if(!$this->has($group)) 137 $this->set($group, array()); 138 139 $group_array = $this->get($group); 140 141 if($index) 142 $group_array[$name][$index] = $value; 143 else 144 $group_array[$name][] = $value; 145 146 $this->set($group, $group_array); 147 } 148 else 149 { 150 if(!$this->has($name)) 151 $this->set($name, array()); 152 153 $array = $this->get($name); 154 155 if($index) 156 $array[$index] = $value; 157 else 158 $array[] = $value; 159 160 $this->set($name, $array); 161 } 162 } 163 164 protected function _parseConstants($value) 165 { 166 return preg_replace('~\{([^\}]+)\}~e', "constant('\\1')", $value); 167 } 168 169 function getOption($var_name, $group_name = null) 170 { 171 if($group_name) 172 { 173 if($group = $this->get($group_name)) 174 { 175 if(is_array($group) && isset($group[$var_name])) 176 return $group[$var_name]; 177 } 178 } 179 else 180 return $this->get($var_name); 181 } 182 183 function assignOption(&$variable, $var_name, $group_name = null) 184 { 185 if(!$this->hasOption($var_name, $group_name)) 186 return false; 187 188 $variable = $this->getOption($var_name, $group_name); 189 return true; 190 } 191 192 function hasOption($var_name, $group_name = null) 193 { 194 if($group_name) 195 { 196 if($group = $this->get($group_name)) 197 return is_array($group) && array_key_exists($var_name, $group); 198 else 199 return false; 200 } 201 else 202 return $this->has($var_name); 203 } 204 205 function hasGroup($group_name) 206 { 207 return is_array($this->get($group_name)); 208 } 209 210 function getGroup($group_name) 211 { 212 return $this->get($group_name); 213 } 214 215 function getAll() 216 { 217 return $this->export(); 218 } 219 220 function mergeWith($ini) 221 { 222 $clone = clone($this); 223 $clone->merge($ini->export()); 224 return $clone; 225 } 226 } 227 228 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sun Oct 12 04:41:30 2008 | Cross-referenced by PHPXref 0.7 |