PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/3.0/obsolete/web_client/system/core/Kohana_Config.php

http://github.com/gallery/gallery3-contrib
PHP | 331 lines | 170 code | 23 blank | 138 comment | 12 complexity | cb5d7260ed4a7fbc5dca351ea02c0c2f MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Provides a driver-based interface for setting and getting
  4. * configuration options for the Kohana environment
  5. *
  6. * $Id: Kohana_Config.php 4679 2009-11-10 01:45:52Z isaiah $
  7. *
  8. * @package KohanaConfig
  9. * @author Kohana Team
  10. * @copyright (c) 2007-2009 Kohana Team
  11. * @license http://kohanaphp.com/license
  12. */
  13. class Kohana_Config_Core implements ArrayAccess {
  14. /**
  15. * The default Kohana_Config driver
  16. * to use for system setup
  17. *
  18. * @var string
  19. * @static
  20. */
  21. public static $default_driver = 'array';
  22. /**
  23. * Kohana_Config instance
  24. *
  25. * @var array
  26. * @static
  27. */
  28. protected static $instance;
  29. /**
  30. * Returns a new instance of the Kohana_Config library
  31. * based on the singleton pattern
  32. *
  33. * @param string driver
  34. * @return Kohana_Config
  35. * @access public
  36. * @static
  37. */
  38. public static function & instance()
  39. {
  40. // If the driver has not been initialised, intialise it
  41. if ( empty(Kohana_Config::$instance))
  42. {
  43. //call a 1 time non singleton of Kohana_Config to get a list of drivers
  44. $config = new Kohana_Config(array(
  45. 'config_drivers'=>array(
  46. ), 'internal_cache'=>FALSE
  47. ));
  48. $core_config = $config->get('core');
  49. Kohana_Config::$instance = new Kohana_Config($core_config);
  50. }
  51. // Return the Kohana_Config driver requested
  52. return Kohana_Config::$instance;
  53. }
  54. /**
  55. * The drivers for this object
  56. *
  57. * @var Kohana_Config_Driver
  58. */
  59. protected $drivers;
  60. /**
  61. * Kohana_Config constructor to load the supplied driver.
  62. * Enforces the singleton pattern.
  63. *
  64. * @param string driver
  65. * @access protected
  66. */
  67. protected function __construct(array $core_config)
  68. {
  69. $drivers = $core_config['config_drivers'];
  70. //remove array if it's found in config
  71. if (in_array('array', $drivers))
  72. unset($drivers[array_search('array', $drivers)]);
  73. //add array at the very end
  74. $this->drivers = $drivers = array_merge($drivers, array(
  75. 'array'
  76. ));
  77. foreach ($this->drivers as & $driver)
  78. {
  79. // Create the driver name
  80. $driver = 'Config_'.ucfirst($driver).'_Driver';
  81. // Ensure the driver loads correctly
  82. if (!Kohana::auto_load($driver))
  83. throw new Kohana_Exception('The :driver: driver for the :library: library could not be found.', array(
  84. ':driver:' => $driver, ':library:' => get_class($this)
  85. ));
  86. // Load the new driver
  87. $driver = new $driver($core_config);
  88. // Ensure the new driver is valid
  89. if (!$driver instanceof Config_Driver)
  90. throw new Kohana_Exception('The :driver: driver for the :library: library must implement the :interface: interface', array(
  91. ':driver:' => $driver, ':library:' => get_class($this), ':interface:' => 'Config_Driver'
  92. ));
  93. }
  94. }
  95. /**
  96. * Gets a value from the configuration driver
  97. *
  98. * @param string key
  99. * @param bool slash
  100. * @param bool required
  101. * @return mixed
  102. * @access public
  103. */
  104. public function get($key, $slash = FALSE, $required = FALSE)
  105. {
  106. foreach ($this->drivers as $driver)
  107. {
  108. try
  109. {
  110. return $driver->get($key, $slash, $required);
  111. }
  112. catch (Kohana_Config_Exception $e)
  113. {
  114. //if it's the last driver in the list and it threw an exception, re throw it
  115. if ($driver === $this->drivers[(count($this->drivers) - 1)])
  116. throw $e;
  117. }
  118. }
  119. }
  120. /**
  121. * Sets a value to the configuration drivers
  122. *
  123. * @param string key
  124. * @param mixed value
  125. * @return bool
  126. * @access public
  127. */
  128. public function set($key, $value)
  129. {
  130. foreach ($this->drivers as $driver)
  131. {
  132. try
  133. {
  134. $driver->set($key, $value);
  135. }
  136. catch (Kohana_Config_Exception $e)
  137. {
  138. //if it's the last driver in the list and it threw an exception, re throw it
  139. if ($driver === $this->drivers[(count($this->drivers) - 1)])
  140. throw $e;
  141. }
  142. }
  143. return TRUE;
  144. }
  145. /**
  146. * Clears a group from configuration
  147. *
  148. * @param string group
  149. * @return bool
  150. * @access public
  151. */
  152. public function clear($group)
  153. {
  154. foreach ($this->drivers as $driver)
  155. {
  156. try
  157. {
  158. $driver->clear($group);
  159. }
  160. catch (Kohana_Config_Exception $e)
  161. {
  162. //if it's the last driver in the list and it threw an exception, re throw it
  163. if ($driver === $this->drivers[(count($this->drivers) - 1)])
  164. throw $e;
  165. }
  166. }
  167. return TRUE;
  168. }
  169. /**
  170. * Loads a configuration group
  171. *
  172. * @param string group
  173. * @param bool required
  174. * @return array
  175. * @access public
  176. */
  177. public function load($group, $required = FALSE)
  178. {
  179. foreach ($this->drivers as $driver)
  180. {
  181. try
  182. {
  183. return $driver->load($group, $required);
  184. }
  185. catch (Kohana_Config_Exception $e)
  186. {
  187. //if it's the last driver in the list and it threw an exception, re throw it
  188. if ($driver === $this->drivers[(count($this->drivers) - 1)])
  189. throw $e;
  190. }
  191. }
  192. }
  193. /**
  194. * Returns true or false if any config has been loaded(either manually or from cache)
  195. *
  196. * @return boolean
  197. */
  198. public function loaded()
  199. {
  200. return $this->drivers[(count($this->drivers) - 1)]->loaded;
  201. }
  202. /**
  203. * The following allows access using
  204. * array syntax.
  205. *
  206. * @example $config['core.site_domain']
  207. */
  208. /**
  209. * Allows access to configuration settings
  210. * using the ArrayAccess interface
  211. *
  212. * @param string key
  213. * @return mixed
  214. * @access public
  215. */
  216. public function offsetGet($key)
  217. {
  218. foreach ($this->drivers as $driver)
  219. {
  220. try
  221. {
  222. return $driver->get($key);
  223. }
  224. catch (Kohana_Config_Exception $e)
  225. {
  226. //if it's the last driver in the list and it threw an exception, re throw it
  227. if ($driver === $this->drivers[(count($this->drivers) - 1)])
  228. throw $e;
  229. }
  230. }
  231. }
  232. /**
  233. * Allows access to configuration settings
  234. * using the ArrayAccess interface
  235. *
  236. * @param string key
  237. * @param mixed value
  238. * @return bool
  239. * @access public
  240. */
  241. public function offsetSet($key, $value)
  242. {
  243. foreach ($this->drivers as $driver)
  244. {
  245. try
  246. {
  247. $driver->set($key, $value);
  248. }
  249. catch (Kohana_Config_Exception $e)
  250. {
  251. //if it's the last driver in the list and it threw an exception, re throw it
  252. if ($driver === $this->drivers[(count($this->drivers) - 1)])
  253. throw $e;
  254. }
  255. }
  256. return TRUE;
  257. }
  258. /**
  259. * Allows access to configuration settings
  260. * using the ArrayAccess interface
  261. *
  262. * @param string key
  263. * @return bool
  264. * @access public
  265. */
  266. public function offsetExists($key)
  267. {
  268. foreach ($this->drivers as $driver)
  269. {
  270. try
  271. {
  272. return $driver->setting_exists($key);
  273. }
  274. catch (Kohana_Config_Exception $e)
  275. {
  276. //if it's the last driver in the list and it threw an exception, re throw it
  277. if ($driver === $this->drivers[(count($this->drivers) - 1)])
  278. throw $e;
  279. }
  280. }
  281. }
  282. /**
  283. * Allows access to configuration settings
  284. * using the ArrayAccess interface
  285. *
  286. * @param string key
  287. * @return bool
  288. * @access public
  289. */
  290. public function offsetUnset($key)
  291. {
  292. foreach ($this->drivers as $driver)
  293. {
  294. try
  295. {
  296. return $driver->set($key, NULL);
  297. }
  298. catch (Kohana_Config_Exception $e)
  299. {
  300. //if it's the last driver in the list and it threw an exception, re throw it
  301. if ($driver === $this->drivers[(count($this->drivers) - 1)])
  302. throw $e;
  303. }
  304. }
  305. return TRUE;
  306. }
  307. } // End KohanaConfig
  308. class Kohana_Config_Exception extends Kohana_Exception {}