/halogy/libraries/Config.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 244 lines · 114 code · 32 blank · 98 comment · 23 complexity · d503685a99727ef4f016cb1950bce811 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter Config Class
  18. *
  19. * This class contains functions that enable config files to be managed
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Libraries
  23. * @category Libraries
  24. * @author ExpressionEngine Dev Team
  25. * @link http://codeigniter.com/user_guide/libraries/config.html
  26. */
  27. class CI_Config {
  28. var $config = array();
  29. var $is_loaded = array();
  30. /**
  31. * Constructor
  32. *
  33. * Sets the $config data from the primary config.php file as a class variable
  34. *
  35. * @access public
  36. * @param string the config file name
  37. * @param boolean if configuration values should be loaded into their own section
  38. * @param boolean true if errors should just return false, false if an error message should be displayed
  39. * @return boolean if the file was successfully loaded or not
  40. */
  41. function CI_Config()
  42. {
  43. $this->config =& get_config();
  44. log_message('debug', "Config Class Initialized");
  45. }
  46. // --------------------------------------------------------------------
  47. /**
  48. * Load Config File
  49. *
  50. * @access public
  51. * @param string the config file name
  52. * @return boolean if the file was loaded correctly
  53. */
  54. function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
  55. {
  56. $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
  57. if (in_array($file, $this->is_loaded, TRUE))
  58. {
  59. return TRUE;
  60. }
  61. if ( ! file_exists(APPPATH.'config/'.$file.EXT))
  62. {
  63. if ($fail_gracefully === TRUE)
  64. {
  65. return FALSE;
  66. }
  67. show_error('The configuration file '.$file.EXT.' does not exist.');
  68. }
  69. include(APPPATH.'config/'.$file.EXT);
  70. if ( ! isset($config) OR ! is_array($config))
  71. {
  72. if ($fail_gracefully === TRUE)
  73. {
  74. return FALSE;
  75. }
  76. show_error('Your '.$file.EXT.' file does not appear to contain a valid configuration array.');
  77. }
  78. if ($use_sections === TRUE)
  79. {
  80. if (isset($this->config[$file]))
  81. {
  82. $this->config[$file] = array_merge($this->config[$file], $config);
  83. }
  84. else
  85. {
  86. $this->config[$file] = $config;
  87. }
  88. }
  89. else
  90. {
  91. $this->config = array_merge($this->config, $config);
  92. }
  93. $this->is_loaded[] = $file;
  94. unset($config);
  95. log_message('debug', 'Config file loaded: config/'.$file.EXT);
  96. return TRUE;
  97. }
  98. // --------------------------------------------------------------------
  99. /**
  100. * Fetch a config file item
  101. *
  102. *
  103. * @access public
  104. * @param string the config item name
  105. * @param string the index name
  106. * @param bool
  107. * @return string
  108. */
  109. function item($item, $index = '')
  110. {
  111. if ($index == '')
  112. {
  113. if ( ! isset($this->config[$item]))
  114. {
  115. return FALSE;
  116. }
  117. $pref = $this->config[$item];
  118. }
  119. else
  120. {
  121. if ( ! isset($this->config[$index]))
  122. {
  123. return FALSE;
  124. }
  125. if ( ! isset($this->config[$index][$item]))
  126. {
  127. return FALSE;
  128. }
  129. $pref = $this->config[$index][$item];
  130. }
  131. return $pref;
  132. }
  133. // --------------------------------------------------------------------
  134. /**
  135. * Fetch a config file item - adds slash after item
  136. *
  137. * The second parameter allows a slash to be added to the end of
  138. * the item, in the case of a path.
  139. *
  140. * @access public
  141. * @param string the config item name
  142. * @param bool
  143. * @return string
  144. */
  145. function slash_item($item)
  146. {
  147. if ( ! isset($this->config[$item]))
  148. {
  149. return FALSE;
  150. }
  151. $pref = $this->config[$item];
  152. if ($pref != '' && substr($pref, -1) != '/')
  153. {
  154. $pref .= '/';
  155. }
  156. return $pref;
  157. }
  158. // --------------------------------------------------------------------
  159. /**
  160. * Site URL
  161. *
  162. * @access public
  163. * @param string the URI string
  164. * @return string
  165. */
  166. function site_url($uri = '')
  167. {
  168. if (is_array($uri))
  169. {
  170. $uri = implode('/', $uri);
  171. }
  172. if ($uri == '')
  173. {
  174. return $this->slash_item('base_url').$this->item('index_page');
  175. }
  176. else
  177. {
  178. $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
  179. return $this->slash_item('base_url').$this->slash_item('index_page').trim($uri, '/').$suffix;
  180. }
  181. }
  182. // --------------------------------------------------------------------
  183. /**
  184. * System URL
  185. *
  186. * @access public
  187. * @return string
  188. */
  189. function system_url()
  190. {
  191. $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
  192. return $this->slash_item('base_url').end($x).'/';
  193. }
  194. // --------------------------------------------------------------------
  195. /**
  196. * Set a config file item
  197. *
  198. * @access public
  199. * @param string the config item key
  200. * @param string the config item value
  201. * @return void
  202. */
  203. function set_item($item, $value)
  204. {
  205. $this->config[$item] = $value;
  206. }
  207. }
  208. // END CI_Config class
  209. /* End of file Config.php */
  210. /* Location: ./system/libraries/Config.php */