/web/fuel/core/classes/config.php

https://github.com/leonardteo/INSE6530 · PHP · 236 lines · 167 code · 39 blank · 30 comment · 21 complexity · 030b742e9a6d82f62e6d0caffee95287 MD5 · raw file

  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. class Config {
  14. public static $loaded_files = array();
  15. public static $items = array();
  16. public static function load($file, $group = null, $reload = false)
  17. {
  18. if ( ! is_array($file) && array_key_exists($file, static::$loaded_files) and ! $reload)
  19. {
  20. return false;
  21. }
  22. $config = array();
  23. if (is_array($file))
  24. {
  25. $config = $file;
  26. }
  27. elseif ($paths = \Fuel::find_file('config', $file, '.php', true))
  28. {
  29. // Reverse the file list so that we load the core configs first and
  30. // the app can override anything.
  31. $paths = array_reverse($paths);
  32. foreach ($paths as $path)
  33. {
  34. $config = array_merge($config, \Fuel::load($path));
  35. }
  36. }
  37. if ($group === null)
  38. {
  39. static::$items = $reload ? $config : static::$items + $config;
  40. }
  41. else
  42. {
  43. $group = ($group === true) ? $file : $group;
  44. if ( ! isset(static::$items[$group]) or $reload)
  45. {
  46. static::$items[$group] = array();
  47. }
  48. static::$items[$group] = array_merge(static::$items[$group],$config);
  49. }
  50. if ( ! is_array($file))
  51. {
  52. static::$loaded_files[$file] = true;
  53. }
  54. return $config;
  55. }
  56. public static function save($file, $config)
  57. {
  58. if ( ! is_array($config))
  59. {
  60. if ( ! isset(static::$items[$config]))
  61. {
  62. return false;
  63. }
  64. $config = static::$items[$config];
  65. }
  66. $content = <<<CONF
  67. <?php
  68. /**
  69. * Fuel is a fast, lightweight, community driven PHP5 framework.
  70. *
  71. * @package Fuel
  72. * @version 1.0
  73. * @author Fuel Development Team
  74. * @license MIT License
  75. * @copyright 2011 Fuel Development Team
  76. * @link http://fuelphp.com
  77. */
  78. CONF;
  79. $content .= 'return '.str_replace(' ', "\t", var_export($config, true)).';';
  80. if ( ! $path = \Fuel::find_file('config', $file, '.php'))
  81. {
  82. if ($pos = strripos($file, '::'))
  83. {
  84. // get the namespace path
  85. if ($path = \Autoloader::namespace_path('\\'.ucfirst(substr($file, 0, $pos))))
  86. {
  87. // strip the namespace from the filename
  88. $file = substr($file, $pos+2);
  89. // strip the classes directory as we need the module root
  90. // and construct the filename
  91. $path = substr($path,0, -8).'config'.DS.$file.'.php';
  92. }
  93. else
  94. {
  95. // invalid namespace requested
  96. return false;
  97. }
  98. }
  99. }
  100. $content .= <<<CONF
  101. CONF;
  102. // make sure we have a fallback
  103. $path or $path = APPPATH.'config'.DS.$file.'.php';
  104. $path = pathinfo($path);
  105. return File::update($path['dirname'], $path['basename'], $content);
  106. }
  107. public static function get($item, $default = null)
  108. {
  109. if (isset(static::$items[$item]))
  110. {
  111. return static::$items[$item];
  112. }
  113. if (strpos($item, '.') !== false)
  114. {
  115. $parts = explode('.', $item);
  116. switch (count($parts))
  117. {
  118. case 2:
  119. if (isset(static::$items[$parts[0]][$parts[1]]))
  120. {
  121. return static::$items[$parts[0]][$parts[1]];
  122. }
  123. break;
  124. case 3:
  125. if (isset(static::$items[$parts[0]][$parts[1]][$parts[2]]))
  126. {
  127. return static::$items[$parts[0]][$parts[1]][$parts[2]];
  128. }
  129. break;
  130. case 4:
  131. if (isset(static::$items[$parts[0]][$parts[1]][$parts[2]][$parts[3]]))
  132. {
  133. return static::$items[$parts[0]][$parts[1]][$parts[2]][$parts[3]];
  134. }
  135. break;
  136. default:
  137. $return = false;
  138. foreach ($parts as $part)
  139. {
  140. if ($return === false and isset(static::$items[$part]))
  141. {
  142. $return = static::$items[$part];
  143. }
  144. elseif (isset($return[$part]))
  145. {
  146. $return = $return[$part];
  147. }
  148. else
  149. {
  150. return $default;
  151. }
  152. }
  153. return $return;
  154. break;
  155. }
  156. }
  157. return $default;
  158. }
  159. public static function set($item, $value)
  160. {
  161. $parts = explode('.', $item);
  162. switch (count($parts))
  163. {
  164. case 1:
  165. static::$items[$parts[0]] = $value;
  166. break;
  167. case 2:
  168. static::$items[$parts[0]][$parts[1]] = $value;
  169. break;
  170. case 3:
  171. static::$items[$parts[0]][$parts[1]][$parts[2]] = $value;
  172. break;
  173. case 4:
  174. static::$items[$parts[0]][$parts[1]][$parts[2]][$parts[3]] = $value;
  175. break;
  176. default:
  177. $item =& static::$items;
  178. foreach ($parts as $part)
  179. {
  180. // if it's not an array it can't have a subvalue
  181. if ( ! is_array($item))
  182. {
  183. return false;
  184. }
  185. // if the part didn't exist yet: add it
  186. if ( ! isset($item[$part]))
  187. {
  188. $item[$part] = array();
  189. }
  190. $item =& $item[$part];
  191. }
  192. $item = $value;
  193. break;
  194. }
  195. return true;
  196. }
  197. }