diff options
author | Andrey Andreev <narf@bofh.bg> | 2012-11-25 14:07:35 +0100 |
---|---|---|
committer | Andrey Andreev <narf@bofh.bg> | 2012-11-25 14:07:35 +0100 |
commit | 89ecc8a1c69bed3451d43d3f0a367f4e498d1b80 (patch) | |
tree | 91abfa8346b81a3b98ae567f5c7c608b1dc3394d /system/libraries/Driver.php | |
parent | ade05b40714070bcda7ab32187068e95b98c8cf8 (diff) | |
parent | 6262d053db57957c4445ef7fce76070854c3e30d (diff) |
Merge pull request #2029 from dchill42/driver_ext
Added driver extension support
Diffstat (limited to 'system/libraries/Driver.php')
-rw-r--r-- | system/libraries/Driver.php | 125 |
1 files changed, 92 insertions, 33 deletions
diff --git a/system/libraries/Driver.php b/system/libraries/Driver.php index 621d22631..8f5107b0d 100644 --- a/system/libraries/Driver.php +++ b/system/libraries/Driver.php @@ -60,8 +60,8 @@ class CI_Driver_Library { * The first time a child is used it won't exist, so we instantiate it * subsequents calls will go straight to the proper child. * - * @param string Child class name - * @return object Child class + * @param string Child class name + * @return object Child class */ public function __get($child) { @@ -74,61 +74,120 @@ class CI_Driver_Library { * * Separate load_driver call to support explicit driver load by library or user * - * @param string Child class name - * @return object Child class + * @param string Driver name (w/o parent prefix) + * @return object Child class */ public function load_driver($child) { + // Get CodeIgniter instance and subclass prefix + $CI = get_instance(); + $prefix = (string) $CI->config->item('subclass_prefix'); + if ( ! isset($this->lib_name)) { - $this->lib_name = get_class($this); + // Get library name without any prefix + $this->lib_name = str_replace(array('CI_', $prefix), '', get_class($this)); + } + + // The child will be prefixed with the parent lib + $child_name = $this->lib_name.'_'.$child; + + // See if requested child is a valid driver + if ( ! in_array($child, array_map('strtolower', $this->valid_drivers))) + { + // The requested driver isn't valid! + $msg = 'Invalid driver requested: '.$child_name; + log_message('error', $msg); + show_error($msg); } - // The class will be prefixed with the parent lib - $child_class = $this->lib_name.'_'.$child; + // All driver files should be in a library subdirectory - capitalized + $subdir = ucfirst(strtolower($this->lib_name)); - // Remove the CI_ prefix and lowercase - $lib_name = ucfirst(strtolower(str_replace('CI_', '', $this->lib_name))); - $driver_name = strtolower(str_replace('CI_', '', $child_class)); + // Get package paths and filename case variations to search + $paths = $CI->load->get_package_paths(TRUE); + $cases = array(ucfirst($child_name), strtolower($child_name)); - if (in_array($driver_name, array_map('strtolower', $this->valid_drivers))) + // Is there an extension? + $class_name = $prefix.$child_name; + $found = class_exists($class_name); + if ( ! $found) { - // check and see if the driver is in a separate file - if ( ! class_exists($child_class)) + // Check for subclass file + foreach ($paths as $path) { - // check application path first - foreach (get_instance()->load->get_package_paths(TRUE) as $path) + // Extension will be in drivers subdirectory + $path .= 'libraries/'.$subdir.'/drivers/'; + + // Try filename with caps and all lowercase + foreach ($cases as $name) { - // loves me some nesting! - foreach (array(ucfirst($driver_name), $driver_name) as $class) + // Does the file exist? + $file = $path.$prefix.$name.'.php'; + if (file_exists($file)) { - $filepath = $path.'libraries/'.$lib_name.'/drivers/'.$class.'.php'; - - if (file_exists($filepath)) + // Yes - require base class from BASEPATH + $basepath = BASEPATH.'libraries/'.$subdir.'/drivers/'.ucfirst($child_name).'.php'; + if ( ! file_exists($basepath)) { - include_once $filepath; - break 2; + $msg = 'Unable to load the requested class: CI_'.$child_name; + log_message('error', $msg); + show_error($msg); } + + // Include both sources and mark found + include($basepath); + include($file); + $found = TRUE; + break 2; } } + } + } - // it's a valid driver, but the file simply can't be found - if ( ! class_exists($child_class)) + // Do we need to search for the class? + if ( ! $found) + { + // Use standard class name + $class_name = 'CI_'.$child_name; + $found = class_exists($class_name); + if ( ! $found) + { + // Check package paths + foreach ($paths as $path) { - log_message('error', 'Unable to load the requested driver: '.$child_class); - show_error('Unable to load the requested driver: '.$child_class); + // Class will be in drivers subdirectory + $path .= 'libraries/'.$subdir.'/drivers/'; + + // Try filename with caps and all lowercase + foreach ($cases as $name) + { + // Does the file exist? + $file = $path.$name.'.php'; + if (file_exists($file)) + { + // Include source + include($file); + break 2; + } + } } } + } - $obj = new $child_class; - $obj->decorate($this); - $this->$child = $obj; - return $this->$child; + // Did we finally find the class? + if ( ! class_exists($class_name)) + { + $msg = 'Unable to load the requested driver: '.$class_name; + log_message('error', $msg); + show_error($msg); } - // The requested driver isn't valid! - log_message('error', 'Invalid driver requested: '.$child_class); - show_error('Invalid driver requested: '.$child_class); + // Instantiate, decorate, and add child + $obj = new $class_name; + $obj->decorate($this); + $this->$child = $obj; + return $this->$child; } } |