Source for file Config.php

Documentation is available at Config.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Config
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Collections/Hashtable.php';
  23. 23:  
  24. 24:  
  25. 25:  
  26. 26: /**
  27. 27:  * Configuration storage.
  28. 28:  *
  29. 29:  * @author     David Grudl
  30. 30:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  31. 31:  * @package    Nette\Config
  32. 32:  */
  33. 33: class Config extends Hashtable
  34. 34: {
  35. 35:     /**#@+ flag */
  36. 36:     const READONLY = 1;
  37. 37:     const EXPAND = 2;
  38. 38:     /**#@-*/
  39. 39:  
  40. 40:     /** @var array */
  41. 41:     private static $extensions array(
  42. 42:         'ini' => 'ConfigAdapterIni',
  43. 43:         'xml' => 'ConfigAdapterXml',
  44. 44:     );
  45. 45:  
  46. 46:  
  47. 47:  
  48. 48:     /**
  49. 49:      * Registers adapter for given file extension.
  50. 50:      * @param  string  file extension
  51. 51:      * @param  string  class name (IConfigAdapter)
  52. 52:      * @return void 
  53. 53:      */
  54. 54:     public static function registerExtension($extension$class)
  55. 55:     {
  56. 56:         if (!class_exists($class)) {
  57. 57:             throw new InvalidArgumentException("Class '$class' was not found.");
  58. 58:         }
  59. 59:  
  60. 60:         $reflection new ReflectionClass($class);
  61. 61:         if (!$reflection->implementsInterface('IConfigAdapter')) {
  62. 62:             throw new InvalidArgumentException("Configuration adapter '$class' is not Nette\\Config\\IConfigAdapter implementor.");
  63. 63:         }
  64. 64:  
  65. 65:         self::$extensions[strtolower($extension)$class;
  66. 66:     }
  67. 67:  
  68. 68:  
  69. 69:  
  70. 70:     /**
  71. 71:      * Creates new configuration object from file.
  72. 72:      * @param  string  file name
  73. 73:      * @param  string  section to load
  74. 74:      * @param  int     flags (readOnly, autoexpand variables)
  75. 75:      * @return Config 
  76. 76:      */
  77. 77:     public static function fromFile($file$section NULL$flags self::READONLY)
  78. 78:     {
  79. 79:         $extension strtolower(pathinfo($filePATHINFO_EXTENSION));
  80. 80:         if (isset(self::$extensions[$extension])) {
  81. 81:             $arr call_user_func(array(self::$extensions[$extension]'load')$file$section);
  82. 82:             return new self($arr$flags);
  83. 83:  
  84. 84:         else {
  85. 85:             throw new InvalidArgumentException("Unknown file extension '$file'.");
  86. 86:         }
  87. 87:     }
  88. 88:  
  89. 89:  
  90. 90:  
  91. 91:     /**
  92. 92:      * @param  array to wrap
  93. 93:      * @throws InvalidArgumentException
  94. 94:      */
  95. 95:     public function __construct($arr NULL$flags self::READONLY)
  96. 96:     {
  97. 97:         parent::__construct($arr);
  98. 98:  
  99. 99:         if ($arr !== NULL{
  100. 100:             if ($flags self::EXPAND{
  101. 101:                 $this->expand();
  102. 102:             }
  103. 103:  
  104. 104:             if ($flags self::READONLY{
  105. 105:                 $this->freeze();
  106. 106:             }
  107. 107:         }
  108. 108:     }
  109. 109:  
  110. 110:  
  111. 111:  
  112. 112:     /**
  113. 113:      * Save configuration to file.
  114. 114:      * @param  string  file
  115. 115:      * @param  string  section to write
  116. 116:      * @return void 
  117. 117:      */
  118. 118:     public function save($file$section NULL)
  119. 119:     {
  120. 120:         $extension strtolower(pathinfo($filePATHINFO_EXTENSION));
  121. 121:         if (isset(self::$extensions[$extension])) {
  122. 122:             return call_user_func(array(self::$extensions[$extension]'save')$this$file$section);
  123. 123:  
  124. 124:         else {
  125. 125:             throw new InvalidArgumentException("Unknown file extension '$file'.");
  126. 126:         }
  127. 127:     }
  128. 128:  
  129. 129:  
  130. 130:  
  131. 131:     /********************* data access ****************d*g**/
  132. 132:  
  133. 133:  
  134. 134:  
  135. 135:     /**
  136. 136:      * Expand all variables.
  137. 137:      * @return void 
  138. 138:      */
  139. 139:     public function expand()
  140. 140:     {
  141. 141:         $this->updating();
  142. 142:  
  143. 143:         $data $this->getArrayCopy();
  144. 144:         foreach ($data as $key => $val{
  145. 145:             if (is_string($val)) {
  146. 146:                 $data[$keyEnvironment::expand($val);
  147. 147:             elseif ($val instanceof self{
  148. 148:                 $val->expand();
  149. 149:             }
  150. 150:         }
  151. 151:         $this->setArray($data);
  152. 152:     }
  153. 153:  
  154. 154:  
  155. 155:  
  156. 156:     /**
  157. 157:      * Import from array or any traversable object.
  158. 158:      * @param  array|\Traversable
  159. 159:      * @return void 
  160. 160:      * @throws InvalidArgumentException
  161. 161:      */
  162. 162:     public function import($arr)
  163. 163:     {
  164. 164:         $this->updating();
  165. 165:  
  166. 166:         foreach ($arr as $key => $val{
  167. 167:             if (is_array($val)) {
  168. 168:                 $arr[$key$obj clone $this;
  169. 169:                 $obj->import($val);
  170. 170:             }
  171. 171:         }
  172. 172:         $this->setArray($arr);
  173. 173:     }
  174. 174:  
  175. 175:  
  176. 176:  
  177. 177:     /**
  178. 178:      * Returns an array containing all of the elements in this collection.
  179. 179:      * @return array 
  180. 180:      */
  181. 181:     public function toArray()
  182. 182:     {
  183. 183:         $res $this->getArrayCopy();
  184. 184:         foreach ($res as $key => $val{
  185. 185:             if ($val instanceof self{
  186. 186:                 $res[$key$val->toArray();
  187. 187:             }
  188. 188:         }
  189. 189:         return $res;
  190. 190:     }
  191. 191:  
  192. 192:  
  193. 193:  
  194. 194:     /**
  195. 195:      * Makes the object unmodifiable.
  196. 196:      * @return void 
  197. 197:      */
  198. 198:     public function freeze()
  199. 199:     {
  200. 200:         parent::freeze();
  201. 201:         foreach ($this->getArrayCopy(as $val{
  202. 202:             if ($val instanceof self{
  203. 203:                 $val->freeze();
  204. 204:             }
  205. 205:         }
  206. 206:     }
  207. 207:  
  208. 208:  
  209. 209:  
  210. 210:     /**
  211. 211:      * Creates a modifiable clone of the object.
  212. 212:      * @return void 
  213. 213:      */
  214. 214:     public function __clone()
  215. 215:     {
  216. 216:         parent::__clone();
  217. 217:         $data $this->getArrayCopy();
  218. 218:         foreach ($data as $key => $val{
  219. 219:             if ($val instanceof self{
  220. 220:                 $data[$keyclone $val;
  221. 221:             }
  222. 222:         }
  223. 223:         $this->setArray($data);
  224. 224:     }
  225. 225:  
  226. 226:  
  227. 227:  
  228. 228:     /********************* data access via overloading ****************d*g**/
  229. 229:  
  230. 230:  
  231. 231:  
  232. 232:     /**
  233. 233:      * Returns item. Do not call directly.
  234. 234:      * @param  int index
  235. 235:      * @return mixed 
  236. 236:      */
  237. 237:     public function &__get($key)
  238. 238:     {
  239. 239:         $val $this->offsetGet($key);
  240. 240:         return $val;
  241. 241:     }
  242. 242:  
  243. 243:  
  244. 244:  
  245. 245:     /**
  246. 246:      * Inserts (replaces) item. Do not call directly.
  247. 247:      * @param  int index
  248. 248:      * @param  object 
  249. 249:      * @return void 
  250. 250:      */
  251. 251:     public function __set($key$item)
  252. 252:     {
  253. 253:         $this->offsetSet($key$item);
  254. 254:     }
  255. 255:  
  256. 256:  
  257. 257:  
  258. 258:     /**
  259. 259:      * Exists item?
  260. 260:      * @param  string  name
  261. 261:      * @return bool 
  262. 262:      */
  263. 263:     public function __isset($key)
  264. 264:     {
  265. 265:         return $this->offsetExists($key);
  266. 266:     }
  267. 267:  
  268. 268:  
  269. 269:  
  270. 270:     /**
  271. 271:      * Removes the element at the specified position in this list.
  272. 272:      * @param  string  name
  273. 273:      * @return void 
  274. 274:      */
  275. 275:     public function __unset($key)
  276. 276:     {
  277. 277:         $this->offsetUnset($key);
  278. 278:     }
  279. 279: