Config.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter Config Class
00020  *
00021  * This class contains functions that enable config files to be managed
00022  *
00023  * @package             CodeIgniter
00024  * @subpackage  Libraries
00025  * @category    Libraries
00026  * @author              ExpressionEngine Dev Team
00027  * @link                http://codeigniter.com/user_guide/libraries/config.html
00028  */
00029 class CI_Config {
00030 
00031         var $config = array();
00032         var $is_loaded = array();
00033 
00034         /**
00035          * Constructor
00036          *
00037          * Sets the $config data from the primary config.php file as a class variable
00038          *
00039          * @access   public
00040          * @param   string      the config file name
00041          * @param   boolean  if configuration values should be loaded into their own section
00042          * @param   boolean  true if errors should just return false, false if an error message should be displayed
00043          * @return  boolean  if the file was successfully loaded or not
00044          */
00045         function CI_Config()
00046         {
00047                 $this->config =& get_config();
00048                 log_message('debug', "Config Class Initialized");
00049         }       
00050         
00051         // --------------------------------------------------------------------
00052 
00053         /**
00054          * Load Config File
00055          *
00056          * @access      public
00057          * @param       string  the config file name
00058          * @return      boolean if the file was loaded correctly
00059          */     
00060         function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
00061         {
00062                 $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
00063         
00064                 if (in_array($file, $this->is_loaded, TRUE))
00065                 {
00066                         return TRUE;
00067                 }
00068 
00069                 if ( ! file_exists(APPPATH.'config/'.$file.EXT))
00070                 {
00071                         if ($fail_gracefully === TRUE)
00072                         {
00073                                 return FALSE;
00074                         }
00075                         show_error('The configuration file '.$file.EXT.' does not exist.');
00076                 }
00077         
00078                 include(APPPATH.'config/'.$file.EXT);
00079 
00080                 if ( ! isset($config) OR ! is_array($config))
00081                 {
00082                         if ($fail_gracefully === TRUE)
00083                         {
00084                                 return FALSE;
00085                         }
00086                         show_error('Your '.$file.EXT.' file does not appear to contain a valid configuration array.');
00087                 }
00088 
00089                 if ($use_sections === TRUE)
00090                 {
00091                         if (isset($this->config[$file]))
00092                         {
00093                                 $this->config[$file] = array_merge($this->config[$file], $config);
00094                         }
00095                         else
00096                         {
00097                                 $this->config[$file] = $config;
00098                         }
00099                 }
00100                 else
00101                 {
00102                         $this->config = array_merge($this->config, $config);
00103                 }
00104 
00105                 $this->is_loaded[] = $file;
00106                 unset($config);
00107 
00108                 log_message('debug', 'Config file loaded: config/'.$file.EXT);
00109                 return TRUE;
00110         }
00111         
00112         // --------------------------------------------------------------------
00113 
00114         /**
00115          * Fetch a config file item
00116          *
00117          *
00118          * @access      public
00119          * @param       string  the config item name
00120          * @param       string  the index name
00121          * @param       bool
00122          * @return      string
00123          */
00124         function item($item, $index = '')
00125         {       
00126                 if ($index == '')
00127                 {       
00128                         if ( ! isset($this->config[$item]))
00129                         {
00130                                 return FALSE;
00131                         }
00132 
00133                         $pref = $this->config[$item];
00134                 }
00135                 else
00136                 {
00137                         if ( ! isset($this->config[$index]))
00138                         {
00139                                 return FALSE;
00140                         }
00141 
00142                         if ( ! isset($this->config[$index][$item]))
00143                         {
00144                                 return FALSE;
00145                         }
00146 
00147                         $pref = $this->config[$index][$item];
00148                 }
00149 
00150                 return $pref;
00151         }
00152         
00153         // --------------------------------------------------------------------
00154 
00155         /**
00156          * Fetch a config file item - adds slash after item
00157          *
00158          * The second parameter allows a slash to be added to the end of
00159          * the item, in the case of a path.
00160          *
00161          * @access      public
00162          * @param       string  the config item name
00163          * @param       bool
00164          * @return      string
00165          */
00166         function slash_item($item)
00167         {
00168                 if ( ! isset($this->config[$item]))
00169                 {
00170                         return FALSE;
00171                 }
00172 
00173                 $pref = $this->config[$item];
00174 
00175                 if ($pref != '' && substr($pref, -1) != '/')
00176                 {       
00177                         $pref .= '/';
00178                 }
00179 
00180                 return $pref;
00181         }
00182         
00183         // --------------------------------------------------------------------
00184 
00185         /**
00186          * Site URL
00187          *
00188          * @access      public
00189          * @param       string  the URI string
00190          * @return      string
00191          */
00192         function site_url($uri = '')
00193         {
00194                 if (is_array($uri))
00195                 {
00196                         $uri = implode('/', $uri);
00197                 }
00198 
00199                 if ($uri == '')
00200                 {
00201                         return $this->slash_item('base_url').$this->item('index_page');
00202                 }
00203                 else
00204                 {
00205                         $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
00206                         return $this->slash_item('base_url').$this->slash_item('index_page').preg_replace("|^/*(.+?)/*$|", "\\1", $uri).$suffix;
00207                 }
00208         }
00209         
00210         // --------------------------------------------------------------------
00211 
00212         /**
00213          * System URL
00214          *
00215          * @access      public
00216          * @return      string
00217          */
00218         function system_url()
00219         {
00220                 $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
00221                 return $this->slash_item('base_url').end($x).'/';
00222         }
00223         
00224         // --------------------------------------------------------------------
00225 
00226         /**
00227          * Set a config file item
00228          *
00229          * @access      public
00230          * @param       string  the config item key
00231          * @param       string  the config item value
00232          * @return      void
00233          */
00234         function set_item($item, $value)
00235         {
00236                 $this->config[$item] = $value;
00237         }
00238 
00239 }
00240 
00241 // END CI_Config class
00242 
00243 /* End of file Config.php */
00244 /* Location: ./system/libraries/Config.php */