Driver.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2006 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * CodeIgniter Driver Library Class
  19. *
  20. * This class enables you to create "Driver" libraries that add runtime ability
  21. * to extend the capabilities of a class via additional driver objects
  22. *
  23. * @package CodeIgniter
  24. * @subpackage Libraries
  25. * @category Libraries
  26. * @author EllisLab Dev Team
  27. * @link
  28. */
  29. class CI_Driver_Library {
  30. protected $valid_drivers = array();
  31. protected $lib_name;
  32. // The first time a child is used it won't exist, so we instantiate it
  33. // subsequents calls will go straight to the proper child.
  34. function __get($child)
  35. {
  36. if ( ! isset($this->lib_name))
  37. {
  38. $this->lib_name = get_class($this);
  39. }
  40. // The class will be prefixed with the parent lib
  41. $child_class = $this->lib_name.'_'.$child;
  42. // Remove the CI_ prefix and lowercase
  43. $lib_name = ucfirst(strtolower(str_replace('CI_', '', $this->lib_name)));
  44. $driver_name = strtolower(str_replace('CI_', '', $child_class));
  45. if (in_array($driver_name, array_map('strtolower', $this->valid_drivers)))
  46. {
  47. // check and see if the driver is in a separate file
  48. if ( ! class_exists($child_class))
  49. {
  50. // check application path first
  51. foreach (get_instance()->load->get_package_paths(TRUE) as $path)
  52. {
  53. // loves me some nesting!
  54. foreach (array(ucfirst($driver_name), $driver_name) as $class)
  55. {
  56. $filepath = $path.'libraries/'.$lib_name.'/drivers/'.$class.'.php';
  57. if (file_exists($filepath))
  58. {
  59. include_once $filepath;
  60. break;
  61. }
  62. }
  63. }
  64. // it's a valid driver, but the file simply can't be found
  65. if ( ! class_exists($child_class))
  66. {
  67. log_message('error', "Unable to load the requested driver: ".$child_class);
  68. show_error("Unable to load the requested driver: ".$child_class);
  69. }
  70. }
  71. $obj = new $child_class;
  72. $obj->decorate($this);
  73. $this->$child = $obj;
  74. return $this->$child;
  75. }
  76. // The requested driver isn't valid!
  77. log_message('error', "Invalid driver requested: ".$child_class);
  78. show_error("Invalid driver requested: ".$child_class);
  79. }
  80. // --------------------------------------------------------------------
  81. }
  82. // END CI_Driver_Library CLASS
  83. /**
  84. * CodeIgniter Driver Class
  85. *
  86. * This class enables you to create drivers for a Library based on the Driver Library.
  87. * It handles the drivers' access to the parent library
  88. *
  89. * @package CodeIgniter
  90. * @subpackage Libraries
  91. * @category Libraries
  92. * @author EllisLab Dev Team
  93. * @link
  94. */
  95. class CI_Driver {
  96. protected $parent;
  97. private $methods = array();
  98. private $properties = array();
  99. private static $reflections = array();
  100. /**
  101. * Decorate
  102. *
  103. * Decorates the child with the parent driver lib's methods and properties
  104. *
  105. * @param object
  106. * @return void
  107. */
  108. public function decorate($parent)
  109. {
  110. $this->parent = $parent;
  111. // Lock down attributes to what is defined in the class
  112. // and speed up references in magic methods
  113. $class_name = get_class($parent);
  114. if ( ! isset(self::$reflections[$class_name]))
  115. {
  116. $r = new ReflectionObject($parent);
  117. foreach ($r->getMethods() as $method)
  118. {
  119. if ($method->isPublic())
  120. {
  121. $this->methods[] = $method->getName();
  122. }
  123. }
  124. foreach ($r->getProperties() as $prop)
  125. {
  126. if ($prop->isPublic())
  127. {
  128. $this->properties[] = $prop->getName();
  129. }
  130. }
  131. self::$reflections[$class_name] = array($this->methods, $this->properties);
  132. }
  133. else
  134. {
  135. list($this->methods, $this->properties) = self::$reflections[$class_name];
  136. }
  137. }
  138. // --------------------------------------------------------------------
  139. /**
  140. * __call magic method
  141. *
  142. * Handles access to the parent driver library's methods
  143. *
  144. * @access public
  145. * @param string
  146. * @param array
  147. * @return mixed
  148. */
  149. public function __call($method, $args = array())
  150. {
  151. if (in_array($method, $this->methods))
  152. {
  153. return call_user_func_array(array($this->parent, $method), $args);
  154. }
  155. $trace = debug_backtrace();
  156. _exception_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']);
  157. exit;
  158. }
  159. // --------------------------------------------------------------------
  160. /**
  161. * __get magic method
  162. *
  163. * Handles reading of the parent driver library's properties
  164. *
  165. * @param string
  166. * @return mixed
  167. */
  168. public function __get($var)
  169. {
  170. if (in_array($var, $this->properties))
  171. {
  172. return $this->parent->$var;
  173. }
  174. }
  175. // --------------------------------------------------------------------
  176. /**
  177. * __set magic method
  178. *
  179. * Handles writing to the parent driver library's properties
  180. *
  181. * @param string
  182. * @param array
  183. * @return mixed
  184. */
  185. public function __set($var, $val)
  186. {
  187. if (in_array($var, $this->properties))
  188. {
  189. $this->parent->$var = $val;
  190. }
  191. }
  192. }
  193. // END CI_Driver CLASS
  194. /* End of file Driver.php */
  195. /* Location: ./system/libraries/Driver.php */