From 5c1aa631c5f5ec2f6b75ba1158178418e50ba11a Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Sun, 25 Dec 2011 01:24:29 -0600 Subject: Abstracting the loading of files in the config directory depending on environments. --- system/core/CodeIgniter.php | 9 +- system/core/Common.php | 200 +++++++++++++++++++++---------------- system/core/Hooks.php | 11 +- system/core/Loader.php | 9 +- system/core/Output.php | 10 +- system/core/Router.php | 9 +- system/helpers/download_helper.php | 9 +- system/helpers/file_helper.php | 9 +- system/helpers/html_helper.php | 9 +- system/helpers/smiley_helper.php | 9 +- system/helpers/text_helper.php | 9 +- system/libraries/Upload.php | 15 +-- system/libraries/User_agent.php | 13 +-- 13 files changed, 135 insertions(+), 186 deletions(-) (limited to 'system') diff --git a/system/core/CodeIgniter.php b/system/core/CodeIgniter.php index 97527e5ca..04f346c7c 100755 --- a/system/core/CodeIgniter.php +++ b/system/core/CodeIgniter.php @@ -59,14 +59,7 @@ * Load the framework constants * ------------------------------------------------------ */ - if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php')) - { - require(APPPATH.'config/'.ENVIRONMENT.'/constants.php'); - } - else - { - require(APPPATH.'config/constants.php'); - } + load_environ_config('constants', TRUE); /* * ------------------------------------------------------ diff --git a/system/core/Common.php b/system/core/Common.php index b0921fe0c..9e05f3db4 100644 --- a/system/core/Common.php +++ b/system/core/Common.php @@ -47,7 +47,6 @@ * Since there are a few places where we conditionally test for PHP > 5 * we'll set a static variable. * -* @access public * @param string * @return bool TRUE if the current version is $version or higher */ @@ -76,7 +75,6 @@ if ( ! function_exists('is_php')) * the file, based on the read-only attribute. is_writable() is also unreliable * on Unix servers if safe_mode is on. * - * @access private * @return void */ if ( ! function_exists('is_really_writable')) @@ -118,18 +116,17 @@ if ( ! function_exists('is_really_writable')) // ------------------------------------------------------------------------ /** -* Class registry -* -* This function acts as a singleton. If the requested class does not -* exist it is instantiated and set to a static variable. If it has -* previously been instantiated the variable is returned. -* -* @access public -* @param string the class name being requested -* @param string the directory where the class should be found -* @param string the class name prefix -* @return object -*/ + * Class registry + * + * This function acts as a singleton. If the requested class does not + * exist it is instantiated and set to a static variable. If it has + * previously been instantiated the variable is returned. + * + * @param string the class name being requested + * @param string the directory where the class should be found + * @param string the class name prefix + * @return object + */ if ( ! function_exists('load_class')) { function &load_class($class, $directory = 'libraries', $prefix = 'CI_') @@ -191,12 +188,11 @@ if ( ! function_exists('load_class')) // -------------------------------------------------------------------- /** -* Keeps track of which libraries have been loaded. This function is -* called by the load_class() function above -* -* @access public -* @return array -*/ + * Keeps track of which libraries have been loaded. This function is + * called by the load_class() function above + * + * @return array + */ if ( ! function_exists('is_loaded')) { function is_loaded($class = '') @@ -215,14 +211,13 @@ if ( ! function_exists('is_loaded')) // ------------------------------------------------------------------------ /** -* Loads the main config.php file -* -* This function lets us grab the config file even if the Config class -* hasn't been instantiated yet -* -* @access private -* @return array -*/ + * Loads the main config.php file + * + * This function lets us grab the config file even if the Config class + * hasn't been instantiated yet + * + * @return array + */ if ( ! function_exists('get_config')) { function &get_config($replace = array()) @@ -273,11 +268,10 @@ if ( ! function_exists('get_config')) // ------------------------------------------------------------------------ /** -* Returns the specified config item -* -* @access public -* @return mixed -*/ + * Returns the specified config item + * + * @return mixed + */ if ( ! function_exists('config_item')) { function config_item($item) @@ -302,17 +296,16 @@ if ( ! function_exists('config_item')) // ------------------------------------------------------------------------ /** -* Error Handler -* -* This function lets us invoke the exception class and -* display errors using the standard error template located -* in application/errors/errors.php -* This function will send the error page directly to the -* browser and exit. -* -* @access public -* @return void -*/ + * Error Handler + * + * This function lets us invoke the exception class and + * display errors using the standard error template located + * in application/errors/errors.php + * This function will send the error page directly to the + * browser and exit. + * + * @return void + */ if ( ! function_exists('show_error')) { function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') @@ -326,15 +319,14 @@ if ( ! function_exists('show_error')) // ------------------------------------------------------------------------ /** -* 404 Page Handler -* -* This function is similar to the show_error() function above -* However, instead of the standard error template it displays -* 404 errors. -* -* @access public -* @return void -*/ + * 404 Page Handler + * + * This function is similar to the show_error() function above + * However, instead of the standard error template it displays + * 404 errors. + * + * @return void + */ if ( ! function_exists('show_404')) { function show_404($page = '', $log_error = TRUE) @@ -348,14 +340,13 @@ if ( ! function_exists('show_404')) // ------------------------------------------------------------------------ /** -* Error Logging Interface -* -* We use this as a simple mechanism to access the logging -* class and send messages to be logged. -* -* @access public -* @return void -*/ + * Error Logging Interface + * + * We use this as a simple mechanism to access the logging + * class and send messages to be logged. + * + * @return void + */ if ( ! function_exists('log_message')) { function log_message($level = 'error', $message, $php_error = FALSE) @@ -377,7 +368,6 @@ if ( ! function_exists('log_message')) /** * Set HTTP Status Header * - * @access public * @param int the status code * @param string * @return void @@ -464,19 +454,18 @@ if ( ! function_exists('set_status_header')) // -------------------------------------------------------------------- /** -* Exception Handler -* -* This is the custom exception handler that is declaired at the top -* of Codeigniter.php. The main reason we use this is to permit -* PHP errors to be logged in our own log files since the user may -* not have access to server logs. Since this function -* effectively intercepts PHP errors, however, we also need -* to display errors based on the current error_reporting level. -* We do that with the use of a PHP error template. -* -* @access private -* @return void -*/ + * Exception Handler + * + * This is the custom exception handler that is declaired at the top + * of Codeigniter.php. The main reason we use this is to permit + * PHP errors to be logged in our own log files since the user may + * not have access to server logs. Since this function + * effectively intercepts PHP errors, however, we also need + * to display errors based on the current error_reporting level. + * We do that with the use of a PHP error template. + * + * @return void + */ if ( ! function_exists('_exception_handler')) { function _exception_handler($severity, $message, $filepath, $line) @@ -518,7 +507,6 @@ if ( ! function_exists('_exception_handler')) * This prevents sandwiching null characters * between ascii characters, like Java\0script. * - * @access public * @param string * @return string */ @@ -552,12 +540,11 @@ if ( ! function_exists('remove_invisible_characters')) // ------------------------------------------------------------------------ /** -* Returns HTML escaped variable -* -* @access public -* @param mixed -* @return mixed -*/ + * Returns HTML escaped variable + * + * @param mixed + * @return mixed + */ if ( ! function_exists('html_escape')) { function html_escape($var) @@ -566,12 +553,57 @@ if ( ! function_exists('html_escape')) { return array_map('html_escape', $var); } + + return htmlspecialchars($var, ENT_QUOTES, config_item('charset')); + } +} + +// ------------------------------------------------------------------------ + +/** + * Load Environmental config directory files. + * + * In several places we check to see if the environment is loaded up and if the file + * that is being requested lives in said environment. Otherwise load up the file from + * the main CI config dir. + * + * @todo Optimize a bit to lessen the file system hits if the file has been loaded. + * @param string filename without extension. eg: 'config' or 'hooks' + * @param boolean whether or not to do a `require_once()` or a simple `include()` + * @return void + */ + if ( ! function_exists('load_environ_config')) + { + function load_environ_config($file, $require=FALSE) + { + if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/'.$file.'.php')) + { + if ($require) + { + require_once(APPPATH.'config/'.ENVIRONMENT.'/'.$file.'.php'); + } + else + { + include(APPPATH.'config/'.ENVIRONMENT.'/'.$file.'.php'); + } + + return; + } + + if ($require) + { + require_once(APPPATH.'config/'.$file.'.php'); + } else { - return htmlspecialchars($var, ENT_QUOTES, config_item('charset')); + include(APPPATH.'config/'.$file.'.php'); } + + return; } -} + } + +// ------------------------------------------------------------------------ /* End of file Common.php */ /* Location: ./system/core/Common.php */ \ No newline at end of file diff --git a/system/core/Hooks.php b/system/core/Hooks.php index aa251a389..6a36ce963 100755 --- a/system/core/Hooks.php +++ b/system/core/Hooks.php @@ -91,16 +91,7 @@ class CI_Hooks { // Grab the "hooks" definition file. // If there are no hooks, we're done. - - if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/hooks.php')) - { - include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'); - } - elseif (is_file(APPPATH.'config/hooks.php')) - { - include(APPPATH.'config/hooks.php'); - } - + load_environ_config('hooks'); if ( ! isset($hook) OR ! is_array($hook)) { diff --git a/system/core/Loader.php b/system/core/Loader.php index 4e14b54af..12d07bbe0 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -1125,14 +1125,7 @@ class CI_Loader { */ protected function _ci_autoloader() { - if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php')) - { - include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'); - } - else - { - include(APPPATH.'config/autoload.php'); - } + load_environ_config('autoload'); if ( ! isset($autoload)) { diff --git a/system/core/Output.php b/system/core/Output.php index 9727a184f..4d1036ceb 100755 --- a/system/core/Output.php +++ b/system/core/Output.php @@ -106,15 +106,7 @@ class CI_Output { $this->_zlib_oc = @ini_get('zlib.output_compression'); // Get mime types for later - if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) - { - include APPPATH.'config/'.ENVIRONMENT.'/mimes.php'; - } - else - { - include APPPATH.'config/mimes.php'; - } - + load_environ_config('mimes'); $this->mime_types = $mimes; diff --git a/system/core/Router.php b/system/core/Router.php index 748678d67..5388a9289 100755 --- a/system/core/Router.php +++ b/system/core/Router.php @@ -141,14 +141,7 @@ class CI_Router { } // Load the routes.php file. - if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php')) - { - include(APPPATH.'config/'.ENVIRONMENT.'/routes.php'); - } - elseif (is_file(APPPATH.'config/routes.php')) - { - include(APPPATH.'config/routes.php'); - } + load_environ_config('routes'); $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route; unset($route); diff --git a/system/helpers/download_helper.php b/system/helpers/download_helper.php index 5f5d1aaf7..3173c9840 100644 --- a/system/helpers/download_helper.php +++ b/system/helpers/download_helper.php @@ -70,14 +70,7 @@ if ( ! function_exists('force_download')) $extension = end($x); // Load the mime types - if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) - { - include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'); - } - elseif (is_file(APPPATH.'config/mimes.php')) - { - include(APPPATH.'config/mimes.php'); - } + load_environ_config('mimes'); // Set a default mime if we can't find it if ( ! isset($mimes[$extension])) diff --git a/system/helpers/file_helper.php b/system/helpers/file_helper.php index 5b5085381..b4edcdea4 100644 --- a/system/helpers/file_helper.php +++ b/system/helpers/file_helper.php @@ -364,14 +364,7 @@ if ( ! function_exists('get_mime_by_extension')) if ( ! is_array($mimes)) { - if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) - { - include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'); - } - elseif (is_file(APPPATH.'config/mimes.php')) - { - include(APPPATH.'config/mimes.php'); - } + load_environ_config('mimes'); if ( ! is_array($mimes)) { diff --git a/system/helpers/html_helper.php b/system/helpers/html_helper.php index b6bb402a8..fa805f14f 100644 --- a/system/helpers/html_helper.php +++ b/system/helpers/html_helper.php @@ -275,14 +275,7 @@ if ( ! function_exists('doctype')) if ( ! is_array($_doctypes)) { - if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php')) - { - include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'); - } - elseif (is_file(APPPATH.'config/doctypes.php')) - { - include(APPPATH.'config/doctypes.php'); - } + load_environ_config('doctypes'); if ( ! is_array($_doctypes)) { diff --git a/system/helpers/smiley_helper.php b/system/helpers/smiley_helper.php index 38e2965fc..e04d5d611 100644 --- a/system/helpers/smiley_helper.php +++ b/system/helpers/smiley_helper.php @@ -241,14 +241,7 @@ if ( ! function_exists('_get_smiley_array')) { function _get_smiley_array() { - if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php')) - { - include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'); - } - elseif (file_exists(APPPATH.'config/smileys.php')) - { - include(APPPATH.'config/smileys.php'); - } + load_environ_config('smileys'); if (isset($smileys) AND is_array($smileys)) { diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 38b46e248..7a4a68995 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -378,14 +378,7 @@ if ( ! function_exists('convert_accented_characters')) { function convert_accented_characters($str) { - if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php')) - { - include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'); - } - elseif (is_file(APPPATH.'config/foreign_chars.php')) - { - include(APPPATH.'config/foreign_chars.php'); - } + load_environ_config('foreign_chars'); if ( ! isset($foreign_characters)) { diff --git a/system/libraries/Upload.php b/system/libraries/Upload.php index 826bcceb8..5108afa1b 100644 --- a/system/libraries/Upload.php +++ b/system/libraries/Upload.php @@ -952,17 +952,12 @@ class CI_Upload { { global $mimes; - if (count($this->mimes) == 0) + if (count($this->mimes) === 0) { - if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) - { - include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'); - } - elseif (is_file(APPPATH.'config/mimes.php')) - { - include(APPPATH.'config//mimes.php'); - } - else + load_environ_config('mimes'); + + // Return FALSE if we still have no mimes after trying to load them up. + if (count($this->mimes) === 0) { return FALSE; } diff --git a/system/libraries/User_agent.php b/system/libraries/User_agent.php index a007acec8..4a29d7d94 100644 --- a/system/libraries/User_agent.php +++ b/system/libraries/User_agent.php @@ -96,15 +96,10 @@ class CI_User_agent { */ private function _load_agent_file() { - if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php')) - { - include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'); - } - elseif (is_file(APPPATH.'config/user_agents.php')) - { - include(APPPATH.'config/user_agents.php'); - } - else + load_environ_config('user_agents'); + + // Return FALSE if we still have no mimes after trying to load them up. + if (count($this->mimes) === 0) { return FALSE; } -- cgit v1.2.3-24-g4f1b