blob: f409f47d4263999d2fbb8827ac90377124236b64 [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
42 protected $valid_drivers = array();
43 protected static $lib_name;
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Jones8dca0412010-03-05 13:01:44 -060045 // The first time a child is used it won't exist, so we instantiate it
46 // subsequents calls will go straight to the proper child.
Andrey Andreev83d15052011-12-22 13:35:42 +020047 public function __get($child)
Derek Jones8dca0412010-03-05 13:01:44 -060048 {
Pascal Kriete14287f32011-02-14 13:39:34 -050049 if ( ! isset($this->lib_name))
Derek Jones8dca0412010-03-05 13:01:44 -060050 {
Barry Mienydd671972010-10-04 16:33:58 +020051 $this->lib_name = get_class($this);
Derek Jones8dca0412010-03-05 13:01:44 -060052 }
53
54 // The class will be prefixed with the parent lib
55 $child_class = $this->lib_name.'_'.$child;
Andrey Andreev83d15052011-12-22 13:35:42 +020056
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010057 // Remove the CI_ prefix and lowercase
Greg Aker28bda7f2011-04-25 15:00:45 -050058 $lib_name = ucfirst(strtolower(str_replace('CI_', '', $this->lib_name)));
59 $driver_name = strtolower(str_replace('CI_', '', $child_class));
Andrey Andreev83d15052011-12-22 13:35:42 +020060
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010061 if (in_array($driver_name, array_map('strtolower', $this->valid_drivers)))
Derek Jones8dca0412010-03-05 13:01:44 -060062 {
63 // check and see if the driver is in a separate file
64 if ( ! class_exists($child_class))
65 {
66 // check application path first
Phil Sturgeon02d45b52011-08-14 09:56:47 -060067 foreach (get_instance()->load->get_package_paths(TRUE) as $path)
Derek Jones8dca0412010-03-05 13:01:44 -060068 {
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010069 // loves me some nesting!
70 foreach (array(ucfirst($driver_name), $driver_name) as $class)
Derek Jones8dca0412010-03-05 13:01:44 -060071 {
Greg Aker3a746652011-04-19 10:59:47 -050072 $filepath = $path.'libraries/'.$lib_name.'/drivers/'.$class.'.php';
Derek Jones8dca0412010-03-05 13:01:44 -060073
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010074 if (file_exists($filepath))
75 {
76 include_once $filepath;
Stéphane Goetz35be6442011-10-24 23:10:07 +030077 break 2;
Derek Jones8dca0412010-03-05 13:01:44 -060078 }
79 }
80 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Jones8dca0412010-03-05 13:01:44 -060082 // it's a valid driver, but the file simply can't be found
83 if ( ! class_exists($child_class))
84 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +030085 log_message('error', 'Unable to load the requested driver: '.$child_class);
86 show_error('Unable to load the requested driver: '.$child_class);
Derek Jones8dca0412010-03-05 13:01:44 -060087 }
88 }
89
90 $obj = new $child_class;
91 $obj->decorate($this);
92 $this->$child = $obj;
93 return $this->$child;
94 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Jones8dca0412010-03-05 13:01:44 -060096 // The requested driver isn't valid!
Andrey Andreev8486e9c2012-03-26 20:24:12 +030097 log_message('error', 'Invalid driver requested: '.$child_class);
98 show_error('Invalid driver requested: '.$child_class);
Derek Jones8dca0412010-03-05 13:01:44 -060099 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
Derek Jones8dca0412010-03-05 13:01:44 -0600101}
Derek Jones8dca0412010-03-05 13:01:44 -0600102
103/**
104 * CodeIgniter Driver Class
105 *
106 * This class enables you to create drivers for a Library based on the Driver Library.
107 * It handles the drivers' access to the parent library
108 *
109 * @package CodeIgniter
110 * @subpackage Libraries
111 * @category Libraries
112 * @author EllisLab Dev Team
Barry Mienydd671972010-10-04 16:33:58 +0200113 * @link
Derek Jones8dca0412010-03-05 13:01:44 -0600114 */
115class CI_Driver {
Andrey Andreev83d15052011-12-22 13:35:42 +0200116
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300117 protected $_parent;
Barry Mienydd671972010-10-04 16:33:58 +0200118
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300119 protected $_methods = array();
120 protected $_properties = array();
Derek Jones8dca0412010-03-05 13:01:44 -0600121
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300122 protected static $_reflections = array();
Derek Jones8dca0412010-03-05 13:01:44 -0600123
124 /**
125 * Decorate
126 *
127 * Decorates the child with the parent driver lib's methods and properties
128 *
Derek Jones8dca0412010-03-05 13:01:44 -0600129 * @param object
130 * @return void
131 */
Greg Akera9263282010-11-10 15:26:43 -0600132 public function decorate($parent)
Derek Jones8dca0412010-03-05 13:01:44 -0600133 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300134 $this->_parent = $parent;
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Jones8dca0412010-03-05 13:01:44 -0600136 // Lock down attributes to what is defined in the class
137 // and speed up references in magic methods
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Jones8dca0412010-03-05 13:01:44 -0600139 $class_name = get_class($parent);
Barry Mienydd671972010-10-04 16:33:58 +0200140
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300141 if ( ! isset(self::$_reflections[$class_name]))
Derek Jones8dca0412010-03-05 13:01:44 -0600142 {
143 $r = new ReflectionObject($parent);
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Jones8dca0412010-03-05 13:01:44 -0600145 foreach ($r->getMethods() as $method)
146 {
147 if ($method->isPublic())
148 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300149 $this->_methods[] = $method->getName();
Derek Jones8dca0412010-03-05 13:01:44 -0600150 }
151 }
152
Pascal Kriete14287f32011-02-14 13:39:34 -0500153 foreach ($r->getProperties() as $prop)
Derek Jones8dca0412010-03-05 13:01:44 -0600154 {
155 if ($prop->isPublic())
156 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300157 $this->_properties[] = $prop->getName();
Derek Jones8dca0412010-03-05 13:01:44 -0600158 }
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300161 self::$_reflections[$class_name] = array($this->_methods, $this->_properties);
Derek Jones8dca0412010-03-05 13:01:44 -0600162 }
163 else
164 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300165 list($this->_methods, $this->_properties) = self::$_reflections[$class_name];
Derek Jones8dca0412010-03-05 13:01:44 -0600166 }
167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Jones8dca0412010-03-05 13:01:44 -0600169 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Jones8dca0412010-03-05 13:01:44 -0600171 /**
172 * __call magic method
173 *
174 * Handles access to the parent driver library's methods
175 *
Derek Jones8dca0412010-03-05 13:01:44 -0600176 * @param string
177 * @param array
178 * @return mixed
179 */
180 public function __call($method, $args = array())
181 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300182 if (in_array($method, $this->_methods))
Derek Jones8dca0412010-03-05 13:01:44 -0600183 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300184 return call_user_func_array(array($this->_parent, $method), $args);
Derek Jones8dca0412010-03-05 13:01:44 -0600185 }
186
187 $trace = debug_backtrace();
188 _exception_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']);
189 exit;
190 }
191
192 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Jones8dca0412010-03-05 13:01:44 -0600194 /**
195 * __get magic method
196 *
197 * Handles reading of the parent driver library's properties
198 *
Derek Jones8dca0412010-03-05 13:01:44 -0600199 * @param string
200 * @return mixed
201 */
Greg Akera9263282010-11-10 15:26:43 -0600202 public function __get($var)
Derek Jones8dca0412010-03-05 13:01:44 -0600203 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300204 if (in_array($var, $this->_properties))
Derek Jones8dca0412010-03-05 13:01:44 -0600205 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300206 return $this->_parent->$var;
Derek Jones8dca0412010-03-05 13:01:44 -0600207 }
208 }
209
210 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Jones8dca0412010-03-05 13:01:44 -0600212 /**
213 * __set magic method
214 *
215 * Handles writing to the parent driver library's properties
216 *
Derek Jones8dca0412010-03-05 13:01:44 -0600217 * @param string
218 * @param array
219 * @return mixed
220 */
Greg Akera9263282010-11-10 15:26:43 -0600221 public function __set($var, $val)
Derek Jones8dca0412010-03-05 13:01:44 -0600222 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300223 if (in_array($var, $this->_properties))
Derek Jones8dca0412010-03-05 13:01:44 -0600224 {
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300225 $this->_parent->$var = $val;
Derek Jones8dca0412010-03-05 13:01:44 -0600226 }
227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Jones8dca0412010-03-05 13:01:44 -0600229}
Derek Jones8dca0412010-03-05 13:01:44 -0600230
231/* End of file Driver.php */
Andrey Andreev8486e9c2012-03-26 20:24:12 +0300232/* Location: ./system/libraries/Driver.php */