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