/plugins_repo/openXWorkflow/www/admin/plugins/openXWorkflow/library/Zend/Loader/Autoloader.php

https://github.com/orchestra-io/sample-openx · PHP · 464 lines · 231 code · 46 blank · 187 comment · 25 complexity · 141547e9c57c05b4791639937a222899 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Loader
  17. * @subpackage Autoloader
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Autoloader.php 15508 2009-05-11 03:29:01Z matthew $
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Loader */
  23. require_once 'Zend/Loader.php';
  24. /**
  25. * Autoloader stack and namespace autoloader
  26. *
  27. * @uses Zend_Loader_Autoloader
  28. * @package Zend_Loader
  29. * @subpackage Autoloader
  30. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license New BSD {@link http://framework.zend.com/license/new-bsd}
  32. */
  33. class Zend_Loader_Autoloader
  34. {
  35. /**
  36. * @var Zend_Loader_Autoloader Singleton instance
  37. */
  38. protected static $_instance;
  39. /**
  40. * @var array Concrete autoloader callback implementations
  41. */
  42. protected $_autoloaders = array();
  43. /**
  44. * @var array Default autoloader callback
  45. */
  46. protected $_defaultAutoloader = array('Zend_Loader', 'loadClass');
  47. /**
  48. * @var bool Whether or not to act as a fallback autoloader
  49. */
  50. protected $_fallbackAutoloader = false;
  51. /**
  52. * @var array Callback for internal autoloader implementation
  53. */
  54. protected $_internalAutoloader;
  55. /**
  56. * @var array Supported namespaces 'Zend' and 'ZendX' by default.
  57. */
  58. protected $_namespaces = array(
  59. 'Zend_' => true,
  60. 'ZendX_' => true,
  61. );
  62. /**
  63. * @var array Namespace-specific autoloaders
  64. */
  65. protected $_namespaceAutoloaders = array();
  66. /**
  67. * @var bool Whether or not to suppress file not found warnings
  68. */
  69. protected $_suppressNotFoundWarnings = false;
  70. /**
  71. * Retrieve singleton instance
  72. *
  73. * @return Zend_Loader_Autoloader
  74. */
  75. public static function getInstance()
  76. {
  77. if (null === self::$_instance) {
  78. self::$_instance = new self();
  79. }
  80. return self::$_instance;
  81. }
  82. /**
  83. * Reset the singleton instance
  84. *
  85. * @return void
  86. */
  87. public static function resetInstance()
  88. {
  89. self::$_instance = null;
  90. }
  91. /**
  92. * Autoload a class
  93. *
  94. * @param string $class
  95. * @return bool
  96. */
  97. public static function autoload($class)
  98. {
  99. $self = self::getInstance();
  100. foreach ($self->getClassAutoloaders($class) as $autoloader) {
  101. if ($autoloader instanceof Zend_Loader_Autoloader_Interface) {
  102. if ($autoloader->autoload($class)) {
  103. return true;
  104. }
  105. } elseif (is_string($autoloader)) {
  106. if ($autoloader($class)) {
  107. return true;
  108. }
  109. } elseif (is_array($autoloader)) {
  110. $object = array_shift($autoloader);
  111. $method = array_shift($autoloader);
  112. if (call_user_func(array($object, $method), $class)) {
  113. return true;
  114. }
  115. }
  116. }
  117. return false;
  118. }
  119. /**
  120. * Set the default autoloader implementation
  121. *
  122. * @param string|array $callback PHP callback
  123. * @return void
  124. */
  125. public function setDefaultAutoloader($callback)
  126. {
  127. if (!is_callable($callback)) {
  128. throw new Zend_Loader_Exception('Invalid callback specified for default autoloader');
  129. }
  130. $this->_defaultAutoloader = $callback;
  131. return $this;
  132. }
  133. /**
  134. * Retrieve the default autoloader callback
  135. *
  136. * @return string|array PHP Callback
  137. */
  138. public function getDefaultAutoloader()
  139. {
  140. return $this->_defaultAutoloader;
  141. }
  142. /**
  143. * Set several autoloader callbacks at once
  144. *
  145. * @param array $autoloaders Array of PHP callbacks (or Zend_Loader_Autoloader_Interface implementations) to act as autoloaders
  146. * @return Zend_Loader_Autoloader
  147. */
  148. public function setAutoloaders(array $autoloaders)
  149. {
  150. $this->_autoloaders = $autoloaders;
  151. return $this;
  152. }
  153. /**
  154. * Get attached autoloader implementations
  155. *
  156. * @return array
  157. */
  158. public function getAutoloaders()
  159. {
  160. return $this->_autoloaders;
  161. }
  162. /**
  163. * Return all autoloaders for a given namespace
  164. *
  165. * @param string $namespace
  166. * @return array
  167. */
  168. public function getNamespaceAutoloaders($namespace)
  169. {
  170. $namespace = (string) $namespace;
  171. if (!array_key_exists($namespace, $this->_namespaceAutoloaders)) {
  172. return array();
  173. }
  174. return $this->_namespaceAutoloaders[$namespace];
  175. }
  176. /**
  177. * Register a namespace to autoload
  178. *
  179. * @param string $namespace
  180. * @return Zend_Loader_Autoloader
  181. */
  182. public function registerNamespace($namespace)
  183. {
  184. if (is_string($namespace)) {
  185. $namespace = (array) $namespace;
  186. } elseif (!is_array($namespace)) {
  187. throw new Zend_Loader_Exception('Invalid namespace provided');
  188. }
  189. foreach ($namespace as $ns) {
  190. if (!isset($this->_namespaces[$ns])) {
  191. $this->_namespaces[$ns] = true;
  192. }
  193. }
  194. return $this;
  195. }
  196. /**
  197. * Unload a registered autoload namespace
  198. *
  199. * @param string $namespace
  200. * @return Zend_Loader_Autoloader
  201. */
  202. public function unregisterNamespace($namespace)
  203. {
  204. if (is_string($namespace)) {
  205. $namespace = (array) $namespace;
  206. } elseif (!is_array($namespace)) {
  207. throw new Zend_Loader_Exception('Invalid namespace provided');
  208. }
  209. foreach ($namespace as $ns) {
  210. if (isset($this->_namespaces[$ns])) {
  211. unset($this->_namespaces[$ns]);
  212. }
  213. }
  214. return $this;
  215. }
  216. /**
  217. * Get a list of registered autoload namespaces
  218. *
  219. * @return array
  220. */
  221. public function getRegisteredNamespaces()
  222. {
  223. return array_keys($this->_namespaces);
  224. }
  225. /**
  226. * Get or set the value of the "suppress not found warnings" flag
  227. *
  228. * @param null|bool $flag
  229. * @return bool|Zend_Loader_Autoloader Returns boolean if no argument is passed, object instance otherwise
  230. */
  231. public function suppressNotFoundWarnings($flag = null)
  232. {
  233. if (null === $flag) {
  234. return $this->_suppressNotFoundWarnings;
  235. }
  236. $this->_suppressNotFoundWarnings = (bool) $flag;
  237. return $this;
  238. }
  239. /**
  240. * Indicate whether or not this autoloader should be a fallback autoloader
  241. *
  242. * @param bool $flag
  243. * @return Zend_Loader_Autoloader
  244. */
  245. public function setFallbackAutoloader($flag)
  246. {
  247. $this->_fallbackAutoloader = (bool) $flag;
  248. return $this;
  249. }
  250. /**
  251. * Is this instance acting as a fallback autoloader?
  252. *
  253. * @return bool
  254. */
  255. public function isFallbackAutoloader()
  256. {
  257. return $this->_fallbackAutoloader;
  258. }
  259. /**
  260. * Get autoloaders to use when matching class
  261. *
  262. * Determines if the class matches a registered namespace, and, if so,
  263. * returns only the autoloaders for that namespace. Otherwise, it returns
  264. * all non-namespaced autoloaders.
  265. *
  266. * @param string $class
  267. * @return array Array of autoloaders to use
  268. */
  269. public function getClassAutoloaders($class)
  270. {
  271. $namespace = false;
  272. $autoloaders = array();
  273. // Add concrete namespaced autoloaders
  274. foreach (array_keys($this->_namespaceAutoloaders) as $ns) {
  275. if ('' == $ns) {
  276. continue;
  277. }
  278. if (0 === strpos($class, $ns)) {
  279. $namespace = $ns;
  280. $autoloaders = $autoloaders + $this->getNamespaceAutoloaders($ns);
  281. break;
  282. }
  283. }
  284. // Add internal namespaced autoloader
  285. foreach ($this->getRegisteredNamespaces() as $ns) {
  286. if (0 === strpos($class, $ns)) {
  287. $namespace = $ns;
  288. $autoloaders[] = $this->_internalAutoloader;
  289. break;
  290. }
  291. }
  292. // Add non-namespaced autoloaders
  293. $autoloaders = $autoloaders + $this->getNamespaceAutoloaders('');
  294. // Add fallback autoloader
  295. if (!$namespace && $this->isFallbackAutoloader()) {
  296. $autoloaders[] = $this->_internalAutoloader;
  297. }
  298. return $autoloaders;
  299. }
  300. /**
  301. * Add an autoloader to the beginning of the stack
  302. *
  303. * @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
  304. * @param string|array $namespace Specific namespace(s) under which to register callback
  305. * @return Zend_Loader_Autoloader
  306. */
  307. public function unshiftAutoloader($callback, $namespace = '')
  308. {
  309. $autoloaders = $this->getAutoloaders();
  310. array_unshift($autoloaders, $callback);
  311. $this->setAutoloaders($autoloaders);
  312. $namespace = (array) $namespace;
  313. foreach ($namespace as $ns) {
  314. $autoloaders = $this->getNamespaceAutoloaders($ns);
  315. array_unshift($autoloaders, $callback);
  316. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  317. }
  318. return $this;
  319. }
  320. /**
  321. * Append an autoloader to the autoloader stack
  322. *
  323. * @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
  324. * @param string|array $namespace Specific namespace(s) under which to register callback
  325. * @return Zend_Loader_Autoloader
  326. */
  327. public function pushAutoloader($callback, $namespace = '')
  328. {
  329. $autoloaders = $this->getAutoloaders();
  330. array_push($autoloaders, $callback);
  331. $this->setAutoloaders($autoloaders);
  332. $namespace = (array) $namespace;
  333. foreach ($namespace as $ns) {
  334. $autoloaders = $this->getNamespaceAutoloaders($ns);
  335. array_push($autoloaders, $callback);
  336. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  337. }
  338. return $this;
  339. }
  340. /**
  341. * Remove an autoloader from the autoloader stack
  342. *
  343. * @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
  344. * @param null|string|array $namespace Specific namespace(s) from which to remove autoloader
  345. * @return Zend_Loader_Autoloader
  346. */
  347. public function removeAutoloader($callback, $namespace = null)
  348. {
  349. if (null === $namespace) {
  350. $autoloaders = $this->getAutoloaders();
  351. if (false !== ($index = array_search($callback, $autoloaders, true))) {
  352. unset($autoloaders[$index]);
  353. $this->setAutoloaders($autoloaders);
  354. }
  355. foreach ($this->_namespaceAutoloaders as $ns => $autoloaders) {
  356. if (false !== ($index = array_search($callback, $autoloaders, true))) {
  357. unset($autoloaders[$index]);
  358. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  359. }
  360. }
  361. } else {
  362. $namespace = (array) $namespace;
  363. foreach ($namespace as $ns) {
  364. $autoloaders = $this->getNamespaceAutoloaders($ns);
  365. if (false !== ($index = array_search($callback, $autoloaders, true))) {
  366. unset($autoloaders[$index]);
  367. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  368. }
  369. }
  370. }
  371. return $this;
  372. }
  373. /**
  374. * Constructor
  375. *
  376. * Registers instance with spl_autoload stack
  377. *
  378. * @return void
  379. */
  380. protected function __construct()
  381. {
  382. spl_autoload_register(array(__CLASS__, 'autoload'));
  383. $this->_internalAutoloader = array($this, '_autoload');
  384. }
  385. /**
  386. * Internal autoloader implementation
  387. *
  388. * @param string $class
  389. * @return bool
  390. */
  391. protected function _autoload($class)
  392. {
  393. $callback = $this->getDefaultAutoloader();
  394. try {
  395. if ($this->suppressNotFoundWarnings()) {
  396. @call_user_func($callback, $class);
  397. } else {
  398. call_user_func($callback, $class);
  399. }
  400. return $class;
  401. } catch (Zend_Exception $e) {
  402. return false;
  403. }
  404. }
  405. /**
  406. * Set autoloaders for a specific namespace
  407. *
  408. * @param array $autoloaders
  409. * @param string $namespace
  410. * @return Zend_Loader_Autoloader
  411. */
  412. protected function _setNamespaceAutoloaders(array $autoloaders, $namespace = '')
  413. {
  414. $namespace = (string) $namespace;
  415. $this->_namespaceAutoloaders[$namespace] = $autoloaders;
  416. return $this;
  417. }
  418. }