PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Config/Config.php

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