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