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