From cf49390d3d699d878eb6e151745e80285465ddb9 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 10 Oct 2006 07:12:31 +0000 Subject: --- system/codeigniter/Base4.php | 29 +++- system/libraries/Controller.php | 17 +-- system/libraries/Loader.php | 219 +++++++++++++++++++++-------- system/libraries/Model.php | 4 +- system/scaffolding/Scaffolding.php | 2 +- user_guide/general/changelog.html | 4 +- user_guide/general/creating_libraries.html | 3 + user_guide/libraries/calendar.html | 2 - user_guide/libraries/encryption.html | 2 - user_guide/libraries/loader.html | 4 + user_guide/libraries/parser.html | 1 - user_guide/libraries/sessions.html | 1 - user_guide/libraries/table.html | 2 - user_guide/libraries/trackback.html | 1 - user_guide/libraries/unit_testing.html | 1 - user_guide/libraries/user_agent.html | 1 - user_guide/libraries/zip.html | 1 - 17 files changed, 197 insertions(+), 97 deletions(-) diff --git a/system/codeigniter/Base4.php b/system/codeigniter/Base4.php index eccf58ed8..115c10727 100644 --- a/system/codeigniter/Base4.php +++ b/system/codeigniter/Base4.php @@ -18,10 +18,18 @@ /** * CI_BASE - For PHP 4 * - * This file is used only when Code Igniter is being run under PHP 4. - * Since PHP 4 has such poor object handling we had to come up with - * a hack (and a really ugly one at that...) to resolve some scoping - * problems. PHP 5 doesn't suffer from this problem so we load one of + * This file is used only when Code Igniter is being run under PHP 4. + * + * In order to allow CI to work under PHP 4 we had to make the Loader class + * the parent class of the Controller Base class. It's the only way we + * could enable functions like $this->load->library('email') to instantiate + * classes that can then be used within controllers as $this->email->send() + * + * PHP 4 also has trouble referencing the CI super object within application + * constructors since objects do not exist until the class is fully + * instantiated. Basically PHP 4 sucks... + * + * Since PHP 5 doesn't suffer from this problem so we load one of * two files based on the version of PHP being run. * * @package CodeIgniter @@ -34,18 +42,25 @@ function CI_Base() { - global $OBJ; parent::CI_Loader(); $this->load =& $this; + + global $OBJ; $OBJ = $this->load; } } function &get_instance() { - global $OBJ, $CI; + global $CI, $OBJ; + + if (is_object($CI)) + { + $CI->_ci_use_instance = TRUE; + return $CI; + } - return (is_object($CI)) ? $CI : $OBJ->load; + return $OBJ->load; } ?> \ No newline at end of file diff --git a/system/libraries/Controller.php b/system/libraries/Controller.php index 4b9e3e960..c80097a17 100644 --- a/system/libraries/Controller.php +++ b/system/libraries/Controller.php @@ -80,20 +80,13 @@ class Controller extends CI_Base { if (floor(phpversion()) >= 5) { $this->load = new CI_Loader(); + $this->load->_ci_use_instance = TRUE; + $this->load->_ci_autoloader(); } - - // Load everything specified in the autoload.php file - $this->load->_ci_autoloader(); - - // 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) + else { - if (is_object($var)) - { - $this->load->$key =& $this->$key; - } - } + $this->_ci_autoloader(); + } } // -------------------------------------------------------------------- diff --git a/system/libraries/Loader.php b/system/libraries/Loader.php index ca4c4feba..1738438ed 100644 --- a/system/libraries/Loader.php +++ b/system/libraries/Loader.php @@ -28,15 +28,18 @@ */ class CI_Loader { - var $CI; - var $ob_level; - var $view_path = ''; - var $cached_vars = array(); - var $models = array(); - var $helpers = array(); - var $plugins = array(); - var $scripts = array(); - var $varmap = array('unit_test' => 'unit', 'user_agent' => 'agent'); + var $_ci_ob_level; + var $_ci_view_path = ''; + var $_ci_cached_vars = array(); + var $_ci_models = array(); + var $_ci_helpers = array(); + var $_ci_plugins = array(); + var $_ci_scripts = array(); + var $_ci_varmap = array('unit_test' => 'unit', 'user_agent' => 'agent'); + + var $_ci_use_instance = FALSE; // This variable determines whether we should + // use $this or $CI =& get_instance() + // throughout this class. Don't mess with it. /** * Constructor @@ -47,11 +50,9 @@ class CI_Loader { * @access public */ function CI_Loader() - { - $this->CI =& get_instance(); - - $this->view_path = APPPATH.'views/'; - $this->ob_level = ob_get_level(); + { + $this->_ci_view_path = APPPATH.'views/'; + $this->_ci_ob_level = ob_get_level(); log_message('debug', "Loader Class Initialized"); } @@ -95,8 +96,7 @@ class CI_Loader { if ($model == '') return; - // Is the model in a sub-folder? - // If so, parse out the filename and path. + // Is the model in a sub-folder? If so, parse out the filename and path. if (strpos($model, '/') === FALSE) { $path = ''; @@ -114,14 +114,25 @@ class CI_Loader { $name = $model; } - if (in_array($name, $this->models, TRUE)) + if (in_array($name, $this->_ci_models, TRUE)) { return; - } + } - if (isset($this->CI->$name)) + if ($this->_ci_use_instance) + { + $CI =& get_instance(); + if (isset($CI->$name)) + { + show_error('The model name you are loading is the name of a resource that is already being used: '.$name); + } + } + else { - show_error('The model name you are loading is the name of a resource that is already being used: '.$name); + 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); @@ -136,7 +147,14 @@ class CI_Loader { if ($db_conn === TRUE) $db_conn = ''; - $this->CI->load->database($db_conn, FALSE, TRUE); + if ($this->_ci_use_instance) + { + $CI->load->database($db_conn, FALSE, TRUE); + } + else + { + $this->database($db_conn, FALSE, TRUE); + } } if ( ! class_exists('Model')) @@ -147,8 +165,17 @@ class CI_Loader { require_once(APPPATH.'models/'.$path.$model.EXT); $model = ucfirst($model); - $this->CI->$name = new $model(); - $this->models[] = $name; + + if ($this->_ci_use_instance) + { + $CI->$name = new $model(); + } + else + { + $this->$name = new $model(); + } + + $this->_ci_models[] = $name; $this->_ci_assign_to_models(); } @@ -239,7 +266,7 @@ class CI_Loader { { foreach ($vars as $key => $val) { - $this->cached_vars[$key] = $val; + $this->_ci_cached_vars[$key] = $val; } } } @@ -264,7 +291,7 @@ class CI_Loader { foreach ($helpers as $helper) { - if (isset($this->helpers[$helper])) + if (isset($this->_ci_helpers[$helper])) { continue; } @@ -287,7 +314,7 @@ class CI_Loader { } } - $this->helpers[$helper] = TRUE; + $this->_ci_helpers[$helper] = TRUE; } log_message('debug', 'Helpers loaded: '.implode(', ', $helpers)); @@ -330,7 +357,7 @@ class CI_Loader { foreach ($plugins as $plugin) { - if (isset($this->plugins[$plugin])) + if (isset($this->_ci_plugins[$plugin])) { continue; } @@ -353,7 +380,7 @@ class CI_Loader { } } - $this->plugins[$plugin] = TRUE; + $this->_ci_plugins[$plugin] = TRUE; } log_message('debug', 'Plugins loaded: '.implode(', ', $plugins)); @@ -400,7 +427,7 @@ class CI_Loader { foreach ($scripts as $script) { - if (isset($this->scripts[$script])) + if (isset($this->_ci_scripts[$script])) { continue; } @@ -414,7 +441,7 @@ class CI_Loader { include_once(APPPATH.'scripts/'.$script.EXT); - $this->scripts[$script] = TRUE; + $this->_ci_scripts[$script] = TRUE; } log_message('debug', 'Scripts loaded: '.implode(', ', $scripts)); @@ -431,7 +458,15 @@ class CI_Loader { */ function language($file = '', $lang = '', $return = FALSE) { - return $this->CI->lang->load($file, $lang, $return); + if ($this->_ci_use_instance) + { + $CI =& get_instance(); + return $CI->lang->load($file, $lang, $return); + } + else + { + return $this->lang->load($file, $lang, $return); + } } // -------------------------------------------------------------------- @@ -444,8 +479,16 @@ class CI_Loader { * @return void */ function config($file = '') - { - $this->CI->config->load($file); + { + if ($this->_ci_use_instance) + { + $CI =& get_instance(); + $CI->config->load($file); + } + else + { + $this->config->load($file); + } } // -------------------------------------------------------------------- @@ -471,9 +514,18 @@ class CI_Loader { { 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; + + if ($this->_ci_use_instance) + { + $CI =& get_instance(); + $CI->_ci_scaffolding = TRUE; + $CI->_ci_scaff_table = $table; + } + else + { + $this->_ci_scaffolding = TRUE; + $this->_ci_scaff_table = $table; + } } // -------------------------------------------------------------------- @@ -491,14 +543,20 @@ class CI_Loader { { // This allows anything loaded using $this->load (viwes, files, etc.) // to become accessible from within the Controller and Model functions. - foreach (get_object_vars($this->CI) as $key => $var) + // Only needed when running PHP 5 + + if ($this->_ci_use_instance) { - if (is_object($var)) + $CI =& get_instance(); + foreach (get_object_vars($CI) as $key => $var) { - $this->$key =& $this->CI->$key; + if ( ! isset($this->$key)) + { + $this->$key =& $CI->$key; + } } } - + // Set the default data variables foreach (array('view', 'vars', 'path', 'return') as $val) { @@ -515,16 +573,16 @@ class CI_Loader { */ if (is_array($vars)) { - $this->cached_vars = array_merge($this->cached_vars, $vars); + $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $vars); } - extract($this->cached_vars); + extract($this->_ci_cached_vars); // Set the path to the requested file if ($path == '') { $ext = pathinfo($view, PATHINFO_EXTENSION); $file = ($ext == '') ? $view.EXT : $view; - $path = $this->view_path.$file; + $path = $this->_ci_view_path.$file; } else { @@ -572,14 +630,15 @@ class CI_Loader { * it can be seen and included properly by the first included * template and any subsequent ones. Oy! * - */ - if (ob_get_level() > $this->ob_level + 1) + */ + if (ob_get_level() > $this->_ci_ob_level + 1) { ob_end_flush(); } else { - $this->CI->output->set_output(ob_get_contents()); + global $OUT; + $OUT->set_output(ob_get_contents()); ob_end_clean(); } } @@ -682,18 +741,33 @@ class CI_Loader { else { $class = strtolower($class); - $classvar = ( ! isset($this->varmap[$class])) ? $class : $this->varmap[$class]; + $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class]; } - // Instantiate the class - if ($config !== NULL) + // Instantiate the class + if ($this->_ci_use_instance) { - $this->CI->$classvar = new $name($config); + $CI =& get_instance(); + if ($config !== NULL) + { + $CI->$classvar = new $name($config); + } + else + { + $CI->$classvar = new $name; + } } else - { - $this->CI->$classvar = new $name; - } + { + if ($config !== NULL) + { + $this->$classvar = new $name($config); + } + else + { + $this->$classvar = new $name; + } + } } // -------------------------------------------------------------------- @@ -719,10 +793,21 @@ class CI_Loader { // Load any custome config file if (count($autoload['config']) > 0) - { - foreach ($autoload['config'] as $key => $val) + { + if ($this->_ci_use_instance) + { + $CI =& get_instance(); + foreach ($autoload['config'] as $key => $val) + { + $CI->config->load($val); + } + } + else { - $this->CI->config->load($val); + foreach ($autoload['config'] as $key => $val) + { + $this->config->load($val); + } } } @@ -788,14 +873,26 @@ class CI_Loader { */ function _ci_assign_to_models() { - if (count($this->models) == 0) + if (count($this->_ci_models) == 0) { return; } - foreach ($this->models as $model) - { - $this->CI->$model->_assign_libraries(); - } + + if ($this->_ci_use_instance) + { + $CI =& get_instance(); + foreach ($this->_ci_models as $model) + { + $CI->$model->_assign_libraries(); + } + } + else + { + foreach ($this->_ci_models as $model) + { + $this->$model->_assign_libraries(); + } + } } // -------------------------------------------------------------------- diff --git a/system/libraries/Model.php b/system/libraries/Model.php index 46f0367cb..48615e07c 100644 --- a/system/libraries/Model.php +++ b/system/libraries/Model.php @@ -51,8 +51,8 @@ class Model { { $CI =& get_instance(); foreach (get_object_vars($CI) as $key => $var) - { - if (is_object($var) AND ! isset($this->$key)) + { + if ( ! isset($this->$key)) { if ($use_reference === TRUE) { diff --git a/system/scaffolding/Scaffolding.php b/system/scaffolding/Scaffolding.php index c046d228f..6c4b3a537 100644 --- a/system/scaffolding/Scaffolding.php +++ b/system/scaffolding/Scaffolding.php @@ -56,7 +56,7 @@ class Scaffolding { * the load->view function knows where to look. */ - $this->CI->load->view_path = BASEPATH.'scaffolding/views/'; + $this->CI->load->_ci_view_path = BASEPATH.'scaffolding/views/'; // Set the base URL $this->base_url = $this->CI->config->site_url().'/'.$this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'both'); diff --git a/user_guide/general/changelog.html b/user_guide/general/changelog.html index 2a871acf5..732e1c77e 100644 --- a/user_guide/general/changelog.html +++ b/user_guide/general/changelog.html @@ -82,9 +82,9 @@ Change Log
  • Added $query->free_result() to database class.
  • Added $query->field_names() function to database class
  • Added $this->db->platform() function
  • -
  • Added two more protocols to the URI handler to make it more reliable when the $config['uri_protocol'] item is set to AUTO.
  • Added "is_numeric" to validation, which uses the native PHP is_numeric function.
  • -
  • Moved most of the functions in the Controller class into the Loader class, allowing fewer reserved function names for controllers.
  • +
  • Improved the URI handler to make it more reliable when the $config['uri_protocol'] item is set to AUTO.
  • +
  • Moved most of the functions in the Controller class into the Loader class, allowing fewer reserved function names for controllers when running under PHP 5.
  • Updated the DB Result class to return an empty array when $query->result() doesn't produce a result.
  • Updated the input->cookie() and input->post() functions in Input Class to permit arrays contained cookies that are arrays to be run through the XSS filter.
  • Fixed a bug in the Email class related to SMTP Helo data.
  • diff --git a/user_guide/general/creating_libraries.html b/user_guide/general/creating_libraries.html index 73bc4d60f..fb3f41789 100644 --- a/user_guide/general/creating_libraries.html +++ b/user_guide/general/creating_libraries.html @@ -250,8 +250,11 @@ class MY_Email extends CI_Email {
    $this->load->library('my_email'); +

    Once loaded you will use the class variable as you normally would for the class you are extending. In the case of +the email class all calls will use: +$this->email->some_function(); diff --git a/user_guide/libraries/calendar.html b/user_guide/libraries/calendar.html index 9d2a01e1b..cd3e66441 100644 --- a/user_guide/libraries/calendar.html +++ b/user_guide/libraries/calendar.html @@ -75,8 +75,6 @@ template, allowing 100% control over every aspect of its design. In addition, yo $this->load->library('calendar');

    Once loaded, the Calendar object will be available using: $this->calendar

    -

    You can also set your own class variable name. Please see the Loader Class for more info.

    -

    Displaying a Calendar

    diff --git a/user_guide/libraries/encryption.html b/user_guide/libraries/encryption.html index 92d128cd8..9be32cf4f 100644 --- a/user_guide/libraries/encryption.html +++ b/user_guide/libraries/encryption.html @@ -110,8 +110,6 @@ for example, can only hold 4K of information.

    $this->load->library('encrypt');

    Once loaded, the Encrypt library object will be available using: $this->encrypt

    -

    You can also set your own class variable name. Please see the Loader Class for more info.

    -

    $this->encrypt->encode()

    diff --git a/user_guide/libraries/loader.html b/user_guide/libraries/loader.html index b93d5fa79..347421d68 100644 --- a/user_guide/libraries/loader.html +++ b/user_guide/libraries/loader.html @@ -85,6 +85,10 @@ Note: We use the terms "class" and "library" interchangeably.

    Each library is described in detail in its own page, so please read theinformation regarding each one you would like to use.

    +

    Parameters can be passed to the library via an array in the second parameter. + + +

    If you would like your libraries assigned to a different variable name then the default you can specify the name in the second paramter:

    diff --git a/user_guide/libraries/parser.html b/user_guide/libraries/parser.html index 2f93baf0b..ace408e09 100644 --- a/user_guide/libraries/parser.html +++ b/user_guide/libraries/parser.html @@ -103,7 +103,6 @@ full-blown template parsing solution. We've kept it very lean on purpose in orde $this->load->library('parser');

    Once loaded, the Parser library object will be available using: $this->parser

    -

    You can also set your own class variable name. Please see the Loader Class for more info.

    The following functions are available in this library:

    diff --git a/user_guide/libraries/sessions.html b/user_guide/libraries/sessions.html index 7a894faed..5a1216f6e 100644 --- a/user_guide/libraries/sessions.html +++ b/user_guide/libraries/sessions.html @@ -87,7 +87,6 @@ will cause it to read, create, and update sessions.

    $this->load->library('session');

    Once loaded, the Sessions library object will be available using: $this->session

    -

    You can also set your own class variable name. Please see the Loader Class for more info.

    How do Sessions work?

    diff --git a/user_guide/libraries/table.html b/user_guide/libraries/table.html index c2e323993..61b5ce82f 100644 --- a/user_guide/libraries/table.html +++ b/user_guide/libraries/table.html @@ -72,8 +72,6 @@ HTML Table Class $this->load->library('table');

    Once loaded, the Table library object will be available using: $this->table

    -

    You can also set your own class variable name. Please see the Loader Class for more info.

    -

    Examples

    diff --git a/user_guide/libraries/trackback.html b/user_guide/libraries/trackback.html index 87f6f3a0b..0621d4e75 100644 --- a/user_guide/libraries/trackback.html +++ b/user_guide/libraries/trackback.html @@ -74,7 +74,6 @@ Trackback Class $this->load->library('trackback');

    Once loaded, the Trackback library object will be available using: $this->trackback

    -

    You can also set your own class variable name. Please see the Loader Class for more info.

    Sending Trackbacks

    diff --git a/user_guide/libraries/unit_testing.html b/user_guide/libraries/unit_testing.html index 9951c824d..45b92abb8 100644 --- a/user_guide/libraries/unit_testing.html +++ b/user_guide/libraries/unit_testing.html @@ -78,7 +78,6 @@ to determine if it is producing the correct data type and result. $this->load->library('unit_test');

    Once loaded, the Unit Test object will be available using: $this->unit

    -

    You can also set your own class variable name. Please see the Loader Class for more info.

    Running Tests

    diff --git a/user_guide/libraries/user_agent.html b/user_guide/libraries/user_agent.html index 50f4b77f4..8f3eb07de 100644 --- a/user_guide/libraries/user_agent.html +++ b/user_guide/libraries/user_agent.html @@ -72,7 +72,6 @@ In addition you can get referrer information as well as language and supported c $this->load->library('user_agent');

    Once loaded, the object will be available using: $this->agent

    -

    You can also set your own class variable name. Please see the Loader Class for more info.

    User Agent Definitions

    diff --git a/user_guide/libraries/zip.html b/user_guide/libraries/zip.html index 32ef3bedd..d072a5705 100644 --- a/user_guide/libraries/zip.html +++ b/user_guide/libraries/zip.html @@ -71,7 +71,6 @@ desktop or saved to a directory.

    $this->load->library('zip');

    Once loaded, the Zip library object will be available using: $this->zip

    -

    You can also set your own class variable name. Please see the Loader Class for more info.

    Usage Example

    -- cgit v1.2.3-24-g4f1b