blob: 30204d0756b64ddcebc096b7e28bcbf272fe7834 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
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 Jones8dca0412010-03-05 13:01:44 -060019 * @package CodeIgniter
20 * @author EllisLab Dev Team
Derek Jonesf4a4bd82011-10-20 12:18:42 -050021 * @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 Jones8dca0412010-03-05 13:01:44 -060023 * @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 Mienydd671972010-10-04 16:33:58 +020040 * @link
Derek Jones8dca0412010-03-05 13:01:44 -060041 */
42class CI_Driver_Library {
43
44 protected $valid_drivers = array();
45 protected static $lib_name;
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Jones8dca0412010-03-05 13:01:44 -060047 // 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.
49 function __get($child)
50 {
Pascal Kriete14287f32011-02-14 13:39:34 -050051 if ( ! isset($this->lib_name))
Derek Jones8dca0412010-03-05 13:01:44 -060052 {
Barry Mienydd671972010-10-04 16:33:58 +020053 $this->lib_name = get_class($this);
Derek Jones8dca0412010-03-05 13:01:44 -060054 }
55
56 // The class will be prefixed with the parent lib
57 $child_class = $this->lib_name.'_'.$child;
Greg Aker28bda7f2011-04-25 15:00:45 -050058
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010059 // Remove the CI_ prefix and lowercase
Greg Aker28bda7f2011-04-25 15:00:45 -050060 $lib_name = ucfirst(strtolower(str_replace('CI_', '', $this->lib_name)));
61 $driver_name = strtolower(str_replace('CI_', '', $child_class));
62
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010063 if (in_array($driver_name, array_map('strtolower', $this->valid_drivers)))
Derek Jones8dca0412010-03-05 13:01:44 -060064 {
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 Sturgeon02d45b52011-08-14 09:56:47 -060069 foreach (get_instance()->load->get_package_paths(TRUE) as $path)
Derek Jones8dca0412010-03-05 13:01:44 -060070 {
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010071 // loves me some nesting!
72 foreach (array(ucfirst($driver_name), $driver_name) as $class)
Derek Jones8dca0412010-03-05 13:01:44 -060073 {
Greg Aker3a746652011-04-19 10:59:47 -050074 $filepath = $path.'libraries/'.$lib_name.'/drivers/'.$class.'.php';
Derek Jones8dca0412010-03-05 13:01:44 -060075
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010076 if (file_exists($filepath))
77 {
78 include_once $filepath;
Stéphane Goetz35be6442011-10-24 23:10:07 +030079 break 2;
Derek Jones8dca0412010-03-05 13:01:44 -060080 }
81 }
82 }
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Jones8dca0412010-03-05 13:01:44 -060084 // 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 Mienydd671972010-10-04 16:33:58 +020097
Derek Jones8dca0412010-03-05 13:01:44 -060098 // 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 Mienydd671972010-10-04 16:33:58 +0200102
Derek Jones8dca0412010-03-05 13:01:44 -0600103 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Jones8dca0412010-03-05 13:01:44 -0600105}
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 Mienydd671972010-10-04 16:33:58 +0200119 * @link
Derek Jones8dca0412010-03-05 13:01:44 -0600120 */
121class CI_Driver {
122 protected $parent;
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Jones8dca0412010-03-05 13:01:44 -0600124 private $methods = array();
125 private $properties = array();
126
127 private static $reflections = array();
128
129 /**
130 * Decorate
131 *
132 * Decorates the child with the parent driver lib's methods and properties
133 *
Derek Jones8dca0412010-03-05 13:01:44 -0600134 * @param object
135 * @return void
136 */
Greg Akera9263282010-11-10 15:26:43 -0600137 public function decorate($parent)
Derek Jones8dca0412010-03-05 13:01:44 -0600138 {
139 $this->parent = $parent;
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Jones8dca0412010-03-05 13:01:44 -0600141 // Lock down attributes to what is defined in the class
142 // and speed up references in magic methods
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Jones8dca0412010-03-05 13:01:44 -0600144 $class_name = get_class($parent);
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Jones8dca0412010-03-05 13:01:44 -0600146 if ( ! isset(self::$reflections[$class_name]))
147 {
148 $r = new ReflectionObject($parent);
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Jones8dca0412010-03-05 13:01:44 -0600150 foreach ($r->getMethods() as $method)
151 {
152 if ($method->isPublic())
153 {
154 $this->methods[] = $method->getName();
155 }
156 }
157
Pascal Kriete14287f32011-02-14 13:39:34 -0500158 foreach ($r->getProperties() as $prop)
Derek Jones8dca0412010-03-05 13:01:44 -0600159 {
160 if ($prop->isPublic())
161 {
162 $this->properties[] = $prop->getName();
163 }
164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Jones8dca0412010-03-05 13:01:44 -0600166 self::$reflections[$class_name] = array($this->methods, $this->properties);
167 }
168 else
169 {
170 list($this->methods, $this->properties) = self::$reflections[$class_name];
171 }
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Jones8dca0412010-03-05 13:01:44 -0600174 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Jones8dca0412010-03-05 13:01:44 -0600176 /**
177 * __call magic method
178 *
179 * Handles access to the parent driver library's methods
180 *
181 * @access public
182 * @param string
183 * @param array
184 * @return mixed
185 */
186 public function __call($method, $args = array())
187 {
188 if (in_array($method, $this->methods))
189 {
190 return call_user_func_array(array($this->parent, $method), $args);
191 }
192
193 $trace = debug_backtrace();
194 _exception_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']);
195 exit;
196 }
197
198 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Jones8dca0412010-03-05 13:01:44 -0600200 /**
201 * __get magic method
202 *
203 * Handles reading of the parent driver library's properties
204 *
Derek Jones8dca0412010-03-05 13:01:44 -0600205 * @param string
206 * @return mixed
207 */
Greg Akera9263282010-11-10 15:26:43 -0600208 public function __get($var)
Derek Jones8dca0412010-03-05 13:01:44 -0600209 {
210 if (in_array($var, $this->properties))
211 {
212 return $this->parent->$var;
213 }
214 }
215
216 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Jones8dca0412010-03-05 13:01:44 -0600218 /**
219 * __set magic method
220 *
221 * Handles writing to the parent driver library's properties
222 *
Derek Jones8dca0412010-03-05 13:01:44 -0600223 * @param string
224 * @param array
225 * @return mixed
226 */
Greg Akera9263282010-11-10 15:26:43 -0600227 public function __set($var, $val)
Derek Jones8dca0412010-03-05 13:01:44 -0600228 {
229 if (in_array($var, $this->properties))
230 {
231 $this->parent->$var = $val;
232 }
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Jones8dca0412010-03-05 13:01:44 -0600235 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Jones8dca0412010-03-05 13:01:44 -0600237}
238// END CI_Driver CLASS
239
240/* End of file Driver.php */
Phil Sturgeon02d45b52011-08-14 09:56:47 -0600241/* Location: ./system/libraries/Driver.php */