PageRenderTime 42ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/system/libraries/Driver.php

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