diff options
-rw-r--r-- | system/application/config/config.php | 43 | ||||
-rw-r--r-- | system/codeigniter/Common.php | 78 | ||||
-rw-r--r-- | system/libraries/Benchmark.php | 1 | ||||
-rw-r--r-- | system/libraries/Loader.php | 50 | ||||
-rw-r--r-- | user_guide/general/core_classes.html | 15 | ||||
-rw-r--r-- | user_guide/general/creating_libraries.html | 34 |
6 files changed, 121 insertions, 100 deletions
diff --git a/system/application/config/config.php b/system/application/config/config.php index bf15525cb..b0d2bf500 100644 --- a/system/application/config/config.php +++ b/system/application/config/config.php @@ -83,6 +83,21 @@ $config['enable_hooks'] = TRUE; /* |-------------------------------------------------------------------------- +| Class Extension Prefix +|-------------------------------------------------------------------------- +| +| This item allows you to set the filename/classname prefix when extending +| native libraries. For more information please see the user guide: +| +| http://www.codeigniter.com/user_guide/general/core_classes.html +| http://www.codeigniter.com/user_guide/general/creating_libraries.html +| +*/ +$config['subclass_prefix'] = 'MY_'; + + +/* +|-------------------------------------------------------------------------- | Allowed URL Characters |-------------------------------------------------------------------------- | @@ -129,20 +144,7 @@ $config['function_trigger'] = 'm'; /* |-------------------------------------------------------------------------- -| Master Time Reference -|-------------------------------------------------------------------------- -| -| Options are "local" or "gmt". This pref tells the system whether to use -| your server's local time as the master "now" reference, or convert it to -| GMT. See the "date helper" page of the user guide for information -| regarding date handling. -| -*/ -$config['time_reference'] = 'local'; - -/* -|-------------------------------------------------------------------------- -| Error Logging Threshold +| Error Logging |-------------------------------------------------------------------------- | | You can enable error logging by setting a threshold over zero. The @@ -267,5 +269,18 @@ $config['global_xss_filtering'] = FALSE; */ $config['compress_output'] = FALSE; +/* +|-------------------------------------------------------------------------- +| Master Time Reference +|-------------------------------------------------------------------------- +| +| Options are "local" or "gmt". This pref tells the system whether to use +| your server's local time as the master "now" reference, or convert it to +| GMT. See the "date helper" page of the user guide for information +| regarding date handling. +| +*/ +$config['time_reference'] = 'local'; + ?>
\ No newline at end of file diff --git a/system/codeigniter/Common.php b/system/codeigniter/Common.php index 07b468fef..a82d486e6 100644 --- a/system/codeigniter/Common.php +++ b/system/codeigniter/Common.php @@ -50,57 +50,27 @@ function &load_class($class, $instantiate = TRUE) { return $objects[$class]; } - - // 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') - { - $objects[$class] =& new $class(); - return $objects[$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)) + // folder we'll load the native class from the system/libraries folder. + if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT)) { - require(BASEPATH.'libraries/'.$class.EXT); + require(BASEPATH.'libraries/'.$class.EXT); + require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT); + $is_subclass = TRUE; } else { - // A core class can either be extended or replaced by putting an - // identically named file in the application/libraries folder. - // We need to determine, however, if the class being requested is - // a sub-class of an existing library or an independent instance - // since each needs to be handled slightly different. - // To do this we'll open the requested class and read the top portion - // of it. If the class extends a base class we will load the base first. - // 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 top part of the file (I set a character limit of 5000 bytes, - // which correlates to roughly the first 100 lines of code), but - // I can't think of a better way to allow classes to be extended or - // replaced on-the-fly with nothing required for the user to do - // except write the declaration. Fortunately PHP is ridiculously fast - // at file reading operations so I'm not able to discern a performance - // hit based on my benchmarks, assuming only a small number of core - // files are being extended, which will usually be the case. - - $fp = fopen(APPPATH.'libraries/'.$class.EXT, "rb"); - - if (preg_match("/MY_".$class."\s+extends\s+CI_".$class."/i", fread($fp, '6000'))) + if (file_exists(APPPATH.'libraries/'.$class.EXT)) { - require(BASEPATH.'libraries/'.$class.EXT); - require(APPPATH.'libraries/'.$class.EXT); - $is_subclass = TRUE; + require(APPPATH.'libraries/'.$class.EXT); + $is_subclass = FALSE; } else { - require(APPPATH.'libraries/'.$class.EXT); + require(BASEPATH.'libraries/'.$class.EXT); + $is_subclass = FALSE; } - fclose($fp); } if ($instantiate == FALSE) @@ -111,7 +81,7 @@ function &load_class($class, $instantiate = TRUE) if ($is_subclass == TRUE) { - $name = 'MY_'.$class; + $name = config_item('subclass_prefix').$class; $objects[$class] =& new $name(); return $objects[$class]; } @@ -151,6 +121,30 @@ function &get_config() return $main_conf[0]; } +/** +* Gets a config item +* +* @access public +* @return mixed +*/ +function &config_item($item) +{ + static $config_item = array(); + + if ( ! isset($config_item[$item])) + { + $config =& get_config(); + + if ( ! isset($config[$item])) + { + return FALSE; + } + $config_item[$item] = $config[$item]; + } + + return $config_item[$item]; +} + /** * Error Handler diff --git a/system/libraries/Benchmark.php b/system/libraries/Benchmark.php index c20a54269..d4262792c 100644 --- a/system/libraries/Benchmark.php +++ b/system/libraries/Benchmark.php @@ -31,7 +31,6 @@ class CI_Benchmark { var $marker = array(); - // -------------------------------------------------------------------- /** diff --git a/system/libraries/Loader.php b/system/libraries/Loader.php index 3b1a7f2cd..b313c6d5a 100644 --- a/system/libraries/Loader.php +++ b/system/libraries/Loader.php @@ -655,51 +655,37 @@ class CI_Loader { function _ci_load_class($class, $params = NULL) { // Prep the class name - $class = strtolower(str_replace(EXT, '', $class)); - - // Is this a class extension request? - if (substr($class, 0, 3) == 'my_') - { - $class = preg_replace("/my_(.+)/", "\\1", $class); + $class = ucfirst(strtolower(str_replace(EXT, '', $class))); - // 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) + // Is this a class extension request? + if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT)) + { + if ( ! file_exists(BASEPATH.'libraries/'.$class.EXT)) { - if (file_exists(APPPATH.'libraries/'.$filename.EXT)) - { - include_once(APPPATH.'libraries/'.$filename.EXT); - } + log_message('error', "Unable to load the requested class: ".$class); + show_error("Unable to load the requested class: ".$class); } - - return $this->_ci_init_class($filename, 'MY_', $params); + + include_once(BASEPATH.'libraries/'.ucfirst($class).EXT); + include_once(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT); + + return $this->_ci_init_class($filename, config_item('subclass_prefix'), $params); } // 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++) { - for ($i = 1; $i < 3; $i++) + $path = ($i % 2) ? APPPATH : BASEPATH; + if (file_exists($path.'libraries/'.$filename.EXT)) { - $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); - } + 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); + show_error("Unable to load the requested class: ".$class); } // -------------------------------------------------------------------- diff --git a/user_guide/general/core_classes.html b/user_guide/general/core_classes.html index 556a4efc3..6d2f3804f 100644 --- a/user_guide/general/core_classes.html +++ b/user_guide/general/core_classes.html @@ -113,11 +113,15 @@ class CI_Input {<br /><br /> <h2>Extending Core Class</h2>
<p>If all you need to do is add some functionality to an existing library - perhaps add a function or two - then
-it's overkill to replace the entire library with your version. In this case it's better to simply extend the class.</p>
+it's overkill to replace the entire library with your version. In this case it's better to simply extend the class.
+Extending a class is nearly identical to replacing a class with a couple exceptions:</p>
-<p>Extending a class is identical to replacing a class with one exception: The class declaration must extend the parent class
-and your new class must be prefixed with <kbd>MY_</kbd>. For example, to extend the native <kbd>Input</kbd> class
-you'll create a file named <dfn>application/libraries/Input.php</dfn>, and declare your class with:</p>
+<ul>
+<li>The class declaration must extend the parent class.</li>
+<li>Your new class name and filename must be prefixed with <kbd>MY_</kbd> (this item is configurable. See below.).</li>
+</ul>
+
+<p>For example, to extend the native <kbd>Input</kbd> class you'll create a file named <dfn>application/libraries/</dfn><kbd>MY_Input.php</kbd>, and declare your class with:</p>
<code>
class MY_Input extends CI_Input {<br /><br />
@@ -135,7 +139,8 @@ class MY_Input extends CI_Input {<br /> }<br />
}</code>
-<p>Any functions in your class that are named identically to the functions in the parent class will be used instead of the native ones.
+<p class="important"><strong>Tip:</strong> Any functions in your class that are named identically to the functions in the parent class will be used instead of the native ones
+(this is known as "method overloading").
This allows you to substantially alter the Code Igniter core.</p>
diff --git a/user_guide/general/creating_libraries.html b/user_guide/general/creating_libraries.html index 3f168909d..d76464120 100644 --- a/user_guide/general/creating_libraries.html +++ b/user_guide/general/creating_libraries.html @@ -226,17 +226,24 @@ class CI_Email {<br /><br /> <h2>Extending Native Libraries</h2>
<p>If all you need to do is add some functionality to an existing library - perhaps add a function or two - then
-it's overkill to replace the entire library with your version. In this case it's better to simply extend the class.</p>
+it's overkill to replace the entire library with your version. In this case it's better to simply extend the class.
+Extending a class is nearly identical to replacing a class with a couple exceptions:</p>
-<p>Extending a class is identical to replacing a class with one exception: The class declaration must extend the parent class
-and your new class must be prefixed with <kbd>MY_</kbd>. For example, to extend the native <kbd>Email</kbd> class
-you'll create a file named <dfn>application/libraries/Email.php</dfn>, and declare your class with:</p>
+<ul>
+<li>The class declaration must extend the parent class.</li>
+<li>Your new class name and filename must be prefixed with <kbd>MY_</kbd> (this item is configurable. See below.).</li>
+</ul>
+
+<p>For example, to extend the native <kbd>Email</kbd> class you'll create a file named <dfn>application/libraries/</dfn><kbd>MY_Email.php</kbd>, and declare your class with:</p>
<code>
class MY_Email extends CI_Email {<br /><br />
}</code>
+<p>Note: If you need to use a constructor in your class make sure you extend the parent constructor:</p>
+
+
<code>
class MY_Email extends CI_Email {<br />
<br />
@@ -246,9 +253,13 @@ class MY_Email extends CI_Email {<br /> }<br />
}</code>
-<p class="important"><strong>Important:</strong> To tell Code Igniter to load your sub-class you MUST include <kbd>my_</kbd> in the loading function:</p>
-<code>$this->load->library('<kbd>my_</kbd>email');</code>
+<h3>Loading Your Sub-class</h3>
+
+<p>To load your sub-class you'll use the standard syntax normally used. DO NOT include your prefix. For example,
+to load the example above, which extends the Email class, you will use:</p>
+
+<code>$this->load->library('<kbd>email</kbd>');</code>
<p>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:
@@ -256,6 +267,17 @@ the email class all calls will use: <code>$this-><kbd>email</kbd>->some_function();</code>
+
+<h3>Setting Your Own Prefix</h3>
+
+<p>To set your own sub-class prefix, open your <dfn>application/config/config.php</dfn> file and look for this item:</p>
+
+<code>$config['subclass_prefix'] = 'MY_';</code>
+
+<p>Please note that all native Code Igniter libraries are prefixed with <kbd>CI_</kbd> so DO NOT use that as your prefix.</p>
+
+
+
</div>
<!-- END CONTENT -->
|