blob: b1fff154d1a43cee7cdde52a4b1a9c0a67491b32 [file] [log] [blame]
Andrey Andreev83d15052011-12-22 13:35:42 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Jones8dca0412010-03-05 13:01:44 -06002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Jones8dca0412010-03-05 13:01:44 -06006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev83d15052011-12-22 13:35:42 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev83d15052011-12-22 13:35:42 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Jones8dca0412010-03-05 13:01:44 -060019 * @package CodeIgniter
20 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2006 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Jones8dca0412010-03-05 13:01:44 -060023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Jones8dca0412010-03-05 13:01:44 -060028/**
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 Mienydd671972010-10-04 16:33:58 +020038 * @link
Derek Jones8dca0412010-03-05 13:01:44 -060039 */
40class CI_Driver_Library {
41
Timothy Warren0688ac92012-04-20 10:25:04 -040042 /**
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 Jones8dca0412010-03-05 13:01:44 -060054 protected static $lib_name;
Barry Mienydd671972010-10-04 16:33:58 +020055
Timothy Warren0688ac92012-04-20 10:25:04 -040056 /**
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 Andreev83d15052011-12-22 13:35:42 +020063 public function __get($child)
Derek Jones8dca0412010-03-05 13:01:44 -060064 {
Pascal Kriete14287f32011-02-14 13:39:34 -050065 if ( ! isset($this->lib_name))
Derek Jones8dca0412010-03-05 13:01:44 -060066 {
Barry Mienydd671972010-10-04 16:33:58 +020067 $this->lib_name = get_class($this);
Derek Jones8dca0412010-03-05 13:01:44 -060068 }
69
70 // The class will be prefixed with the parent lib
71 $child_class = $this->lib_name.'_'.$child;
Andrey Andreev83d15052011-12-22 13:35:42 +020072
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010073 // Remove the CI_ prefix and lowercase
Greg Aker28bda7f2011-04-25 15:00:45 -050074 $lib_name = ucfirst(strtolower(str_replace('CI_', '', $this->lib_name)));
75 $driver_name = strtolower(str_replace('CI_', '', $child_class));
Andrey Andreev83d15052011-12-22 13:35:42 +020076
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010077 if (in_array($driver_name, array_map('strtolower', $this->valid_drivers)))
Derek Jones8dca0412010-03-05 13:01:44 -060078 {
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 Sturgeon02d45b52011-08-14 09:56:47 -060083 foreach (get_instance()->load->get_package_paths(TRUE) as $path)
Derek Jones8dca0412010-03-05 13:01:44 -060084 {
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010085 // loves me some nesting!
86 foreach (array(ucfirst($driver_name), $driver_name) as $class)
Derek Jones8dca0412010-03-05 13:01:44 -060087 {
Greg Aker3a746652011-04-19 10:59:47 -050088 $filepath = $path.'libraries/'.$lib_name.'/drivers/'.$class.'.php';
Derek Jones8dca0412010-03-05 13:01:44 -060089
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010090 if (file_exists($filepath))
91 {
92 include_once $filepath;
Stéphane Goetz35be6442011-10-24 23:10:07 +030093 break 2;
Derek Jones8dca0412010-03-05 13:01:44 -060094 }
95 }
96 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Jones8dca0412010-03-05 13:01:44 -060098 // it's a valid driver, but the file simply can't be found
99 if ( ! class_exists($child_class))
100 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300101 log_message('error', 'Unable to load the requested driver: '.$child_class);
102 show_error('Unable to load the requested driver: '.$child_class);
Derek Jones8dca0412010-03-05 13:01:44 -0600103 }
104 }
105
106 $obj = new $child_class;
107 $obj->decorate($this);
108 $this->$child = $obj;
109 return $this->$child;
110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Jones8dca0412010-03-05 13:01:44 -0600112 // The requested driver isn't valid!
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300113 log_message('error', 'Invalid driver requested: '.$child_class);
114 show_error('Invalid driver requested: '.$child_class);
Derek Jones8dca0412010-03-05 13:01:44 -0600115 }
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Jones8dca0412010-03-05 13:01:44 -0600117}
Derek Jones8dca0412010-03-05 13:01:44 -0600118
Timothy Warren0688ac92012-04-20 10:25:04 -0400119// --------------------------------------------------------------------------
120
Derek Jones8dca0412010-03-05 13:01:44 -0600121/**
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 Mienydd671972010-10-04 16:33:58 +0200131 * @link
Derek Jones8dca0412010-03-05 13:01:44 -0600132 */
133class CI_Driver {
Andrey Andreev83d15052011-12-22 13:35:42 +0200134
Timothy Warren0688ac92012-04-20 10:25:04 -0400135 /**
136 * Instance of the parent class
137 *
138 * @var object
139 */
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300140 protected $_parent;
Barry Mienydd671972010-10-04 16:33:58 +0200141
Timothy Warren0688ac92012-04-20 10:25:04 -0400142 /**
143 * List of methods in the parent class
144 *
145 * @var array
146 */
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300147 protected $_methods = array();
Timothy Warren0688ac92012-04-20 10:25:04 -0400148
149 /**
150 * List of properties in the parent class
151 *
152 * @var array
153 */
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300154 protected $_properties = array();
Derek Jones8dca0412010-03-05 13:01:44 -0600155
Timothy Warren0688ac92012-04-20 10:25:04 -0400156 /**
157 * Array of methods and properties for the parent class(es)
158 *
159 * @var array
160 */
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300161 protected static $_reflections = array();
Derek Jones8dca0412010-03-05 13:01:44 -0600162
163 /**
164 * Decorate
165 *
166 * Decorates the child with the parent driver lib's methods and properties
167 *
Derek Jones8dca0412010-03-05 13:01:44 -0600168 * @param object
169 * @return void
170 */
Greg Akera9263282010-11-10 15:26:43 -0600171 public function decorate($parent)
Derek Jones8dca0412010-03-05 13:01:44 -0600172 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300173 $this->_parent = $parent;
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Jones8dca0412010-03-05 13:01:44 -0600175 // Lock down attributes to what is defined in the class
176 // and speed up references in magic methods
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Jones8dca0412010-03-05 13:01:44 -0600178 $class_name = get_class($parent);
Barry Mienydd671972010-10-04 16:33:58 +0200179
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300180 if ( ! isset(self::$_reflections[$class_name]))
Derek Jones8dca0412010-03-05 13:01:44 -0600181 {
182 $r = new ReflectionObject($parent);
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Jones8dca0412010-03-05 13:01:44 -0600184 foreach ($r->getMethods() as $method)
185 {
186 if ($method->isPublic())
187 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300188 $this->_methods[] = $method->getName();
Derek Jones8dca0412010-03-05 13:01:44 -0600189 }
190 }
191
Pascal Kriete14287f32011-02-14 13:39:34 -0500192 foreach ($r->getProperties() as $prop)
Derek Jones8dca0412010-03-05 13:01:44 -0600193 {
194 if ($prop->isPublic())
195 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300196 $this->_properties[] = $prop->getName();
Derek Jones8dca0412010-03-05 13:01:44 -0600197 }
198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300200 self::$_reflections[$class_name] = array($this->_methods, $this->_properties);
Derek Jones8dca0412010-03-05 13:01:44 -0600201 }
202 else
203 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300204 list($this->_methods, $this->_properties) = self::$_reflections[$class_name];
Derek Jones8dca0412010-03-05 13:01:44 -0600205 }
206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Jones8dca0412010-03-05 13:01:44 -0600208 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Jones8dca0412010-03-05 13:01:44 -0600210 /**
211 * __call magic method
212 *
213 * Handles access to the parent driver library's methods
214 *
Derek Jones8dca0412010-03-05 13:01:44 -0600215 * @param string
216 * @param array
217 * @return mixed
218 */
219 public function __call($method, $args = array())
220 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300221 if (in_array($method, $this->_methods))
Derek Jones8dca0412010-03-05 13:01:44 -0600222 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300223 return call_user_func_array(array($this->_parent, $method), $args);
Derek Jones8dca0412010-03-05 13:01:44 -0600224 }
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 Mienydd671972010-10-04 16:33:58 +0200232
Derek Jones8dca0412010-03-05 13:01:44 -0600233 /**
234 * __get magic method
235 *
236 * Handles reading of the parent driver library's properties
237 *
Derek Jones8dca0412010-03-05 13:01:44 -0600238 * @param string
239 * @return mixed
240 */
Greg Akera9263282010-11-10 15:26:43 -0600241 public function __get($var)
Derek Jones8dca0412010-03-05 13:01:44 -0600242 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300243 if (in_array($var, $this->_properties))
Derek Jones8dca0412010-03-05 13:01:44 -0600244 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300245 return $this->_parent->$var;
Derek Jones8dca0412010-03-05 13:01:44 -0600246 }
247 }
248
249 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Jones8dca0412010-03-05 13:01:44 -0600251 /**
252 * __set magic method
253 *
254 * Handles writing to the parent driver library's properties
255 *
Derek Jones8dca0412010-03-05 13:01:44 -0600256 * @param string
257 * @param array
258 * @return mixed
259 */
Greg Akera9263282010-11-10 15:26:43 -0600260 public function __set($var, $val)
Derek Jones8dca0412010-03-05 13:01:44 -0600261 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300262 if (in_array($var, $this->_properties))
Derek Jones8dca0412010-03-05 13:01:44 -0600263 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300264 $this->_parent->$var = $val;
Derek Jones8dca0412010-03-05 13:01:44 -0600265 }
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Jones8dca0412010-03-05 13:01:44 -0600268}
Derek Jones8dca0412010-03-05 13:01:44 -0600269
270/* End of file Driver.php */
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300271/* Location: ./system/libraries/Driver.php */