blob: 183a9598531548c197d2af9d01ff7faad89bea5d [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 *
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
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
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.
Andrey Andreev83d15052011-12-22 13:35:42 +020049 public function __get($child)
Derek Jones8dca0412010-03-05 13:01:44 -060050 {
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;
Andrey Andreev83d15052011-12-22 13:35:42 +020058
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));
Andrey Andreev83d15052011-12-22 13:35:42 +020062
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;
79 break;
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 {
Andrey Andreev83d15052011-12-22 13:35:42 +0200122
Derek Jones8dca0412010-03-05 13:01:44 -0600123 protected $parent;
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Jones8dca0412010-03-05 13:01:44 -0600125 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 Jones8dca0412010-03-05 13:01:44 -0600135 * @param object
136 * @return void
137 */
Greg Akera9263282010-11-10 15:26:43 -0600138 public function decorate($parent)
Derek Jones8dca0412010-03-05 13:01:44 -0600139 {
140 $this->parent = $parent;
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Jones8dca0412010-03-05 13:01:44 -0600142 // Lock down attributes to what is defined in the class
143 // and speed up references in magic methods
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Jones8dca0412010-03-05 13:01:44 -0600145 $class_name = get_class($parent);
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Jones8dca0412010-03-05 13:01:44 -0600147 if ( ! isset(self::$reflections[$class_name]))
148 {
149 $r = new ReflectionObject($parent);
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Jones8dca0412010-03-05 13:01:44 -0600151 foreach ($r->getMethods() as $method)
152 {
153 if ($method->isPublic())
154 {
155 $this->methods[] = $method->getName();
156 }
157 }
158
Pascal Kriete14287f32011-02-14 13:39:34 -0500159 foreach ($r->getProperties() as $prop)
Derek Jones8dca0412010-03-05 13:01:44 -0600160 {
161 if ($prop->isPublic())
162 {
163 $this->properties[] = $prop->getName();
164 }
165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Jones8dca0412010-03-05 13:01:44 -0600167 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 Mienydd671972010-10-04 16:33:58 +0200174
Derek Jones8dca0412010-03-05 13:01:44 -0600175 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Jones8dca0412010-03-05 13:01:44 -0600177 /**
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 Mienydd671972010-10-04 16:33:58 +0200200
Derek Jones8dca0412010-03-05 13:01:44 -0600201 /**
202 * __get magic method
203 *
204 * Handles reading of the parent driver library's properties
205 *
Derek Jones8dca0412010-03-05 13:01:44 -0600206 * @param string
207 * @return mixed
208 */
Greg Akera9263282010-11-10 15:26:43 -0600209 public function __get($var)
Derek Jones8dca0412010-03-05 13:01:44 -0600210 {
211 if (in_array($var, $this->properties))
212 {
213 return $this->parent->$var;
214 }
215 }
216
217 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Jones8dca0412010-03-05 13:01:44 -0600219 /**
220 * __set magic method
221 *
222 * Handles writing to the parent driver library's properties
223 *
Derek Jones8dca0412010-03-05 13:01:44 -0600224 * @param string
225 * @param array
226 * @return mixed
227 */
Greg Akera9263282010-11-10 15:26:43 -0600228 public function __set($var, $val)
Derek Jones8dca0412010-03-05 13:01:44 -0600229 {
230 if (in_array($var, $this->properties))
231 {
232 $this->parent->$var = $val;
233 }
234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Jones8dca0412010-03-05 13:01:44 -0600236 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Jones8dca0412010-03-05 13:01:44 -0600238}
239// END CI_Driver CLASS
240
241/* End of file Driver.php */
Andrey Andreev83d15052011-12-22 13:35:42 +0200242/* Location: ./system/libraries/Driver.php */