From 33de9a144aad28763405f8ae2d5c59df5e929b4f Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 28 Sep 2006 06:50:16 +0000 Subject: --- system/libraries/Controller.php | 165 ++++++++++++++++++++++++---------------- 1 file changed, 99 insertions(+), 66 deletions(-) (limited to 'system/libraries/Controller.php') diff --git a/system/libraries/Controller.php b/system/libraries/Controller.php index 56b4d6f90..aa7b87b00 100644 --- a/system/libraries/Controller.php +++ b/system/libraries/Controller.php @@ -32,8 +32,6 @@ class Controller extends CI_Base { var $_ci_models = array(); var $_ci_scaffolding = FALSE; var $_ci_scaff_table = FALSE; - var $_ci_last_handle = NULL; - var $_ci_last_params = NULL; /** * Constructor @@ -68,48 +66,6 @@ class Controller extends CI_Base { // -------------------------------------------------------------------- - /** - * Initialization Handler - * - * Designed to be called from the class files themselves. - * See: http://www.codeigniter.com/user_guide/general/creating_libraries.html - * - * @access public - * @param string class name - * @param string variable name - * @param mixed any additional parameters - * @return void - */ - function init_class($class, $varname = '', $params = NULL) - { - // First figure out what variable we're going to assign the class to - if ($varname == '') - { - $varname = ( ! is_null($this->_ci_last_handle)) ? $this->_ci_last_handle : strtolower(str_replace('CI_', '', $class)); - } - - // Are there any parameters? - if ($params === NULL AND $this->_ci_last_params !== NULL) - { - $params = $this->_ci_last_params; - } - - // Instantiate the class - if ( ! is_null($params)) - { - $this->$varname = new $class($params); - } - else - { - $this->$varname = new $class; - } - - $this->_ci_last_params = NULL; - $this->_ci_last_handle = NULL; - } - - // -------------------------------------------------------------------- - /** * Initialization Handler * @@ -120,15 +76,21 @@ class Controller extends CI_Base { * @param mixed any additional parameters * @return void */ - function _ci_init_class($class, $params = NULL) + function _ci_load_class($class, $params = NULL) { // Prep the class name $class = strtolower(str_replace(EXT, '', $class)); - - // These are used by $this->init_class() above. - // They lets us dynamically set the object name and pass parameters - $this->_ci_last_handle = $class; - $this->_ci_last_params = $params; + + // Is this a class extension request? + if (substr($class, 0, 3) == 'my_') + { + $class = preg_replace("/my_(.+)/", "\\1", $class); + $extend = TRUE; + } + else + { + $extend = FALSE; + } // Does THIS file (Controller.php) contain an initialization // function that maps to the requested class? @@ -150,30 +112,101 @@ class Controller extends CI_Base { return TRUE; } - // Lets search for the requested library file and load it. - // We'll assume that the file we load contains a call to - // $obj->init_class() so that the class can get instantiated. - // For backward compatibility we'll test for filenames that are - // both uppercase and lower. - foreach (array(ucfirst($class), $class) as $filename) + // Are we extending one of the base classes? + if ($extend == TRUE) { - for ($i = 1; $i < 3; $i++) + // Load the requested library from the main system/libraries folder + if (file_exists(BASEPATH.'libraries/'.ucfirst($class).EXT)) { - $path = ($i % 2) ? APPPATH : BASEPATH; + include_once(BASEPATH.'libraries/'.ucfirst($class).EXT); + } - if (file_exists($path.'libraries/'.$filename.EXT)) + // Now look for a matching library + foreach (array(ucfirst($class), $class) as $filename) + { + if (file_exists(APPPATH.'libraries/'.$filename.EXT)) { - include_once($path.'libraries/'.$filename.EXT); - return TRUE; + include_once(APPPATH.'libraries/'.$filename.EXT); + } + } + + return $this->_ci_init_class($filename, 'MY_', $params); + } + else + { + // Lets search for the requested library file and load it. + // For backward compatibility we'll test for filenames that are + // both uppercase and lower. + foreach (array(ucfirst($class), $class) as $filename) + { + for ($i = 1; $i < 3; $i++) + { + $path = ($i % 2) ? APPPATH : BASEPATH; + + if (file_exists($path.'libraries/'.$filename.EXT)) + { + include_once($path.'libraries/'.$filename.EXT); + return $this->_ci_init_class($filename, '', $params); + } } } - } // If we got this far we were unable to find the requested class log_message('error', "Unable to load the requested class: ".$class); show_error("Unable to load the class: ".$class); } + + // -------------------------------------------------------------------- + + /** + * Instantiates a class + * + * @access private + * @param string + * @param string + * @return null + */ + function _ci_init_class($class, $prefix = '', $config = NULL) + { + // Is there an associated config file for this class? + + if ($config == NULL) + { + if (file_exists(APPPATH.'config/'.$class.EXT)) + { + include_once(APPPATH.'config/'.$class.EXT); + } + } + + if ($prefix == '') + { + $name = ( ! class_exists($class)) ? 'CI_'.$class : $class; + } + else + { + $name = $prefix.ucfirst($class); + } + + $remap = array( + 'DB_export' => 'dbexport', + 'DB_utility' => 'dbutility', + 'Encryption' => 'encrypt', + 'Unit_test' => 'unit' + ); + + $varname = ( ! isset($remap[$class])) ? $class : $remap[$class]; + + // Instantiate the class + if ($config !== NULL) + { + $this->$varname = new $name($config); + } + else + { + $this->$varname = new $name; + } + } // -------------------------------------------------------------------- @@ -305,7 +338,7 @@ class Controller extends CI_Base { { if ( ! in_array($item, $exceptions)) { - $this->_ci_init_class($item); + $this->_ci_load_class($item); } else { @@ -334,10 +367,10 @@ class Controller extends CI_Base { foreach (array('Config', 'Input', 'Benchmark', 'URI', 'Output') as $val) { $class = strtolower($val); - $this->$class =& _load_class('CI_'.$val); + $this->$class =& _load_class($val); } - $this->lang =& _load_class('CI_Language'); + $this->lang =& _load_class('Language'); // In PHP 4 the Controller class is a child of CI_Loader. // In PHP 5 we run it as its own class. @@ -529,7 +562,7 @@ class Controller extends CI_Base { } $this->_ci_init_database("", FALSE, TRUE); - $this->_ci_init_class('pagination'); + $this->_ci_load_class('pagination'); require_once(BASEPATH.'scaffolding/Scaffolding'.EXT); $this->scaff = new Scaffolding($this->_ci_scaff_table); $this->scaff->$method(); -- cgit v1.2.3-24-g4f1b