Autoloader.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * Class auto-loader
  4. *
  5. * PHP versions 4
  6. *
  7. * @category pear
  8. * @package PEAR
  9. * @author Stig Bakken <ssb@php.net>
  10. * @copyright 1997-2009 The Authors
  11. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  12. * @version CVS: $Id: Autoloader.php 313023 2011-07-06 19:17:11Z dufuz $
  13. * @link http://pear.php.net/manual/en/core.ppm.php#core.ppm.pear-autoloader
  14. * @since File available since Release 0.1
  15. * @deprecated File deprecated in Release 1.4.0a1
  16. */
  17. // /* vim: set expandtab tabstop=4 shiftwidth=4: */
  18. if (!extension_loaded("overload")) {
  19. // die hard without ext/overload
  20. die("Rebuild PHP with the `overload' extension to use PEAR_Autoloader");
  21. }
  22. /**
  23. * Include for PEAR_Error and PEAR classes
  24. */
  25. require_once "PEAR.php";
  26. /**
  27. * This class is for objects where you want to separate the code for
  28. * some methods into separate classes. This is useful if you have a
  29. * class with not-frequently-used methods that contain lots of code
  30. * that you would like to avoid always parsing.
  31. *
  32. * The PEAR_Autoloader class provides autoloading and aggregation.
  33. * The autoloading lets you set up in which classes the separated
  34. * methods are found. Aggregation is the technique used to import new
  35. * methods, an instance of each class providing separated methods is
  36. * stored and called every time the aggregated method is called.
  37. *
  38. * @category pear
  39. * @package PEAR
  40. * @author Stig Bakken <ssb@php.net>
  41. * @copyright 1997-2009 The Authors
  42. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  43. * @version Release: 1.9.4
  44. * @link http://pear.php.net/manual/en/core.ppm.php#core.ppm.pear-autoloader
  45. * @since File available since Release 0.1
  46. * @deprecated File deprecated in Release 1.4.0a1
  47. */
  48. class PEAR_Autoloader extends PEAR
  49. {
  50. // {{{ properties
  51. /**
  52. * Map of methods and classes where they are defined
  53. *
  54. * @var array
  55. *
  56. * @access private
  57. */
  58. var $_autoload_map = array();
  59. /**
  60. * Map of methods and aggregate objects
  61. *
  62. * @var array
  63. *
  64. * @access private
  65. */
  66. var $_method_map = array();
  67. // }}}
  68. // {{{ addAutoload()
  69. /**
  70. * Add one or more autoload entries.
  71. *
  72. * @param string $method which method to autoload
  73. *
  74. * @param string $classname (optional) which class to find the method in.
  75. * If the $method parameter is an array, this
  76. * parameter may be omitted (and will be ignored
  77. * if not), and the $method parameter will be
  78. * treated as an associative array with method
  79. * names as keys and class names as values.
  80. *
  81. * @return void
  82. *
  83. * @access public
  84. */
  85. function addAutoload($method, $classname = null)
  86. {
  87. if (is_array($method)) {
  88. array_walk($method, create_function('$a,&$b', '$b = strtolower($b);'));
  89. $this->_autoload_map = array_merge($this->_autoload_map, $method);
  90. } else {
  91. $this->_autoload_map[strtolower($method)] = $classname;
  92. }
  93. }
  94. // }}}
  95. // {{{ removeAutoload()
  96. /**
  97. * Remove an autoload entry.
  98. *
  99. * @param string $method which method to remove the autoload entry for
  100. *
  101. * @return bool TRUE if an entry was removed, FALSE if not
  102. *
  103. * @access public
  104. */
  105. function removeAutoload($method)
  106. {
  107. $method = strtolower($method);
  108. $ok = isset($this->_autoload_map[$method]);
  109. unset($this->_autoload_map[$method]);
  110. return $ok;
  111. }
  112. // }}}
  113. // {{{ addAggregateObject()
  114. /**
  115. * Add an aggregate object to this object. If the specified class
  116. * is not defined, loading it will be attempted following PEAR's
  117. * file naming scheme. All the methods in the class will be
  118. * aggregated, except private ones (name starting with an
  119. * underscore) and constructors.
  120. *
  121. * @param string $classname what class to instantiate for the object.
  122. *
  123. * @return void
  124. *
  125. * @access public
  126. */
  127. function addAggregateObject($classname)
  128. {
  129. $classname = strtolower($classname);
  130. if (!class_exists($classname)) {
  131. $include_file = preg_replace('/[^a-z0-9]/i', '_', $classname);
  132. include_once $include_file;
  133. }
  134. $obj = new $classname;
  135. $methods = get_class_methods($classname);
  136. foreach ($methods as $method) {
  137. // don't import priviate methods and constructors
  138. if ($method{0} != '_' && $method != $classname) {
  139. $this->_method_map[$method] = $obj;
  140. }
  141. }
  142. }
  143. // }}}
  144. // {{{ removeAggregateObject()
  145. /**
  146. * Remove an aggregate object.
  147. *
  148. * @param string $classname the class of the object to remove
  149. *
  150. * @return bool TRUE if an object was removed, FALSE if not
  151. *
  152. * @access public
  153. */
  154. function removeAggregateObject($classname)
  155. {
  156. $ok = false;
  157. $classname = strtolower($classname);
  158. reset($this->_method_map);
  159. while (list($method, $obj) = each($this->_method_map)) {
  160. if (is_a($obj, $classname)) {
  161. unset($this->_method_map[$method]);
  162. $ok = true;
  163. }
  164. }
  165. return $ok;
  166. }
  167. // }}}
  168. // {{{ __call()
  169. /**
  170. * Overloaded object call handler, called each time an
  171. * undefined/aggregated method is invoked. This method repeats
  172. * the call in the right aggregate object and passes on the return
  173. * value.
  174. *
  175. * @param string $method which method that was called
  176. *
  177. * @param string $args An array of the parameters passed in the
  178. * original call
  179. *
  180. * @return mixed The return value from the aggregated method, or a PEAR
  181. * error if the called method was unknown.
  182. */
  183. function __call($method, $args, &$retval)
  184. {
  185. $method = strtolower($method);
  186. if (empty($this->_method_map[$method]) && isset($this->_autoload_map[$method])) {
  187. $this->addAggregateObject($this->_autoload_map[$method]);
  188. }
  189. if (isset($this->_method_map[$method])) {
  190. $retval = call_user_func_array(array($this->_method_map[$method], $method), $args);
  191. return true;
  192. }
  193. return false;
  194. }
  195. // }}}
  196. }
  197. overload("PEAR_Autoloader");
  198. ?>