From 33de9a144aad28763405f8ae2d5c59df5e929b4f Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 28 Sep 2006 06:50:16 +0000 Subject: --- system/codeigniter/CodeIgniter.php | 20 ++++----- system/codeigniter/Common.php | 89 +++++++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 35 deletions(-) (limited to 'system/codeigniter') diff --git a/system/codeigniter/CodeIgniter.php b/system/codeigniter/CodeIgniter.php index 05194503f..4ac03820a 100644 --- a/system/codeigniter/CodeIgniter.php +++ b/system/codeigniter/CodeIgniter.php @@ -50,7 +50,7 @@ set_magic_quotes_runtime(0); // Kill magic quotes * ------------------------------------------------------ */ -$BM =& _load_class('CI_Benchmark'); +$BM =& _load_class('Benchmark'); $BM->mark('code_igniter_start'); /* @@ -59,7 +59,7 @@ $BM->mark('code_igniter_start'); * ------------------------------------------------------ */ -$EXT =& _load_class('CI_Hooks'); +$EXT =& _load_class('Hooks'); /* * ------------------------------------------------------ @@ -74,9 +74,9 @@ $EXT->_call_hook('pre_system'); * ------------------------------------------------------ */ -$CFG =& _load_class('CI_Config'); -$RTR =& _load_class('CI_Router'); -$OUT =& _load_class('CI_Output'); +$CFG =& _load_class('Config'); +$RTR =& _load_class('Router'); +$OUT =& _load_class('Output'); /* * ------------------------------------------------------ @@ -98,9 +98,9 @@ if ($EXT->_call_hook('cache_override') === FALSE) * ------------------------------------------------------ */ -$IN =& _load_class('CI_Input'); -$URI =& _load_class('CI_URI'); -$LANG =& _load_class('CI_Language'); +$IN =& _load_class('Input'); +$URI =& _load_class('URI'); +$LANG =& _load_class('Language'); /* * ------------------------------------------------------ @@ -115,7 +115,7 @@ $LANG =& _load_class('CI_Language'); * */ -_load_class('CI_Loader', FALSE); +_load_class('Loader', FALSE); if (floor(phpversion()) < 5) { @@ -126,7 +126,7 @@ else require(BASEPATH.'codeigniter/Base5'.EXT); } -_load_class('CI_Controller', FALSE); +_load_class('Controller', FALSE); require(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT); diff --git a/system/codeigniter/Common.php b/system/codeigniter/Common.php index 885cca2f5..24e6042cc 100644 --- a/system/codeigniter/Common.php +++ b/system/codeigniter/Common.php @@ -32,42 +32,81 @@ /** * 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 * @return object */ function &_load_class($class, $instantiate = TRUE) { static $objects = array(); - - if ( ! isset($objects[$class])) + + // Does the class exist? If so, we're done... + if (isset($objects[$class])) { - if (FALSE !== strpos($class, 'CI_')) - { - if (file_exists(APPPATH.'libraries/'.str_replace('CI_', '', $class).EXT)) - { - require(APPPATH.'libraries/'.str_replace('CI_', '', $class).EXT); - } - else - { - require(BASEPATH.'libraries/'.str_replace('CI_', '', $class).EXT); - } - } + return $objects[$class]; + } - if ($instantiate == TRUE) + // This is a special case. It's a class in the Base5.php file + // which we don't need to load. We only instantiate it. + if ($class == 'Instance') + { + return $objects[$class] =& new $class(); + } + + // If the requested class does not exist in the application/libraries + // folder we'll load the native class from the system/libraries folder. + + $is_subclass = FALSE; + if ( ! file_exists(APPPATH.'libraries/'.$class.EXT)) + { + require(BASEPATH.'libraries/'.$class.EXT); + } + else + { + // A core class can either be extended or replaced by putting an + // identially named file in the application/libraries folder. + // We need to need to determine if the class being requested is + // a sub-class or an independent instance so we'll open the file, + // read the top portion of it. If the class extends the base class + // we need to load it's parent. If it doesn't extend the base we'll + // only load the requested class. + + // Note: I'm not thrilled with this approach since it requires us to + // read the file, but I can't think of any other way to allow classes + // to be extended on-the-fly. I did benchmark the difference with and + // without the file reading and I'm not seeing a perceptable difference. + + $fp = fopen(APPPATH.'libraries/'.$class.EXT, "rb"); + if (preg_match("/MY_".$class."\s+extends\s+CI_".$class."/", fread($fp, '8000'))) { - if ($class == 'CI_Controller') - $class = 'Controller'; - - $objects[$class] =& new $class(); + require(BASEPATH.'libraries/'.$class.EXT); + require(APPPATH.'libraries/'.$class.EXT); + $is_subclass = TRUE; } else { - $objects[$class] = TRUE; + require(APPPATH.'libraries/'.$class.EXT); } + fclose($fp); } + + if ($instantiate == FALSE) + { + return $objects[$class] = TRUE; + } + + if ($is_subclass == TRUE) + { + $name = 'MY_'.$class; + return $objects[$class] =& new $name(); + } + + $name = ($class != 'Controller') ? 'CI_'.$class : $class; - return $objects[$class]; + return $objects[$class] =& new $name(); } /** @@ -115,7 +154,7 @@ function &_get_config() */ function show_error($message) { - $error =& _load_class('CI_Exceptions'); + $error =& _load_class('Exceptions'); echo $error->show_error('An Error Was Encountered', $message); exit; } @@ -133,7 +172,7 @@ function show_error($message) */ function show_404($page = '') { - $error =& _load_class('CI_Exceptions'); + $error =& _load_class('Exceptions'); $error->show_404($page); exit; } @@ -158,7 +197,7 @@ function log_message($level = 'error', $message, $php_error = FALSE) return; } - $LOG =& _load_class('CI_Log'); + $LOG =& _load_class('Log'); $LOG->write_log($level, $message, $php_error); } @@ -191,7 +230,7 @@ function _exception_handler($severity, $message, $filepath, $line) return; } - $error =& _load_class('CI_Exceptions'); + $error =& _load_class('Exceptions'); // Should we display the error? // We'll get the current error_reporting level and add its bits -- cgit v1.2.3-24-g4f1b