PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/PEAR/Autoloader.php

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