Skip to content

Loading…

fixed error: loading custom cache driver #1934

Closed
wants to merge 2 commits into from

3 participants

@francimedia

Failed to add subclass prefixed based custom cache driver
(example: application/libraries/Cache/drivers/MY_Cache_file.php)

Error message:
"An Error Was Encountered
Unable to load the requested class: Cache"

caused by adding a custom Cache class (/application/libraries/Cache/MY_Cache.php) and loading it using $this->load->driver('cache');

@francimedia francimedia fixed error: loading custom cache driver
Failed to add subclass prefixed based custom cache driver
(example: application/libraries/Cache/drivers/MY_Cache_file.php)

Error message:
"An Error Was Encountered
Unable to load the requested class: Cache"

caused by adding a custom Cache class (/application/libraries/Cache/MY_Cache.php) and loading it using $this->load->driver('cache');
598ccd9
@dchill42

This is related to #1787.

@narfbg

I assume that this has been fixed via #2029, but can anybody confirm?

@narfbg narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 26, 2012
  1. @francimedia

    fixed error: loading custom cache driver

    francimedia committed
    Failed to add subclass prefixed based custom cache driver
    (example: application/libraries/Cache/drivers/MY_Cache_file.php)
    
    Error message:
    "An Error Was Encountered
    Unable to load the requested class: Cache"
    
    caused by adding a custom Cache class (/application/libraries/Cache/MY_Cache.php) and loading it using $this->load->driver('cache');
  2. @francimedia

    var. name fix for 598ccd9

    francimedia committed
This page is out of date. Refresh to see the latest.
Showing with 54 additions and 30 deletions.
  1. +2 −1 system/core/Loader.php
  2. +52 −29 system/libraries/Driver.php
View
3 system/core/Loader.php
@@ -926,7 +926,8 @@ protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
// Is this a class extension request?
if (file_exists($subclass))
{
- $baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
+
+ $baseclass = BASEPATH.'libraries/'.$subdir.ucfirst($class).'.php';
if ( ! file_exists($baseclass))
{
View
81 system/libraries/Driver.php
@@ -63,7 +63,7 @@ class CI_Driver_Library {
* @return object Child class
*/
public function __get($child)
- {
+ {
// Try to load the driver
return $this->load_driver($child);
}
@@ -80,54 +80,77 @@ public function load_driver($child)
{
if ( ! isset($this->lib_name))
{
- $this->lib_name = get_class($this);
+ $this->lib_name = ucfirst(strtolower(str_replace(config_item('subclass_prefix'), '', get_class($this))));
}
// The class will be prefixed with the parent lib
- $child_class = $this->lib_name.'_'.$child;
+ $_child_class = $this->lib_name.'_'.$child;
// Remove the CI_ prefix and lowercase
$lib_name = ucfirst(strtolower(str_replace('CI_', '', $this->lib_name)));
- $driver_name = strtolower(str_replace('CI_', '', $child_class));
+ $driver_name = strtolower(str_replace('CI_', '', $_child_class));
+
+ // Check for CI_ & MY_ prefixed files / classes
+ $prefixes_for_loading = array('CI_', config_item('subclass_prefix'));
+ $prefixes_for_creating = array_reverse($prefixes_for_loading);
+
if (in_array($driver_name, array_map('strtolower', $this->valid_drivers)))
{
- // check and see if the driver is in a separate file
- if ( ! class_exists($child_class))
- {
- // check application path first
- foreach (get_instance()->load->get_package_paths(TRUE) as $path)
+
+ foreach($prefixes_for_loading as $prefix) {
+
+ $child_class = $prefix . $_child_class;
+
+ // check and see if the driver is in a separate file
+ if ( ! class_exists($child_class))
{
- // loves me some nesting!
- foreach (array(ucfirst($driver_name), $driver_name) as $class)
+
+ // check application path first
+ foreach (get_instance()->load->get_package_paths(TRUE) as $path)
{
- $filepath = $path.'libraries/'.$lib_name.'/drivers/'.$class.'.php';
-
- if (file_exists($filepath))
+ // loves me some nesting!
+ foreach (array(ucfirst($driver_name), $driver_name) as $class)
{
- include_once $filepath;
- break 2;
+ if($prefix != 'CI_') {
+ $class = $prefix . $class;
+ }
+
+ $filepath = $path.'libraries/'.$lib_name.'/drivers/'.$class.'.php';
+
+ if (file_exists($filepath))
+ {
+ include_once $filepath;
+ break 2;
+ }
}
}
- }
- // it's a valid driver, but the file simply can't be found
- if ( ! class_exists($child_class))
- {
- log_message('error', 'Unable to load the requested driver: '.$child_class);
- show_error('Unable to load the requested driver: '.$child_class);
+ // it's a valid driver, but the file simply can't be found
+ if ( ! class_exists($child_class))
+ {
+ log_message('error', 'Unable to load the requested driver: '.$child_class);
+ show_error('Unable to load the requested driver: '.$child_class);
+ }
}
+
}
- $obj = new $child_class;
- $obj->decorate($this);
- $this->$child = $obj;
- return $this->$child;
- }
+ // after loading the classes, create new instance, custom classes before CI_
+ foreach($prefixes_for_creating as $prefix) {
+ $child_class = $prefix . $_child_class;
+ $obj = new $child_class;
+ $obj->decorate($this);
+ $this->$child = $obj;
+ return $this->$child;
+ }
+
+ }
+
// The requested driver isn't valid!
- log_message('error', 'Invalid driver requested: '.$child_class);
- show_error('Invalid driver requested: '.$child_class);
+ log_message('error', 'Invalid driver requested: '.$_child_class);
+ show_error('Invalid driver requested: '.$_child_class);
}
}
Something went wrong with that request. Please try again.