blob: 77476e1390cd43183d5484a30ad2e0c3de01f96d [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Jones8dca0412010-03-05 13:01:44 -06002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Jones8dca0412010-03-05 13:01:44 -06006 *
7 * @package CodeIgniter
8 * @author EllisLab Dev Team
Derek Jones700205a2011-01-28 07:44:28 -06009 * @copyright Copyright (c) 2006 - 2011, EllisLab, Inc.
Derek Jones8dca0412010-03-05 13:01:44 -060010 * @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 Mienydd671972010-10-04 16:33:58 +020028 * @link
Derek Jones8dca0412010-03-05 13:01:44 -060029 */
30class CI_Driver_Library {
31
32 protected $valid_drivers = array();
Darren Hillc4e266b2011-08-30 15:40:27 -040033 protected $lib_name;
Barry Mienydd671972010-10-04 16:33:58 +020034
Darren Hill6fbf6bd2011-08-31 14:15:35 -040035 /**
36 * Get magic method
37 *
Darren Hillc4e266b2011-08-30 15:40:27 -040038 * 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.
Darren Hill6fbf6bd2011-08-31 14:15:35 -040040 *
41 * @param string Child class name
42 * @return object Child class
43 */
Darren Hillc4e266b2011-08-30 15:40:27 -040044 public function __get($child)
45 {
Darren Hill6fbf6bd2011-08-31 14:15:35 -040046 // Try to load the driver
Darren Hillc4e266b2011-08-30 15:40:27 -040047 return load_driver($child);
Darren Hill6fbf6bd2011-08-31 14:15:35 -040048 }
Darren Hillc4e266b2011-08-30 15:40:27 -040049
Darren Hill6fbf6bd2011-08-31 14:15:35 -040050 /**
51 * Load driver
52 *
Darren Hillc4e266b2011-08-30 15:40:27 -040053 * Separate load_driver call to support explicit driver load by library or user
Darren Hill6fbf6bd2011-08-31 14:15:35 -040054 *
55 * @param string Child class name
56 * @return object Child class
57 */
Darren Hillc4e266b2011-08-30 15:40:27 -040058 public function load_driver($child)
Derek Jones8dca0412010-03-05 13:01:44 -060059 {
Pascal Kriete14287f32011-02-14 13:39:34 -050060 if ( ! isset($this->lib_name))
Derek Jones8dca0412010-03-05 13:01:44 -060061 {
Barry Mienydd671972010-10-04 16:33:58 +020062 $this->lib_name = get_class($this);
Derek Jones8dca0412010-03-05 13:01:44 -060063 }
64
65 // The class will be prefixed with the parent lib
66 $child_class = $this->lib_name.'_'.$child;
Greg Aker28bda7f2011-04-25 15:00:45 -050067
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010068 // Remove the CI_ prefix and lowercase
Darren Hill4d1cd4c2011-08-31 13:59:09 -040069 $lib_name = ucfirst(strtolower(str_replace('CI_', '', $this->lib_name)));
70 $driver_name = strtolower(str_replace('CI_', '', $child_class));
Greg Aker28bda7f2011-04-25 15:00:45 -050071
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010072 if (in_array($driver_name, array_map('strtolower', $this->valid_drivers)))
Derek Jones8dca0412010-03-05 13:01:44 -060073 {
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 Sturgeon02d45b52011-08-14 09:56:47 -060078 foreach (get_instance()->load->get_package_paths(TRUE) as $path)
Derek Jones8dca0412010-03-05 13:01:44 -060079 {
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010080 // loves me some nesting!
81 foreach (array(ucfirst($driver_name), $driver_name) as $class)
Derek Jones8dca0412010-03-05 13:01:44 -060082 {
Greg Aker3a746652011-04-19 10:59:47 -050083 $filepath = $path.'libraries/'.$lib_name.'/drivers/'.$class.'.php';
Derek Jones8dca0412010-03-05 13:01:44 -060084
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010085 if (file_exists($filepath))
86 {
87 include_once $filepath;
Darren Hillc4e266b2011-08-30 15:40:27 -040088 break 2;
Derek Jones8dca0412010-03-05 13:01:44 -060089 }
90 }
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Jones8dca0412010-03-05 13:01:44 -060093 // 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 Mienydd671972010-10-04 16:33:58 +0200106
Derek Jones8dca0412010-03-05 13:01:44 -0600107 // The requested driver isn't valid!
Darren Hillc4e266b2011-08-30 15:40:27 -0400108 log_message('error', 'Invalid driver requested: '.$child_class);
109 show_error('Invalid driver requested: '.$child_class);
Derek Jones8dca0412010-03-05 13:01:44 -0600110 }
Derek Jones8dca0412010-03-05 13:01:44 -0600111}
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 Mienydd671972010-10-04 16:33:58 +0200125 * @link
Derek Jones8dca0412010-03-05 13:01:44 -0600126 */
127class CI_Driver {
128 protected $parent;
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Jones8dca0412010-03-05 13:01:44 -0600130 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 Jones8dca0412010-03-05 13:01:44 -0600140 * @param object
141 * @return void
142 */
Greg Akera9263282010-11-10 15:26:43 -0600143 public function decorate($parent)
Derek Jones8dca0412010-03-05 13:01:44 -0600144 {
145 $this->parent = $parent;
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Jones8dca0412010-03-05 13:01:44 -0600147 // Lock down attributes to what is defined in the class
148 // and speed up references in magic methods
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Jones8dca0412010-03-05 13:01:44 -0600150 $class_name = get_class($parent);
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jones8dca0412010-03-05 13:01:44 -0600152 if ( ! isset(self::$reflections[$class_name]))
153 {
154 $r = new ReflectionObject($parent);
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Jones8dca0412010-03-05 13:01:44 -0600156 foreach ($r->getMethods() as $method)
157 {
158 if ($method->isPublic())
159 {
160 $this->methods[] = $method->getName();
161 }
162 }
163
Pascal Kriete14287f32011-02-14 13:39:34 -0500164 foreach ($r->getProperties() as $prop)
Derek Jones8dca0412010-03-05 13:01:44 -0600165 {
166 if ($prop->isPublic())
167 {
168 $this->properties[] = $prop->getName();
169 }
170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Jones8dca0412010-03-05 13:01:44 -0600172 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 Mienydd671972010-10-04 16:33:58 +0200179
Derek Jones8dca0412010-03-05 13:01:44 -0600180 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Jones8dca0412010-03-05 13:01:44 -0600182 /**
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 Mienydd671972010-10-04 16:33:58 +0200205
Derek Jones8dca0412010-03-05 13:01:44 -0600206 /**
207 * __get magic method
208 *
209 * Handles reading of the parent driver library's properties
210 *
Derek Jones8dca0412010-03-05 13:01:44 -0600211 * @param string
212 * @return mixed
213 */
Greg Akera9263282010-11-10 15:26:43 -0600214 public function __get($var)
Derek Jones8dca0412010-03-05 13:01:44 -0600215 {
216 if (in_array($var, $this->properties))
217 {
218 return $this->parent->$var;
219 }
220 }
221
222 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Jones8dca0412010-03-05 13:01:44 -0600224 /**
225 * __set magic method
226 *
227 * Handles writing to the parent driver library's properties
228 *
Derek Jones8dca0412010-03-05 13:01:44 -0600229 * @param string
230 * @param array
231 * @return mixed
232 */
Greg Akera9263282010-11-10 15:26:43 -0600233 public function __set($var, $val)
Derek Jones8dca0412010-03-05 13:01:44 -0600234 {
235 if (in_array($var, $this->properties))
236 {
237 $this->parent->$var = $val;
238 }
239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Jones8dca0412010-03-05 13:01:44 -0600241 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Jones8dca0412010-03-05 13:01:44 -0600243}
244// END CI_Driver CLASS
245
246/* End of file Driver.php */
Darren Hillc4e266b2011-08-30 15:40:27 -0400247/* Location: ./system/libraries/Driver.php */