PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/config.php

https://bitbucket.org/codeyash/bootstrap
PHP | 231 lines | 147 code | 24 blank | 60 comment | 18 complexity | 539406fe6c0c1060f86edf249cd53ca1 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. class ConfigException extends \FuelException { }
  14. class Config
  15. {
  16. /**
  17. * @var array $loaded_files array of loaded files
  18. */
  19. public static $loaded_files = array();
  20. /**
  21. * @var array $items the master config array
  22. */
  23. public static $items = array();
  24. /**
  25. * @var string $default_check_value random value used as a not-found check in get()
  26. */
  27. public static $default_check_value;
  28. /**
  29. * @var array $itemcache the dot-notated item cache
  30. */
  31. protected static $itemcache = array();
  32. /**
  33. * Loads a config file.
  34. *
  35. * @param mixed $file string file | config array | Config_Interface instance
  36. * @param mixed $group null for no group, true for group is filename, false for not storing in the master config
  37. * @param bool $reload true to force a reload even if the file is already loaded
  38. * @param bool $overwrite true for array_merge, false for \Arr::merge
  39. * @return array the (loaded) config array
  40. */
  41. public static function load($file, $group = null, $reload = false, $overwrite = false)
  42. {
  43. if ( ! $reload and
  44. ! is_array($file) and
  45. ! is_object($file) and
  46. array_key_exists($file, static::$loaded_files))
  47. {
  48. $group === true and $group = $file;
  49. if ($group === null or $group === false or ! isset(static::$items[$group]))
  50. {
  51. return false;
  52. }
  53. return static::$items[$group];
  54. }
  55. $config = array();
  56. if (is_array($file))
  57. {
  58. $config = $file;
  59. }
  60. elseif (is_string($file))
  61. {
  62. $info = pathinfo($file);
  63. $type = 'php';
  64. if (isset($info['extension']))
  65. {
  66. $type = $info['extension'];
  67. // Keep extension when it's an absolute path, because the finder won't add it
  68. if ($file[0] !== '/' and $file[1] !== ':')
  69. {
  70. $file = substr($file, 0, -(strlen($type) + 1));
  71. }
  72. }
  73. $class = '\\Config_'.ucfirst($type);
  74. if (class_exists($class))
  75. {
  76. static::$loaded_files[$file] = true;
  77. $file = new $class($file);
  78. }
  79. else
  80. {
  81. throw new \FuelException(sprintf('Invalid config type "%s".', $type));
  82. }
  83. }
  84. if ($file instanceof Config_Interface)
  85. {
  86. try
  87. {
  88. $config = $file->load($overwrite, ! $reload);
  89. }
  90. catch (\ConfigException $e)
  91. {
  92. $config = array();
  93. }
  94. $group = $group === true ? $file->group() : $group;
  95. }
  96. if ($group === null)
  97. {
  98. static::$items = $reload ? $config : ($overwrite ? array_merge(static::$items, $config) : \Arr::merge(static::$items, $config));
  99. static::$itemcache = array();
  100. }
  101. else
  102. {
  103. $group = ($group === true) ? $file : $group;
  104. if ( ! isset(static::$items[$group]) or $reload)
  105. {
  106. static::$items[$group] = array();
  107. }
  108. static::$items[$group] = $overwrite ? array_merge(static::$items[$group],$config) : \Arr::merge(static::$items[$group],$config);
  109. $group .= '.';
  110. foreach (static::$itemcache as $key => $value)
  111. {
  112. if (strpos($key, $group) === 0)
  113. {
  114. unset(static::$itemcache[$key]);
  115. }
  116. }
  117. }
  118. return $config;
  119. }
  120. /**
  121. * Save a config array to disc.
  122. *
  123. * @param string $file desired file name
  124. * @param string|array $config master config array key or config array
  125. * @return bool false when config is empty or invalid else \File::update result
  126. */
  127. public static function save($file, $config)
  128. {
  129. if ( ! is_array($config))
  130. {
  131. if ( ! isset(static::$items[$config]))
  132. {
  133. return false;
  134. }
  135. $config = static::$items[$config];
  136. }
  137. $info = pathinfo($file);
  138. $type = 'php';
  139. if (isset($info['extension']))
  140. {
  141. $type = $info['extension'];
  142. // Keep extension when it's an absolute path, because the finder won't add it
  143. if ($file[0] !== '/' and $file[1] !== ':')
  144. {
  145. $file = substr($file, 0, -(strlen($type) + 1));
  146. }
  147. }
  148. $class = '\\Config_'.ucfirst($type);
  149. if ( ! class_exists($class))
  150. {
  151. throw new \FuelException(sprintf('Invalid config type "%s".', $type));
  152. }
  153. $driver = new $class($file);
  154. return $driver->save($config);
  155. }
  156. /**
  157. * Returns a (dot notated) config setting
  158. *
  159. * @param string $item name of the config item, can be dot notated
  160. * @param mixed $default the return value if the item isn't found
  161. * @return mixed the config setting or default if not found
  162. */
  163. public static function get($item, $default = null)
  164. {
  165. is_null(static::$default_check_value) and static::$default_check_value = pack('H*', 'DEADBEEFCAFE');
  166. if (isset(static::$items[$item]))
  167. {
  168. return static::$items[$item];
  169. }
  170. elseif ( ! isset(static::$itemcache[$item]))
  171. {
  172. $val = \Fuel::value(\Arr::get(static::$items, $item, static::$default_check_value));
  173. if ($val === static::$default_check_value)
  174. {
  175. return $default;
  176. }
  177. static::$itemcache[$item] = $val;
  178. }
  179. return static::$itemcache[$item];
  180. }
  181. /**
  182. * Sets a (dot notated) config item
  183. *
  184. * @param string a (dot notated) config key
  185. * @param mixed the config value
  186. * @return void the \Arr::set result
  187. */
  188. public static function set($item, $value)
  189. {
  190. strpos($item, '.') === false or static::$itemcache[$item] = $value;
  191. return \Arr::set(static::$items, $item, \Fuel::value($value));
  192. }
  193. /**
  194. * Deletes a (dot notated) config item
  195. *
  196. * @param string a (dot notated) config key
  197. * @return array|bool the \Arr::delete result, success boolean or array of success booleans
  198. */
  199. public static function delete($item)
  200. {
  201. if (isset(static::$itemcache[$item]))
  202. {
  203. unset(static::$itemcache[$item]);
  204. }
  205. return \Arr::delete(static::$items, $item);
  206. }
  207. }