From 8f0a8f693307a6d04b8a50aa11f81041c961adf6 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 7 Oct 2006 01:17:25 +0000 Subject: --- system/libraries/Benchmark.php | 7 + system/libraries/Controller.php | 439 ++-------------------------------------- system/libraries/Loader.php | 357 ++++++++++++++++++++++++++------ system/libraries/Profiler.php | 2 +- 4 files changed, 328 insertions(+), 477 deletions(-) (limited to 'system/libraries') diff --git a/system/libraries/Benchmark.php b/system/libraries/Benchmark.php index d29e91798..c20a54269 100644 --- a/system/libraries/Benchmark.php +++ b/system/libraries/Benchmark.php @@ -71,9 +71,16 @@ class CI_Benchmark { { return '{elapsed_time}'; } + + if ( ! isset($this->marker[$point1])) + { + return ''; + } if ( ! isset($this->marker[$point2])) + { $this->marker[$point2] = microtime(); + } list($sm, $ss) = explode(' ', $this->marker[$point1]); list($em, $es) = explode(' ', $this->marker[$point2]); diff --git a/system/libraries/Controller.php b/system/libraries/Controller.php index b09be6c57..88b49a9e6 100644 --- a/system/libraries/Controller.php +++ b/system/libraries/Controller.php @@ -29,355 +29,39 @@ */ class Controller extends CI_Base { - var $_ci_models = array(); var $_ci_scaffolding = FALSE; var $_ci_scaff_table = FALSE; /** * Constructor * - * Loads the base classes needed to run CI, and runs the "autoload" - * routine which loads the systems specified in the "autoload" config file. + * Calls the initialize() function */ function Controller() { parent::CI_Base(); - // Assign all the class objects that were instantiated by the - // front controller to local class variables so that CI can be - // run as one big super object. - $this->_ci_assign_core(); - - // Load everything specified in the autoload.php file - $this->load->_ci_autoloader($this->_ci_autoload()); - - // This allows anything loaded using $this->load (viwes, files, etc.) - // to become accessible from within the Controller class functions. - foreach (get_object_vars($this) as $key => $var) - { - if (is_object($var)) - { - $this->load->$key =& $this->$key; - } - } + $this->_ci_initialize(); log_message('debug', "Controller Class Initialized"); } - + // -------------------------------------------------------------------- /** - * Initialization Handler + * Initialize * - * This function loads the requested class. - * - * @access private - * @param string the item that is being loaded - * @param mixed any additional parameters - * @return void - */ - function _ci_load_class($class, $params = NULL) - { - // Prep the class name - $class = strtolower(str_replace(EXT, '', $class)); - - // Bug fix for backward compat. - // Kill this at some point in the future - if ($class == 'unit_test') - { - $class = 'unit'; - } - - // 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? - - $method = '_ci_init_'.$class; - - if (method_exists($this, $method)) - { - if (is_null($params)) - { - $this->$method(); - } - else - { - $this->$method($params); - } - - // We're done... - return TRUE; - } - - // Are we extending one of the base classes? - if ($extend == TRUE) - { - // Load the requested library from the main system/libraries folder - if (file_exists(BASEPATH.'libraries/'.ucfirst($class).EXT)) - { - include_once(BASEPATH.'libraries/'.ucfirst($class).EXT); - } - - // Now look for a matching library - foreach (array(ucfirst($class), $class) as $filename) - { - if (file_exists(APPPATH.'libraries/'.$filename.EXT)) - { - 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.$class; - } - - $varname = ( ! isset($remap[$class])) ? $class : $remap[$class]; - $varname = strtolower($varname); - - // Instantiate the class - if ($config !== NULL) - { - $this->$varname = new $name($config); - } - else - { - $this->$varname = new $name; - } - } - - // -------------------------------------------------------------------- - - /** - * Loads and instantiates the requested model class - * - * @access private - * @param string - * @return array - */ - function _ci_init_model($model, $name = '', $db_conn = FALSE) - { - // Is the model in a sub-folder? - // If so, parse out the filename and path. - if (strpos($model, '/') === FALSE) - { - $path = ''; - } - else - { - $x = explode('/', $model); - $model = end($x); - unset($x[count($x)-1]); - $path = implode('/', $x).'/'; - } - - if ($name == '') - { - $name = $model; - } - - $obj =& get_instance(); - if (in_array($name, $obj->_ci_models, TRUE)) - { - return; - } - - if (isset($this->$name)) - { - show_error('The model name you are loading is the name of a resource that is already being used: '.$name); - } - - $model = strtolower($model); - - if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT)) - { - show_error('Unable to locate the model you have specified: '.$model); - } - - if ($db_conn !== FALSE) - { - if ($db_conn === TRUE) - $db_conn = ''; - - $this->_ci_init_database($db_conn, FALSE, TRUE); - } - - if ( ! class_exists('Model')) - { - require_once(BASEPATH.'libraries/Model'.EXT); - } - - require_once(APPPATH.'models/'.$path.$model.EXT); - - $model = ucfirst($model); - $this->$name = new $model(); - $this->_ci_models[] = $name; - $this->_ci_assign_to_models(); - } - - // -------------------------------------------------------------------- - - /** - * Assign to Models - * - * Makes sure that anything loaded by the loader class (libraries, plugins, etc.) - * will be available to modles, if any exist. - * - * @access public - * @param object - * @return array - */ - function _ci_assign_to_models() - { - $obj =& get_instance(); - if (count($obj->_ci_models) == 0) - { - return; - } - foreach ($obj->_ci_models as $model) - { - $obj->$model->_assign_libraries(); - } - } - - // -------------------------------------------------------------------- - - /** - * Auto-initialize Core Classes - * - * This initializes the core systems that are specified in the - * libraries/autoload.php file, as well as the systems specified in - * the $autoload class array above. - * - * It returns the "autoload" array so we can pass it to the Loader - * class since it needs to autoload plugins and helper files - * - * The config/autoload.php file contains an array that permits - * sub-systems to be loaded automatically. - * - * @access private - * @return array - */ - function _ci_autoload() - { - include_once(APPPATH.'config/autoload'.EXT); - - if ( ! isset($autoload)) - { - return FALSE; - } - - if (count($autoload['config']) > 0) - { - foreach ($autoload['config'] as $key => $val) - { - $this->config->load($val); - } - } - unset($autoload['config']); - - // A little tweak to remain backward compatible - // The $autoload['core'] item was deprecated - if ( ! isset($autoload['libraries'])) - { - $autoload['libraries'] = $autoload['core']; - - } - - $exceptions = array('dbutil', 'dbexport'); - - foreach ($autoload['libraries'] as $item) - { - if ( ! in_array($item, $exceptions, TRUE)) - { - $this->_ci_load_class($item); - } - else - { - $this->_ci_init_dbextra($item); - } - } - unset($autoload['libraries']); - - return $autoload; - } - - // -------------------------------------------------------------------- - - /** - * Assign the core classes to the global $CI object - * - * By assigning all the classes instantiated by the front controller - * local class variables we enable everything to be accessible using - * $this->class->function() + * Assigns all the bases classes loaded by the front controller to + * variables in this class. Also calls the autoload routine. * * @access private * @return void - */ - function _ci_assign_core() + */ + function _ci_initialize() { + // Assign all the class objects that were instantiated by the + // front controller to local class variables so that CI can be + // run as one big super object. foreach (array('Config', 'Input', 'Benchmark', 'URI', 'Output') as $val) { $class = strtolower($val); @@ -392,101 +76,22 @@ class Controller extends CI_Base { { $this->load = new CI_Loader(); } - } - - // -------------------------------------------------------------------- - /** - * Initialize Scaffolding - * - * This initializing function works a bit different than the - * others. It doesn't load the class. Instead, it simply - * sets a flag indicating that scaffolding is allowed to be - * used. The actual scaffolding function below is - * called by the front controller based on whether the - * second segment of the URL matches the "secret" scaffolding - * word stored in the application/config/routes.php - * - * @access private - * @param string the table to scaffold - * @return void - */ - function _ci_init_scaffolding($table = FALSE) - { - if ($table === FALSE) - { - show_error('You must include the name of the table you would like access when you initialize scaffolding'); - } - $this->_ci_scaffolding = TRUE; - $this->_ci_scaff_table = $table; - } - - // -------------------------------------------------------------------- - - /** - * Initialize Database - * - * @access private - * @param mixed database connection values - * @param bool whether to return the object for multiple connections - * @param bool whether to load the active record class - * @return void - */ - function _ci_init_database($params = '', $return = FALSE, $active_record = FALSE) - { - require_once(BASEPATH.'database/DB'.EXT); - return DB($params, $return, $active_record); - } - - // -------------------------------------------------------------------- - - /** - * Returns TRUE if a class is loaded, FALSE if not - * - * @access public - * @param string the class name - * @return bool - */ - function _ci_is_loaded($class) - { - return ( ! isset($this->$class) OR ! is_object($this->$class)) ? FALSE : TRUE; - } - - // -------------------------------------------------------------------- + // Load everything specified in the autoload.php file + $this->load->_ci_autoloader(); - /** - * Scaffolding - * - * Initializes the scaffolding. - * - * @access private - * @return void - */ - function _ci_scaffolding() - { - if ($this->_ci_scaffolding === FALSE OR $this->_ci_scaff_table === FALSE) - { - show_404('Scaffolding unavailable'); - } - - if (class_exists('Scaffolding')) return; - - if ( ! in_array($this->uri->segment(3), array('add', 'insert', 'edit', 'update', 'view', 'delete', 'do_delete'), TRUE)) - { - $method = 'view'; - } - else + // This allows anything loaded using $this->load (viwes, files, etc.) + // to become accessible from within the Controller class functions. + foreach (get_object_vars($this) as $key => $var) { - $method = $this->uri->segment(3); - } - - $this->_ci_init_database("", FALSE, TRUE); - $this->_ci_load_class('pagination'); - require_once(BASEPATH.'scaffolding/Scaffolding'.EXT); - $this->scaff = new Scaffolding($this->_ci_scaff_table); - $this->scaff->$method(); + if (is_object($var)) + { + $this->load->$key =& $this->$key; + } + } } + } // END _Controller class diff --git a/system/libraries/Loader.php b/system/libraries/Loader.php index 6809054e5..f823d95e8 100644 --- a/system/libraries/Loader.php +++ b/system/libraries/Loader.php @@ -28,6 +28,7 @@ */ class CI_Loader { + var $CI; var $ob_level; var $cached_vars = array(); var $helpers = array(); @@ -35,6 +36,7 @@ class CI_Loader { var $scripts = array(); var $languages = array(); var $view_path = ''; + var $models = array(); /** * Constructor @@ -46,6 +48,8 @@ class CI_Loader { */ function CI_Loader() { + $this->CI =& get_instance(); + $this->view_path = APPPATH.'views/'; $this->ob_level = ob_get_level(); @@ -70,9 +74,8 @@ class CI_Loader { if ($class == '') return; - $obj =& get_instance(); - $obj->_ci_load_class($class, $param); - $obj->_ci_assign_to_models(); + $this->_ci_load_class($class, $param); + $this->_ci_assign_to_models(); } // -------------------------------------------------------------------- @@ -92,8 +95,61 @@ class CI_Loader { if ($model == '') return; - $obj =& get_instance(); - $obj->_ci_init_model($model, $name, $db_conn); + // Is the model in a sub-folder? + // If so, parse out the filename and path. + if (strpos($model, '/') === FALSE) + { + $path = ''; + } + else + { + $x = explode('/', $model); + $model = end($x); + unset($x[count($x)-1]); + $path = implode('/', $x).'/'; + } + + if ($name == '') + { + $name = $model; + } + + if (in_array($name, $this->models, TRUE)) + { + return; + } + + if (isset($this->CI->$name)) + { + show_error('The model name you are loading is the name of a resource that is already being used: '.$name); + } + + $model = strtolower($model); + + if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT)) + { + show_error('Unable to locate the model you have specified: '.$model); + } + + if ($db_conn !== FALSE) + { + if ($db_conn === TRUE) + $db_conn = ''; + + $this->CI->load->database($db_conn, FALSE, TRUE); + } + + if ( ! class_exists('Model')) + { + require_once(BASEPATH.'libraries/Model'.EXT); + } + + require_once(APPPATH.'models/'.$path.$model.EXT); + + $model = ucfirst($model); + $this->CI->$name = new $model(); + $this->models[] = $name; + $this->_ci_assign_to_models(); } // -------------------------------------------------------------------- @@ -107,43 +163,21 @@ class CI_Loader { * @param bool whether to enable active record (this allows us to override the config setting) * @return object */ - function database($db = '', $return = FALSE, $active_record = FALSE) + function database($params = '', $return = FALSE, $active_record = FALSE) { - $obj =& get_instance(); - - return DB($params, $return, $active_record); + require_once(BASEPATH.'database/DB'.EXT); if ($return === TRUE) { - return $obj->_ci_init_database($db, TRUE, $active_record); + return DB($params, $return, $active_record); } else { - $obj->_ci_init_database($db, FALSE, $active_record); - $obj->_ci_assign_to_models(); + DB($params, $return, $active_record); + $this->_ci_assign_to_models(); } } - - // -------------------------------------------------------------------- - - /** - * Scaffolding Loader - * - * @access public - * @param string - * @return void - */ - function scaffolding($table = '') - { - if ($table == FALSE) - { - show_error('You must include the name of the table you would like access when you initialize scaffolding'); - } - $obj =& get_instance(); - $obj->_ci_init_scaffolding($table); - } - // -------------------------------------------------------------------- /** @@ -397,8 +431,7 @@ class CI_Loader { */ function language($file = '', $lang = '', $return = FALSE) { - $obj =& get_instance(); - return $obj->lang->load($file, $lang, $return); + return $this->CI->lang->load($file, $lang, $return); } // -------------------------------------------------------------------- @@ -412,31 +445,43 @@ class CI_Loader { */ function config($file = '') { - $obj =& get_instance(); - $obj->config->load($file); + $this->CI->config->load($file); } - + // -------------------------------------------------------------------- /** - * Set the Path to the "views" folder + * Scaffolding Loader * - * @access private + * This initializing function works a bit different than the + * others. It doesn't load the class. Instead, it simply + * sets a flag indicating that scaffolding is allowed to be + * used. The actual scaffolding function below is + * called by the front controller based on whether the + * second segment of the URL matches the "secret" scaffolding + * word stored in the application/config/routes.php + * + * @access public * @param string * @return void - */ - function _ci_set_view_path($path) - { - $this->view_path = $path; + */ + function scaffolding($table = '') + { + if ($table === FALSE) + { + show_error('You must include the name of the table you would like access when you initialize scaffolding'); + } + + $this->CI->_ci_scaffolding = TRUE; + $this->CI->_ci_scaff_table = $table; } - + // -------------------------------------------------------------------- - + /** * Loader * - * This function isn't called directly. It's called from - * the two functions above. It's used to load views and files + * This function is used to load views and files. * * @access private * @param array @@ -446,12 +491,11 @@ class CI_Loader { { // This allows anything loaded using $this->load (viwes, files, etc.) // to become accessible from within the Controller and Model functions. - $obj =& get_instance(); - foreach (get_object_vars($obj) as $key => $var) + foreach (get_object_vars($this->CI) as $key => $var) { if (is_object($var)) { - $this->$key =& $obj->$key; + $this->$key =& $this->CI->$key; } } @@ -535,10 +579,134 @@ class CI_Loader { } else { - $obj->output->set_output(ob_get_contents()); + $this->CI->output->set_output(ob_get_contents()); ob_end_clean(); } } + + // -------------------------------------------------------------------- + + /** + * Load class + * + * This function loads the requested class. + * + * @access private + * @param string the item that is being loaded + * @param mixed any additional parameters + * @return void + */ + function _ci_load_class($class, $params = NULL) + { + // Prep the class name + $class = strtolower(str_replace(EXT, '', $class)); + + // Bug fix for backward compat. + // Kill this at some point in the future + if ($class == 'unit_test') + { + $class = 'unit'; + } + + // Is this a class extension request? + if (substr($class, 0, 3) == 'my_') + { + $class = preg_replace("/my_(.+)/", "\\1", $class); + $extend = TRUE; + } + else + { + $extend = FALSE; + } + + // Are we extending one of the base classes? + if ($extend == TRUE) + { + // Load the requested library from the main system/libraries folder + if (file_exists(BASEPATH.'libraries/'.ucfirst($class).EXT)) + { + include_once(BASEPATH.'libraries/'.ucfirst($class).EXT); + } + + // Now look for a matching library + foreach (array(ucfirst($class), $class) as $filename) + { + if (file_exists(APPPATH.'libraries/'.$filename.EXT)) + { + 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.$class; + } + + $varname = ( ! isset($remap[$class])) ? $class : $remap[$class]; + $varname = strtolower($varname); + + // Instantiate the class + if ($config !== NULL) + { + $this->CI->$varname = new $name($config); + } + else + { + $this->CI->$varname = new $name; + } + } // -------------------------------------------------------------------- @@ -546,27 +714,97 @@ class CI_Loader { * Autoloader * * The config/autoload.php file contains an array that permits sub-systems, - * plugins, and helpers to be loaded automatically. + * libraries, plugins, and helpers to be loaded automatically. * * @access private * @param array * @return void */ - function _ci_autoloader($autoload) - { - if ($autoload === FALSE) + function _ci_autoloader() + { + include_once(APPPATH.'config/autoload'.EXT); + + if ( ! isset($autoload)) { - return; + return FALSE; } - + + // Load any custome config file + if (count($autoload['config']) > 0) + { + foreach ($autoload['config'] as $key => $val) + { + $this->CI->config->load($val); + } + } + + // Load plugins, helpers, and scripts foreach (array('helper', 'plugin', 'script') as $type) { - if (isset($autoload[$type])) + if (isset($autoload[$type]) AND count($autoload[$type]) > 0) { $this->$type($autoload[$type]); - } + } } + + // A little tweak to remain backward compatible + // The $autoload['core'] item was deprecated + if ( ! isset($autoload['libraries'])) + { + $autoload['libraries'] = $autoload['core']; + } + + // Load libraries + if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0) + { + if (in_array('database', $autoload['libraries'])) + { + $this->database(); + $autoload['libraries'] = array_diff($autoload['libraries'], array('database')); + } + + if (in_array('model', $autoload['libraries'])) + { + $this->model(); + $autoload['libraries'] = array_diff($autoload['libraries'], array('model')); + } + + if (in_array('scaffolding', $autoload['libraries'])) + { + $this->scaffolding(); + $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding')); + } + + foreach ($autoload['libraries'] as $item) + { + $this->library($item); + } + } } + + // -------------------------------------------------------------------- + + /** + * Assign to Models + * + * Makes sure that anything loaded by the loader class (libraries, plugins, etc.) + * will be available to modles, if any exist. + * + * @access public + * @param object + * @return array + */ + function _ci_assign_to_models() + { + if (count($this->models) == 0) + { + return; + } + foreach ($this->models as $model) + { + $this->CI->$model->_assign_libraries(); + } + } // -------------------------------------------------------------------- @@ -583,6 +821,7 @@ class CI_Loader { { return (is_object($object)) ? get_object_vars($object) : $object; } + } ?> \ No newline at end of file diff --git a/system/libraries/Profiler.php b/system/libraries/Profiler.php index 9da73b03e..6142267bb 100644 --- a/system/libraries/Profiler.php +++ b/system/libraries/Profiler.php @@ -62,7 +62,7 @@ class CI_Profiler { // up in the order that it was defined if (preg_match("/(.+?)_end/i", $key, $match)) { - if (isset($this->obj->benchmark->marker[$match[1].'_end'])) + if (isset($this->obj->benchmark->marker[$match[1].'_end']) AND isset($this->obj->benchmark->marker[$match[1].'_start'])) { $profile[$match[1]] = $this->obj->benchmark->elapsed_time($match[1].'_start', $key); } -- cgit v1.2.3-24-g4f1b