Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author EllisLab Dev Team |
Derek Jones | 700205a | 2011-01-28 07:44:28 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006 - 2011, EllisLab, Inc. |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * CodeIgniter Driver Library Class |
| 20 | * |
| 21 | * This class enables you to create "Driver" libraries that add runtime ability |
| 22 | * to extend the capabilities of a class via additional driver objects |
| 23 | * |
| 24 | * @package CodeIgniter |
| 25 | * @subpackage Libraries |
| 26 | * @category Libraries |
| 27 | * @author EllisLab Dev Team |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 28 | * @link |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 29 | */ |
| 30 | class CI_Driver_Library { |
| 31 | |
| 32 | protected $valid_drivers = array(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame^] | 33 | protected $lib_name; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 34 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame^] | 35 | /** |
| 36 | * Get magic method |
| 37 | * |
| 38 | * The first time a child is used it won't exist, so we instantiate it |
| 39 | * subsequents calls will go straight to the proper child. |
| 40 | * |
| 41 | * @param string Child class name |
| 42 | * @return object Child class |
| 43 | */ |
| 44 | public function __get($child) |
| 45 | { |
| 46 | // Try to load the driver |
| 47 | return load_driver($child); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Load driver |
| 52 | * |
| 53 | * Separate load_driver call to support explicit driver load by library or user |
| 54 | * |
| 55 | * @param string Child class name |
| 56 | * @return object Child class |
| 57 | */ |
| 58 | public function load_driver($child) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 59 | { |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 60 | if ( ! isset($this->lib_name)) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 61 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 62 | $this->lib_name = get_class($this); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // The class will be prefixed with the parent lib |
| 66 | $child_class = $this->lib_name.'_'.$child; |
Greg Aker | 28bda7f | 2011-04-25 15:00:45 -0500 | [diff] [blame] | 67 | |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 68 | // Remove the CI_ prefix and lowercase |
Greg Aker | 28bda7f | 2011-04-25 15:00:45 -0500 | [diff] [blame] | 69 | $lib_name = ucfirst(strtolower(str_replace('CI_', '', $this->lib_name))); |
| 70 | $driver_name = strtolower(str_replace('CI_', '', $child_class)); |
| 71 | |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 72 | if (in_array($driver_name, array_map('strtolower', $this->valid_drivers))) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 73 | { |
| 74 | // check and see if the driver is in a separate file |
| 75 | if ( ! class_exists($child_class)) |
| 76 | { |
| 77 | // check application path first |
Phil Sturgeon | 02d45b5 | 2011-08-14 09:56:47 -0600 | [diff] [blame] | 78 | foreach (get_instance()->load->get_package_paths(TRUE) as $path) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 79 | { |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 80 | // loves me some nesting! |
| 81 | foreach (array(ucfirst($driver_name), $driver_name) as $class) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 82 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 83 | $filepath = $path.'libraries/'.$lib_name.'/drivers/'.$class.'.php'; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 84 | |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 85 | if (file_exists($filepath)) |
| 86 | { |
| 87 | include_once $filepath; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame^] | 88 | break 2; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 93 | // it's a valid driver, but the file simply can't be found |
| 94 | if ( ! class_exists($child_class)) |
| 95 | { |
| 96 | log_message('error', "Unable to load the requested driver: ".$child_class); |
| 97 | show_error("Unable to load the requested driver: ".$child_class); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | $obj = new $child_class; |
| 102 | $obj->decorate($this); |
| 103 | $this->$child = $obj; |
| 104 | return $this->$child; |
| 105 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 106 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 107 | // The requested driver isn't valid! |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame^] | 108 | log_message('error', 'Invalid driver requested: '.$child_class); |
| 109 | show_error('Invalid driver requested: '.$child_class); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 110 | } |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 111 | } |
| 112 | // END CI_Driver_Library CLASS |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * CodeIgniter Driver Class |
| 117 | * |
| 118 | * This class enables you to create drivers for a Library based on the Driver Library. |
| 119 | * It handles the drivers' access to the parent library |
| 120 | * |
| 121 | * @package CodeIgniter |
| 122 | * @subpackage Libraries |
| 123 | * @category Libraries |
| 124 | * @author EllisLab Dev Team |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | * @link |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 126 | */ |
| 127 | class CI_Driver { |
| 128 | protected $parent; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 130 | private $methods = array(); |
| 131 | private $properties = array(); |
| 132 | |
| 133 | private static $reflections = array(); |
| 134 | |
| 135 | /** |
| 136 | * Decorate |
| 137 | * |
| 138 | * Decorates the child with the parent driver lib's methods and properties |
| 139 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 140 | * @param object |
| 141 | * @return void |
| 142 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 143 | public function decorate($parent) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 144 | { |
| 145 | $this->parent = $parent; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 147 | // Lock down attributes to what is defined in the class |
| 148 | // and speed up references in magic methods |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 150 | $class_name = get_class($parent); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 152 | if ( ! isset(self::$reflections[$class_name])) |
| 153 | { |
| 154 | $r = new ReflectionObject($parent); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 156 | foreach ($r->getMethods() as $method) |
| 157 | { |
| 158 | if ($method->isPublic()) |
| 159 | { |
| 160 | $this->methods[] = $method->getName(); |
| 161 | } |
| 162 | } |
| 163 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 164 | foreach ($r->getProperties() as $prop) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 165 | { |
| 166 | if ($prop->isPublic()) |
| 167 | { |
| 168 | $this->properties[] = $prop->getName(); |
| 169 | } |
| 170 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 171 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 172 | self::$reflections[$class_name] = array($this->methods, $this->properties); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | list($this->methods, $this->properties) = self::$reflections[$class_name]; |
| 177 | } |
| 178 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 180 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 182 | /** |
| 183 | * __call magic method |
| 184 | * |
| 185 | * Handles access to the parent driver library's methods |
| 186 | * |
| 187 | * @access public |
| 188 | * @param string |
| 189 | * @param array |
| 190 | * @return mixed |
| 191 | */ |
| 192 | public function __call($method, $args = array()) |
| 193 | { |
| 194 | if (in_array($method, $this->methods)) |
| 195 | { |
| 196 | return call_user_func_array(array($this->parent, $method), $args); |
| 197 | } |
| 198 | |
| 199 | $trace = debug_backtrace(); |
| 200 | _exception_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']); |
| 201 | exit; |
| 202 | } |
| 203 | |
| 204 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 206 | /** |
| 207 | * __get magic method |
| 208 | * |
| 209 | * Handles reading of the parent driver library's properties |
| 210 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 211 | * @param string |
| 212 | * @return mixed |
| 213 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 214 | public function __get($var) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 215 | { |
| 216 | if (in_array($var, $this->properties)) |
| 217 | { |
| 218 | return $this->parent->$var; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 224 | /** |
| 225 | * __set magic method |
| 226 | * |
| 227 | * Handles writing to the parent driver library's properties |
| 228 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 229 | * @param string |
| 230 | * @param array |
| 231 | * @return mixed |
| 232 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 233 | public function __set($var, $val) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 234 | { |
| 235 | if (in_array($var, $this->properties)) |
| 236 | { |
| 237 | $this->parent->$var = $val; |
| 238 | } |
| 239 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 240 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 241 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 242 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 243 | } |
| 244 | // END CI_Driver CLASS |
| 245 | |
| 246 | /* End of file Driver.php */ |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame^] | 247 | /* Location: ./system/libraries/Driver.php */ |