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. |
| 18 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 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(); |
| 48 | |
| 49 | /** |
| 50 | * Name of the current class - usually the driver class |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 54 | protected static $lib_name; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 55 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 56 | /** |
| 57 | * The first time a child is used it won't exist, so we instantiate it |
| 58 | * subsequents calls will go straight to the proper child. |
| 59 | * |
| 60 | * @param mixed $child |
| 61 | * @return mixed |
| 62 | */ |
Andrey Andreev | 83d1505 | 2011-12-22 13:35:42 +0200 | [diff] [blame] | 63 | public function __get($child) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 64 | { |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 65 | if ( ! isset($this->lib_name)) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 66 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 67 | $this->lib_name = get_class($this); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // The class will be prefixed with the parent lib |
| 71 | $child_class = $this->lib_name.'_'.$child; |
Andrey Andreev | 83d1505 | 2011-12-22 13:35:42 +0200 | [diff] [blame] | 72 | |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 73 | // Remove the CI_ prefix and lowercase |
Greg Aker | 28bda7f | 2011-04-25 15:00:45 -0500 | [diff] [blame] | 74 | $lib_name = ucfirst(strtolower(str_replace('CI_', '', $this->lib_name))); |
| 75 | $driver_name = strtolower(str_replace('CI_', '', $child_class)); |
Andrey Andreev | 83d1505 | 2011-12-22 13:35:42 +0200 | [diff] [blame] | 76 | |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 77 | if (in_array($driver_name, array_map('strtolower', $this->valid_drivers))) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 78 | { |
| 79 | // check and see if the driver is in a separate file |
| 80 | if ( ! class_exists($child_class)) |
| 81 | { |
| 82 | // check application path first |
Phil Sturgeon | 02d45b5 | 2011-08-14 09:56:47 -0600 | [diff] [blame] | 83 | foreach (get_instance()->load->get_package_paths(TRUE) as $path) |
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 | // loves me some nesting! |
| 86 | foreach (array(ucfirst($driver_name), $driver_name) as $class) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 87 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 88 | $filepath = $path.'libraries/'.$lib_name.'/drivers/'.$class.'.php'; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 89 | |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 90 | if (file_exists($filepath)) |
| 91 | { |
| 92 | include_once $filepath; |
Stéphane Goetz | 35be644 | 2011-10-24 23:10:07 +0300 | [diff] [blame] | 93 | break 2; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 97 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 98 | // it's a valid driver, but the file simply can't be found |
| 99 | if ( ! class_exists($child_class)) |
| 100 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 101 | log_message('error', 'Unable to load the requested driver: '.$child_class); |
| 102 | show_error('Unable to load the requested driver: '.$child_class); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | $obj = new $child_class; |
| 107 | $obj->decorate($this); |
| 108 | $this->$child = $obj; |
| 109 | return $this->$child; |
| 110 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 112 | // The requested driver isn't valid! |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 113 | log_message('error', 'Invalid driver requested: '.$child_class); |
| 114 | show_error('Invalid driver requested: '.$child_class); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 115 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 116 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 117 | } |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 118 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 119 | // -------------------------------------------------------------------------- |
| 120 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 121 | /** |
| 122 | * CodeIgniter Driver Class |
| 123 | * |
| 124 | * This class enables you to create drivers for a Library based on the Driver Library. |
| 125 | * It handles the drivers' access to the parent library |
| 126 | * |
| 127 | * @package CodeIgniter |
| 128 | * @subpackage Libraries |
| 129 | * @category Libraries |
| 130 | * @author EllisLab Dev Team |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 131 | * @link |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 132 | */ |
| 133 | class CI_Driver { |
Andrey Andreev | 83d1505 | 2011-12-22 13:35:42 +0200 | [diff] [blame] | 134 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 135 | /** |
| 136 | * Instance of the parent class |
| 137 | * |
| 138 | * @var object |
| 139 | */ |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 140 | protected $_parent; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 142 | /** |
| 143 | * List of methods in the parent class |
| 144 | * |
| 145 | * @var array |
| 146 | */ |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 147 | protected $_methods = array(); |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 148 | |
| 149 | /** |
| 150 | * List of properties in the parent class |
| 151 | * |
| 152 | * @var array |
| 153 | */ |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 154 | protected $_properties = array(); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 155 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame^] | 156 | /** |
| 157 | * Array of methods and properties for the parent class(es) |
| 158 | * |
| 159 | * @var array |
| 160 | */ |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 161 | protected static $_reflections = array(); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * Decorate |
| 165 | * |
| 166 | * Decorates the child with the parent driver lib's methods and properties |
| 167 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 168 | * @param object |
| 169 | * @return void |
| 170 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 171 | public function decorate($parent) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 172 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 173 | $this->_parent = $parent; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 175 | // Lock down attributes to what is defined in the class |
| 176 | // and speed up references in magic methods |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 178 | $class_name = get_class($parent); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 180 | if ( ! isset(self::$_reflections[$class_name])) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 181 | { |
| 182 | $r = new ReflectionObject($parent); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 184 | foreach ($r->getMethods() as $method) |
| 185 | { |
| 186 | if ($method->isPublic()) |
| 187 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 188 | $this->_methods[] = $method->getName(); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 192 | foreach ($r->getProperties() as $prop) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 193 | { |
| 194 | if ($prop->isPublic()) |
| 195 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 196 | $this->_properties[] = $prop->getName(); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 197 | } |
| 198 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 200 | self::$_reflections[$class_name] = array($this->_methods, $this->_properties); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 201 | } |
| 202 | else |
| 203 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 204 | list($this->_methods, $this->_properties) = self::$_reflections[$class_name]; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 205 | } |
| 206 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 207 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 208 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 209 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 210 | /** |
| 211 | * __call magic method |
| 212 | * |
| 213 | * Handles access to the parent driver library's methods |
| 214 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 215 | * @param string |
| 216 | * @param array |
| 217 | * @return mixed |
| 218 | */ |
| 219 | public function __call($method, $args = array()) |
| 220 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 221 | if (in_array($method, $this->_methods)) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 222 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 223 | return call_user_func_array(array($this->_parent, $method), $args); |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | $trace = debug_backtrace(); |
| 227 | _exception_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']); |
| 228 | exit; |
| 229 | } |
| 230 | |
| 231 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 233 | /** |
| 234 | * __get magic method |
| 235 | * |
| 236 | * Handles reading of the parent driver library's properties |
| 237 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 238 | * @param string |
| 239 | * @return mixed |
| 240 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 241 | public function __get($var) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 242 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 243 | if (in_array($var, $this->_properties)) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 244 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 245 | return $this->_parent->$var; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 250 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 251 | /** |
| 252 | * __set magic method |
| 253 | * |
| 254 | * Handles writing to the parent driver library's properties |
| 255 | * |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 256 | * @param string |
| 257 | * @param array |
| 258 | * @return mixed |
| 259 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 260 | public function __set($var, $val) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 261 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 262 | if (in_array($var, $this->_properties)) |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 263 | { |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 264 | $this->_parent->$var = $val; |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 265 | } |
| 266 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 268 | } |
Derek Jones | 8dca041 | 2010-03-05 13:01:44 -0600 | [diff] [blame] | 269 | |
| 270 | /* End of file Driver.php */ |
Andrey Andreev | 8486e9c | 2012-03-26 20:24:12 +0300 | [diff] [blame] | 271 | /* Location: ./system/libraries/Driver.php */ |