Autoloader.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. // /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | [email protected] so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Bakken <[email protected]> |
  17. // | |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Autoloader.php,v 1.11 2004/02/27 02:21:29 cellog Exp $
  21. if (!extension_loaded("overload")) {
  22. // die hard without ext/overload
  23. die("Rebuild PHP with the `overload' extension to use PEAR_Autoloader");
  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. * @author Stig Sæther Bakken <[email protected]>
  39. */
  40. class PEAR_Autoloader extends PEAR
  41. {
  42. // {{{ properties
  43. /**
  44. * Map of methods and classes where they are defined
  45. *
  46. * @var array
  47. *
  48. * @access private
  49. */
  50. var $_autoload_map = array();
  51. /**
  52. * Map of methods and aggregate objects
  53. *
  54. * @var array
  55. *
  56. * @access private
  57. */
  58. var $_method_map = array();
  59. // }}}
  60. // {{{ addAutoload()
  61. /**
  62. * Add one or more autoload entries.
  63. *
  64. * @param string $method which method to autoload
  65. *
  66. * @param string $classname (optional) which class to find the method in.
  67. * If the $method parameter is an array, this
  68. * parameter may be omitted (and will be ignored
  69. * if not), and the $method parameter will be
  70. * treated as an associative array with method
  71. * names as keys and class names as values.
  72. *
  73. * @return void
  74. *
  75. * @access public
  76. */
  77. function addAutoload($method, $classname = null)
  78. {
  79. if (is_array($method)) {
  80. array_walk($method, create_function('$a,&$b', '$b = strtolower($b);'));
  81. $this->_autoload_map = array_merge($this->_autoload_map, $method);
  82. } else {
  83. $this->_autoload_map[strtolower($method)] = $classname;
  84. }
  85. }
  86. // }}}
  87. // {{{ removeAutoload()
  88. /**
  89. * Remove an autoload entry.
  90. *
  91. * @param string $method which method to remove the autoload entry for
  92. *
  93. * @return bool TRUE if an entry was removed, FALSE if not
  94. *
  95. * @access public
  96. */
  97. function removeAutoload($method)
  98. {
  99. $method = strtolower($method);
  100. $ok = isset($this->_autoload_map[$method]);
  101. unset($this->_autoload_map[$method]);
  102. return $ok;
  103. }
  104. // }}}
  105. // {{{ addAggregateObject()
  106. /**
  107. * Add an aggregate object to this object. If the specified class
  108. * is not defined, loading it will be attempted following PEAR's
  109. * file naming scheme. All the methods in the class will be
  110. * aggregated, except private ones (name starting with an
  111. * underscore) and constructors.
  112. *
  113. * @param string $classname what class to instantiate for the object.
  114. *
  115. * @return void
  116. *
  117. * @access public
  118. */
  119. function addAggregateObject($classname)
  120. {
  121. $classname = strtolower($classname);
  122. if (!class_exists($classname)) {
  123. $include_file = preg_replace('/[^a-z0-9]/i', '_', $classname);
  124. include_once $include_file;
  125. }
  126. $obj =& new $classname;
  127. $methods = get_class_methods($classname);
  128. foreach ($methods as $method) {
  129. // don't import priviate methods and constructors
  130. if ($method{0} != '_' && $method != $classname) {
  131. $this->_method_map[$method] = $obj;
  132. }
  133. }
  134. }
  135. // }}}
  136. // {{{ removeAggregateObject()
  137. /**
  138. * Remove an aggregate object.
  139. *
  140. * @param string $classname the class of the object to remove
  141. *
  142. * @return bool TRUE if an object was removed, FALSE if not
  143. *
  144. * @access public
  145. */
  146. function removeAggregateObject($classname)
  147. {
  148. $ok = false;
  149. $classname = strtolower($classname);
  150. reset($this->_method_map);
  151. while (list($method, $obj) = each($this->_method_map)) {
  152. if (is_a($obj, $classname)) {
  153. unset($this->_method_map[$method]);
  154. $ok = true;
  155. }
  156. }
  157. return $ok;
  158. }
  159. // }}}
  160. // {{{ __call()
  161. /**
  162. * Overloaded object call handler, called each time an
  163. * undefined/aggregated method is invoked. This method repeats
  164. * the call in the right aggregate object and passes on the return
  165. * value.
  166. *
  167. * @param string $method which method that was called
  168. *
  169. * @param string $args An array of the parameters passed in the
  170. * original call
  171. *
  172. * @return mixed The return value from the aggregated method, or a PEAR
  173. * error if the called method was unknown.
  174. */
  175. function __call($method, $args, &$retval)
  176. {
  177. $method = strtolower($method);
  178. if (empty($this->_method_map[$method]) && isset($this->_autoload_map[$method])) {
  179. $this->addAggregateObject($this->_autoload_map[$method]);
  180. }
  181. if (isset($this->_method_map[$method])) {
  182. $retval = call_user_func_array(array($this->_method_map[$method], $method), $args);
  183. return true;
  184. }
  185. return false;
  186. }
  187. // }}}
  188. }
  189. overload("PEAR_Autoloader");
  190. ?>