Andrey Andreev | 83d1505 | 2011-12-22 13:35:42 +0200 | [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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 83d1505 | 2011-12-22 13:35:42 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 83d1505 | 2011-12-22 13:35:42 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 18 | * |
| 19 | * @package CodeIgniter |
| 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2006 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 28 | /** |
| 29 | * CodeIgniter Driver Library Class |
| 30 | * |
| 31 | * This class enables you to create "Driver" libraries that add runtime ability |
| 32 | * to extend the capabilities of a class via additional driver objects |
| 33 | * |
| 34 | * @package CodeIgniter |
| 35 | * @subpackage Libraries |
| 36 | * @category Libraries |
| 37 | * @author EllisLab Dev Team |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 38 | * @link |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 39 | */ |
| 40 | class CI_Driver_Library { |
| 41 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 42 | /** |
| 43 | * Array of drivers that are available to use with the driver class |
| 44 | * |
| 45 | * @var array |
| 46 | */ |
| 47 | protected $valid_drivers = array(); |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 48 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 49 | /** |
| 50 | * Name of the current class - usually the driver class |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 54 | protected $lib_name; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 55 | |
Darren Hill | 6fbf6bd | 2011-08-31 14:15:35 -0400 | [diff] [blame] | 56 | /** |
| 57 | * Get magic method |
| 58 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 59 | * The first time a child is used it won't exist, so we instantiate it |
| 60 | * subsequents calls will go straight to the proper child. |
Darren Hill | 6fbf6bd | 2011-08-31 14:15:35 -0400 | [diff] [blame] | 61 | * |
| 62 | * @param string Child class name |
| 63 | * @return object Child class |
| 64 | */ |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 65 | public function __get($child) |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame^] | 66 | { |
Darren Hill | 6fbf6bd | 2011-08-31 14:15:35 -0400 | [diff] [blame] | 67 | // Try to load the driver |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 68 | return $this->load_driver($child); |
Darren Hill | 6fbf6bd | 2011-08-31 14:15:35 -0400 | [diff] [blame] | 69 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 70 | |
Darren Hill | 6fbf6bd | 2011-08-31 14:15:35 -0400 | [diff] [blame] | 71 | /** |
| 72 | * Load driver |
| 73 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 74 | * Separate load_driver call to support explicit driver load by library or user |
Darren Hill | 6fbf6bd | 2011-08-31 14:15:35 -0400 | [diff] [blame] | 75 | * |
| 76 | * @param string Child class name |
| 77 | * @return object Child class |
| 78 | */ |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 79 | public function load_driver($child) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 80 | { |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 81 | if ( ! isset($this->lib_name)) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 82 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 83 | $this->lib_name = get_class($this); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // The class will be prefixed with the parent lib |
| 87 | $child_class = $this->lib_name.'_'.$child; |
Andrey Andreev | 83d1505 | 2011-12-22 13:35:42 +0200 | [diff] [blame] | 88 | |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 89 | // Remove the CI_ prefix and lowercase |
Darren Hill | 4d1cd4c | 2011-08-31 13:59:09 -0400 | [diff] [blame] | 90 | $lib_name = ucfirst(strtolower(str_replace('CI_', '', $this->lib_name))); |
| 91 | $driver_name = strtolower(str_replace('CI_', '', $child_class)); |
Andrey Andreev | 83d1505 | 2011-12-22 13:35:42 +0200 | [diff] [blame] | 92 | |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 93 | if (in_array($driver_name, array_map('strtolower', $this->valid_drivers))) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 94 | { |
| 95 | // check and see if the driver is in a separate file |
| 96 | if ( ! class_exists($child_class)) |
| 97 | { |
| 98 | // check application path first |
Phil Sturgeon | 02d45b5 | 2011-08-14 09:56:47 -0600 | [diff] [blame] | 99 | foreach (get_instance()->load->get_package_paths(TRUE) as $path) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 100 | { |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 101 | // loves me some nesting! |
| 102 | foreach (array(ucfirst($driver_name), $driver_name) as $class) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 103 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 104 | $filepath = $path.'libraries/'.$lib_name.'/drivers/'.$class.'.php'; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 105 | |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 106 | if (file_exists($filepath)) |
| 107 | { |
| 108 | include_once $filepath; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 109 | break 2; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 114 | // it's a valid driver, but the file simply can't be found |
| 115 | if ( ! class_exists($child_class)) |
| 116 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 117 | log_message('error', 'Unable to load the requested driver: '.$child_class); |
| 118 | show_error('Unable to load the requested driver: '.$child_class); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | $obj = new $child_class; |
| 123 | $obj->decorate($this); |
| 124 | $this->$child = $obj; |
| 125 | return $this->$child; |
| 126 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 127 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 128 | // The requested driver isn't valid! |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 129 | log_message('error', 'Invalid driver requested: '.$child_class); |
| 130 | show_error('Invalid driver requested: '.$child_class); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 131 | } |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 132 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 133 | } |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 134 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 135 | // -------------------------------------------------------------------------- |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * CodeIgniter Driver Class |
| 139 | * |
| 140 | * This class enables you to create drivers for a Library based on the Driver Library. |
| 141 | * It handles the drivers' access to the parent library |
| 142 | * |
| 143 | * @package CodeIgniter |
| 144 | * @subpackage Libraries |
| 145 | * @category Libraries |
| 146 | * @author EllisLab Dev Team |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 147 | * @link |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 148 | */ |
| 149 | class CI_Driver { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 151 | /** |
| 152 | * Instance of the parent class |
| 153 | * |
| 154 | * @var object |
| 155 | */ |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 156 | protected $_parent; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 157 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 158 | /** |
| 159 | * List of methods in the parent class |
| 160 | * |
| 161 | * @var array |
| 162 | */ |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 163 | protected $_methods = array(); |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 164 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 165 | /** |
| 166 | * List of properties in the parent class |
| 167 | * |
| 168 | * @var array |
| 169 | */ |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 170 | protected $_properties = array(); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 171 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 172 | /** |
| 173 | * Array of methods and properties for the parent class(es) |
| 174 | * |
| 175 | * @var array |
| 176 | */ |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 177 | protected static $_reflections = array(); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * Decorate |
| 181 | * |
| 182 | * Decorates the child with the parent driver lib's methods and properties |
| 183 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 184 | * @param object |
| 185 | * @return void |
| 186 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 187 | public function decorate($parent) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 188 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 189 | $this->_parent = $parent; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 190 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 191 | // Lock down attributes to what is defined in the class |
| 192 | // and speed up references in magic methods |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 194 | $class_name = get_class($parent); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 196 | if ( ! isset(self::$_reflections[$class_name])) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 197 | { |
| 198 | $r = new ReflectionObject($parent); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 200 | foreach ($r->getMethods() as $method) |
| 201 | { |
| 202 | if ($method->isPublic()) |
| 203 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 204 | $this->_methods[] = $method->getName(); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 208 | foreach ($r->getProperties() as $prop) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 209 | { |
| 210 | if ($prop->isPublic()) |
| 211 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 212 | $this->_properties[] = $prop->getName(); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 213 | } |
| 214 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 215 | |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 216 | self::$_reflections[$class_name] = array($this->_methods, $this->_properties); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 217 | } |
| 218 | else |
| 219 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 220 | list($this->_methods, $this->_properties) = self::$_reflections[$class_name]; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 226 | /** |
| 227 | * __call magic method |
| 228 | * |
| 229 | * Handles access to the parent driver library's methods |
| 230 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 231 | * @param string |
| 232 | * @param array |
| 233 | * @return mixed |
| 234 | */ |
| 235 | public function __call($method, $args = array()) |
| 236 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 237 | if (in_array($method, $this->_methods)) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 238 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 239 | return call_user_func_array(array($this->_parent, $method), $args); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | $trace = debug_backtrace(); |
| 243 | _exception_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']); |
| 244 | exit; |
| 245 | } |
| 246 | |
| 247 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 248 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 249 | /** |
| 250 | * __get magic method |
| 251 | * |
| 252 | * Handles reading of the parent driver library's properties |
| 253 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 254 | * @param string |
| 255 | * @return mixed |
| 256 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 257 | public function __get($var) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 258 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 259 | if (in_array($var, $this->_properties)) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 260 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 261 | return $this->_parent->$var; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 266 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 267 | /** |
| 268 | * __set magic method |
| 269 | * |
| 270 | * Handles writing to the parent driver library's properties |
| 271 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 272 | * @param string |
| 273 | * @param array |
| 274 | * @return mixed |
| 275 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 276 | public function __set($var, $val) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 277 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 278 | if (in_array($var, $this->_properties)) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 279 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 280 | $this->_parent->$var = $val; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 281 | } |
| 282 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 283 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 284 | } |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 285 | |
| 286 | /* End of file Driver.php */ |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame^] | 287 | /* Location: ./system/libraries/Driver.php */ |