You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
8.1 KiB

7 years ago
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Driver Library Class
  41. *
  42. * This class enables you to create "Driver" libraries that add runtime ability
  43. * to extend the capabilities of a class via additional driver objects
  44. *
  45. * @package CodeIgniter
  46. * @subpackage Libraries
  47. * @category Libraries
  48. * @author EllisLab Dev Team
  49. * @link
  50. */
  51. class CI_Driver_Library {
  52. /**
  53. * Array of drivers that are available to use with the driver class
  54. *
  55. * @var array
  56. */
  57. protected $valid_drivers = array();
  58. /**
  59. * Name of the current class - usually the driver class
  60. *
  61. * @var string
  62. */
  63. protected $lib_name;
  64. /**
  65. * Get magic method
  66. *
  67. * The first time a child is used it won't exist, so we instantiate it
  68. * subsequents calls will go straight to the proper child.
  69. *
  70. * @param string Child class name
  71. * @return object Child class
  72. */
  73. public function __get($child)
  74. {
  75. // Try to load the driver
  76. return $this->load_driver($child);
  77. }
  78. /**
  79. * Load driver
  80. *
  81. * Separate load_driver call to support explicit driver load by library or user
  82. *
  83. * @param string Driver name (w/o parent prefix)
  84. * @return object Child class
  85. */
  86. public function load_driver($child)
  87. {
  88. // Get CodeIgniter instance and subclass prefix
  89. $prefix = config_item('subclass_prefix');
  90. if ( ! isset($this->lib_name))
  91. {
  92. // Get library name without any prefix
  93. $this->lib_name = str_replace(array('CI_', $prefix), '', get_class($this));
  94. }
  95. // The child will be prefixed with the parent lib
  96. $child_name = $this->lib_name.'_'.$child;
  97. // See if requested child is a valid driver
  98. if ( ! in_array($child, $this->valid_drivers))
  99. {
  100. // The requested driver isn't valid!
  101. $msg = 'Invalid driver requested: '.$child_name;
  102. log_message('error', $msg);
  103. show_error($msg);
  104. }
  105. // Get package paths and filename case variations to search
  106. $CI = get_instance();
  107. $paths = $CI->load->get_package_paths(TRUE);
  108. // Is there an extension?
  109. $class_name = $prefix.$child_name;
  110. $found = class_exists($class_name, FALSE);
  111. if ( ! $found)
  112. {
  113. // Check for subclass file
  114. foreach ($paths as $path)
  115. {
  116. // Does the file exist?
  117. $file = $path.'libraries/'.$this->lib_name.'/drivers/'.$prefix.$child_name.'.php';
  118. if (file_exists($file))
  119. {
  120. // Yes - require base class from BASEPATH
  121. $basepath = BASEPATH.'libraries/'.$this->lib_name.'/drivers/'.$child_name.'.php';
  122. if ( ! file_exists($basepath))
  123. {
  124. $msg = 'Unable to load the requested class: CI_'.$child_name;
  125. log_message('error', $msg);
  126. show_error($msg);
  127. }
  128. // Include both sources and mark found
  129. include_once($basepath);
  130. include_once($file);
  131. $found = TRUE;
  132. break;
  133. }
  134. }
  135. }
  136. // Do we need to search for the class?
  137. if ( ! $found)
  138. {
  139. // Use standard class name
  140. $class_name = 'CI_'.$child_name;
  141. if ( ! class_exists($class_name, FALSE))
  142. {
  143. // Check package paths
  144. foreach ($paths as $path)
  145. {
  146. // Does the file exist?
  147. $file = $path.'libraries/'.$this->lib_name.'/drivers/'.$child_name.'.php';
  148. if (file_exists($file))
  149. {
  150. // Include source
  151. include_once($file);
  152. break;
  153. }
  154. }
  155. }
  156. }
  157. // Did we finally find the class?
  158. if ( ! class_exists($class_name, FALSE))
  159. {
  160. if (class_exists($child_name, FALSE))
  161. {
  162. $class_name = $child_name;
  163. }
  164. else
  165. {
  166. $msg = 'Unable to load the requested driver: '.$class_name;
  167. log_message('error', $msg);
  168. show_error($msg);
  169. }
  170. }
  171. // Instantiate, decorate and add child
  172. $obj = new $class_name();
  173. $obj->decorate($this);
  174. $this->$child = $obj;
  175. return $this->$child;
  176. }
  177. }
  178. // --------------------------------------------------------------------------
  179. /**
  180. * CodeIgniter Driver Class
  181. *
  182. * This class enables you to create drivers for a Library based on the Driver Library.
  183. * It handles the drivers' access to the parent library
  184. *
  185. * @package CodeIgniter
  186. * @subpackage Libraries
  187. * @category Libraries
  188. * @author EllisLab Dev Team
  189. * @link
  190. */
  191. class CI_Driver {
  192. /**
  193. * Instance of the parent class
  194. *
  195. * @var object
  196. */
  197. protected $_parent;
  198. /**
  199. * List of methods in the parent class
  200. *
  201. * @var array
  202. */
  203. protected $_methods = array();
  204. /**
  205. * List of properties in the parent class
  206. *
  207. * @var array
  208. */
  209. protected $_properties = array();
  210. /**
  211. * Array of methods and properties for the parent class(es)
  212. *
  213. * @static
  214. * @var array
  215. */
  216. protected static $_reflections = array();
  217. /**
  218. * Decorate
  219. *
  220. * Decorates the child with the parent driver lib's methods and properties
  221. *
  222. * @param object
  223. * @return void
  224. */
  225. public function decorate($parent)
  226. {
  227. $this->_parent = $parent;
  228. // Lock down attributes to what is defined in the class
  229. // and speed up references in magic methods
  230. $class_name = get_class($parent);
  231. if ( ! isset(self::$_reflections[$class_name]))
  232. {
  233. $r = new ReflectionObject($parent);
  234. foreach ($r->getMethods() as $method)
  235. {
  236. if ($method->isPublic())
  237. {
  238. $this->_methods[] = $method->getName();
  239. }
  240. }
  241. foreach ($r->getProperties() as $prop)
  242. {
  243. if ($prop->isPublic())
  244. {
  245. $this->_properties[] = $prop->getName();
  246. }
  247. }
  248. self::$_reflections[$class_name] = array($this->_methods, $this->_properties);
  249. }
  250. else
  251. {
  252. list($this->_methods, $this->_properties) = self::$_reflections[$class_name];
  253. }
  254. }
  255. // --------------------------------------------------------------------
  256. /**
  257. * __call magic method
  258. *
  259. * Handles access to the parent driver library's methods
  260. *
  261. * @param string
  262. * @param array
  263. * @return mixed
  264. */
  265. public function __call($method, $args = array())
  266. {
  267. if (in_array($method, $this->_methods))
  268. {
  269. return call_user_func_array(array($this->_parent, $method), $args);
  270. }
  271. throw new BadMethodCallException('No such method: '.$method.'()');
  272. }
  273. // --------------------------------------------------------------------
  274. /**
  275. * __get magic method
  276. *
  277. * Handles reading of the parent driver library's properties
  278. *
  279. * @param string
  280. * @return mixed
  281. */
  282. public function __get($var)
  283. {
  284. if (in_array($var, $this->_properties))
  285. {
  286. return $this->_parent->$var;
  287. }
  288. }
  289. // --------------------------------------------------------------------
  290. /**
  291. * __set magic method
  292. *
  293. * Handles writing to the parent driver library's properties
  294. *
  295. * @param string
  296. * @param array
  297. * @return mixed
  298. */
  299. public function __set($var, $val)
  300. {
  301. if (in_array($var, $this->_properties))
  302. {
  303. $this->_parent->$var = $val;
  304. }
  305. }
  306. }