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