Autoloader.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Autoloader.php 2504 2011-12-28 07:35:29Z liu21st $
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  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. * @var null|string
  72. */
  73. protected $_zfPath;
  74. /**
  75. * Retrieve singleton instance
  76. *
  77. * @return Zend_Loader_Autoloader
  78. */
  79. public static function getInstance()
  80. {
  81. if (null === self::$_instance) {
  82. self::$_instance = new self();
  83. }
  84. return self::$_instance;
  85. }
  86. /**
  87. * Reset the singleton instance
  88. *
  89. * @return void
  90. */
  91. public static function resetInstance()
  92. {
  93. self::$_instance = null;
  94. }
  95. /**
  96. * Autoload a class
  97. *
  98. * @param string $class
  99. * @return bool
  100. */
  101. public static function autoload($class)
  102. {
  103. $self = self::getInstance();
  104. foreach ($self->getClassAutoloaders($class) as $autoloader) {
  105. if ($autoloader instanceof Zend_Loader_Autoloader_Interface) {
  106. if ($autoloader->autoload($class)) {
  107. return true;
  108. }
  109. } elseif (is_string($autoloader)) {
  110. if ($autoloader($class)) {
  111. return true;
  112. }
  113. } elseif (is_array($autoloader)) {
  114. $object = array_shift($autoloader);
  115. $method = array_shift($autoloader);
  116. if (call_user_func(array($object, $method), $class)) {
  117. return true;
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. /**
  124. * Set the default autoloader implementation
  125. *
  126. * @param string|array $callback PHP callback
  127. * @return void
  128. */
  129. public function setDefaultAutoloader($callback)
  130. {
  131. if (!is_callable($callback)) {
  132. throw new Zend_Loader_Exception('Invalid callback specified for default autoloader');
  133. }
  134. $this->_defaultAutoloader = $callback;
  135. return $this;
  136. }
  137. /**
  138. * Retrieve the default autoloader callback
  139. *
  140. * @return string|array PHP Callback
  141. */
  142. public function getDefaultAutoloader()
  143. {
  144. return $this->_defaultAutoloader;
  145. }
  146. /**
  147. * Set several autoloader callbacks at once
  148. *
  149. * @param array $autoloaders Array of PHP callbacks (or Zend_Loader_Autoloader_Interface implementations) to act as autoloaders
  150. * @return Zend_Loader_Autoloader
  151. */
  152. public function setAutoloaders(array $autoloaders)
  153. {
  154. $this->_autoloaders = $autoloaders;
  155. return $this;
  156. }
  157. /**
  158. * Get attached autoloader implementations
  159. *
  160. * @return array
  161. */
  162. public function getAutoloaders()
  163. {
  164. return $this->_autoloaders;
  165. }
  166. /**
  167. * Return all autoloaders for a given namespace
  168. *
  169. * @param string $namespace
  170. * @return array
  171. */
  172. public function getNamespaceAutoloaders($namespace)
  173. {
  174. $namespace = (string) $namespace;
  175. if (!array_key_exists($namespace, $this->_namespaceAutoloaders)) {
  176. return array();
  177. }
  178. return $this->_namespaceAutoloaders[$namespace];
  179. }
  180. /**
  181. * Register a namespace to autoload
  182. *
  183. * @param string|array $namespace
  184. * @return Zend_Loader_Autoloader
  185. */
  186. public function registerNamespace($namespace)
  187. {
  188. if (is_string($namespace)) {
  189. $namespace = (array) $namespace;
  190. } elseif (!is_array($namespace)) {
  191. throw new Zend_Loader_Exception('Invalid namespace provided');
  192. }
  193. foreach ($namespace as $ns) {
  194. if (!isset($this->_namespaces[$ns])) {
  195. $this->_namespaces[$ns] = true;
  196. }
  197. }
  198. return $this;
  199. }
  200. /**
  201. * Unload a registered autoload namespace
  202. *
  203. * @param string|array $namespace
  204. * @return Zend_Loader_Autoloader
  205. */
  206. public function unregisterNamespace($namespace)
  207. {
  208. if (is_string($namespace)) {
  209. $namespace = (array) $namespace;
  210. } elseif (!is_array($namespace)) {
  211. throw new Zend_Loader_Exception('Invalid namespace provided');
  212. }
  213. foreach ($namespace as $ns) {
  214. if (isset($this->_namespaces[$ns])) {
  215. unset($this->_namespaces[$ns]);
  216. }
  217. }
  218. return $this;
  219. }
  220. /**
  221. * Get a list of registered autoload namespaces
  222. *
  223. * @return array
  224. */
  225. public function getRegisteredNamespaces()
  226. {
  227. return array_keys($this->_namespaces);
  228. }
  229. public function setZfPath($spec, $version = 'latest')
  230. {
  231. $path = $spec;
  232. if (is_array($spec)) {
  233. if (!isset($spec['path'])) {
  234. throw new Zend_Loader_Exception('No path specified for ZF');
  235. }
  236. $path = $spec['path'];
  237. if (isset($spec['version'])) {
  238. $version = $spec['version'];
  239. }
  240. }
  241. $this->_zfPath = $this->_getVersionPath($path, $version);
  242. set_include_path(implode(PATH_SEPARATOR, array(
  243. $this->_zfPath,
  244. get_include_path(),
  245. )));
  246. return $this;
  247. }
  248. public function getZfPath()
  249. {
  250. return $this->_zfPath;
  251. }
  252. /**
  253. * Get or set the value of the "suppress not found warnings" flag
  254. *
  255. * @param null|bool $flag
  256. * @return bool|Zend_Loader_Autoloader Returns boolean if no argument is passed, object instance otherwise
  257. */
  258. public function suppressNotFoundWarnings($flag = null)
  259. {
  260. if (null === $flag) {
  261. return $this->_suppressNotFoundWarnings;
  262. }
  263. $this->_suppressNotFoundWarnings = (bool) $flag;
  264. return $this;
  265. }
  266. /**
  267. * Indicate whether or not this autoloader should be a fallback autoloader
  268. *
  269. * @param bool $flag
  270. * @return Zend_Loader_Autoloader
  271. */
  272. public function setFallbackAutoloader($flag)
  273. {
  274. $this->_fallbackAutoloader = (bool) $flag;
  275. return $this;
  276. }
  277. /**
  278. * Is this instance acting as a fallback autoloader?
  279. *
  280. * @return bool
  281. */
  282. public function isFallbackAutoloader()
  283. {
  284. return $this->_fallbackAutoloader;
  285. }
  286. /**
  287. * Get autoloaders to use when matching class
  288. *
  289. * Determines if the class matches a registered namespace, and, if so,
  290. * returns only the autoloaders for that namespace. Otherwise, it returns
  291. * all non-namespaced autoloaders.
  292. *
  293. * @param string $class
  294. * @return array Array of autoloaders to use
  295. */
  296. public function getClassAutoloaders($class)
  297. {
  298. $namespace = false;
  299. $autoloaders = array();
  300. // Add concrete namespaced autoloaders
  301. foreach (array_keys($this->_namespaceAutoloaders) as $ns) {
  302. if ('' == $ns) {
  303. continue;
  304. }
  305. if (0 === strpos($class, $ns)) {
  306. $namespace = $ns;
  307. $autoloaders = $autoloaders + $this->getNamespaceAutoloaders($ns);
  308. break;
  309. }
  310. }
  311. // Add internal namespaced autoloader
  312. foreach ($this->getRegisteredNamespaces() as $ns) {
  313. if (0 === strpos($class, $ns)) {
  314. $namespace = $ns;
  315. $autoloaders[] = $this->_internalAutoloader;
  316. break;
  317. }
  318. }
  319. // Add non-namespaced autoloaders
  320. $autoloaders = $autoloaders + $this->getNamespaceAutoloaders('');
  321. // Add fallback autoloader
  322. if (!$namespace && $this->isFallbackAutoloader()) {
  323. $autoloaders[] = $this->_internalAutoloader;
  324. }
  325. return $autoloaders;
  326. }
  327. /**
  328. * Add an autoloader to the beginning of the stack
  329. *
  330. * @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
  331. * @param string|array $namespace Specific namespace(s) under which to register callback
  332. * @return Zend_Loader_Autoloader
  333. */
  334. public function unshiftAutoloader($callback, $namespace = '')
  335. {
  336. $autoloaders = $this->getAutoloaders();
  337. array_unshift($autoloaders, $callback);
  338. $this->setAutoloaders($autoloaders);
  339. $namespace = (array) $namespace;
  340. foreach ($namespace as $ns) {
  341. $autoloaders = $this->getNamespaceAutoloaders($ns);
  342. array_unshift($autoloaders, $callback);
  343. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  344. }
  345. return $this;
  346. }
  347. /**
  348. * Append an autoloader to the autoloader stack
  349. *
  350. * @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
  351. * @param string|array $namespace Specific namespace(s) under which to register callback
  352. * @return Zend_Loader_Autoloader
  353. */
  354. public function pushAutoloader($callback, $namespace = '')
  355. {
  356. $autoloaders = $this->getAutoloaders();
  357. array_push($autoloaders, $callback);
  358. $this->setAutoloaders($autoloaders);
  359. $namespace = (array) $namespace;
  360. foreach ($namespace as $ns) {
  361. $autoloaders = $this->getNamespaceAutoloaders($ns);
  362. array_push($autoloaders, $callback);
  363. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  364. }
  365. return $this;
  366. }
  367. /**
  368. * Remove an autoloader from the autoloader stack
  369. *
  370. * @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
  371. * @param null|string|array $namespace Specific namespace(s) from which to remove autoloader
  372. * @return Zend_Loader_Autoloader
  373. */
  374. public function removeAutoloader($callback, $namespace = null)
  375. {
  376. if (null === $namespace) {
  377. $autoloaders = $this->getAutoloaders();
  378. if (false !== ($index = array_search($callback, $autoloaders, true))) {
  379. unset($autoloaders[$index]);
  380. $this->setAutoloaders($autoloaders);
  381. }
  382. foreach ($this->_namespaceAutoloaders as $ns => $autoloaders) {
  383. if (false !== ($index = array_search($callback, $autoloaders, true))) {
  384. unset($autoloaders[$index]);
  385. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  386. }
  387. }
  388. } else {
  389. $namespace = (array) $namespace;
  390. foreach ($namespace as $ns) {
  391. $autoloaders = $this->getNamespaceAutoloaders($ns);
  392. if (false !== ($index = array_search($callback, $autoloaders, true))) {
  393. unset($autoloaders[$index]);
  394. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  395. }
  396. }
  397. }
  398. return $this;
  399. }
  400. /**
  401. * Constructor
  402. *
  403. * Registers instance with spl_autoload stack
  404. *
  405. * @return void
  406. */
  407. protected function __construct()
  408. {
  409. spl_autoload_register(array(__CLASS__, 'autoload'));
  410. $this->_internalAutoloader = array($this, '_autoload');
  411. }
  412. /**
  413. * Internal autoloader implementation
  414. *
  415. * @param string $class
  416. * @return bool
  417. */
  418. protected function _autoload($class)
  419. {
  420. $callback = $this->getDefaultAutoloader();
  421. try {
  422. if ($this->suppressNotFoundWarnings()) {
  423. @call_user_func($callback, $class);
  424. } else {
  425. call_user_func($callback, $class);
  426. }
  427. return $class;
  428. } catch (Zend_Exception $e) {
  429. return false;
  430. }
  431. }
  432. /**
  433. * Set autoloaders for a specific namespace
  434. *
  435. * @param array $autoloaders
  436. * @param string $namespace
  437. * @return Zend_Loader_Autoloader
  438. */
  439. protected function _setNamespaceAutoloaders(array $autoloaders, $namespace = '')
  440. {
  441. $namespace = (string) $namespace;
  442. $this->_namespaceAutoloaders[$namespace] = $autoloaders;
  443. return $this;
  444. }
  445. /**
  446. * Retrieve the filesystem path for the requested ZF version
  447. *
  448. * @param string $path
  449. * @param string $version
  450. * @return void
  451. */
  452. protected function _getVersionPath($path, $version)
  453. {
  454. $type = $this->_getVersionType($version);
  455. if ($type == 'latest') {
  456. $version = 'latest';
  457. }
  458. $availableVersions = $this->_getAvailableVersions($path, $version);
  459. if (empty($availableVersions)) {
  460. throw new Zend_Loader_Exception('No valid ZF installations discovered');
  461. }
  462. $matchedVersion = array_pop($availableVersions);
  463. return $matchedVersion;
  464. }
  465. /**
  466. * Retrieve the ZF version type
  467. *
  468. * @param string $version
  469. * @return string "latest", "major", "minor", or "specific"
  470. * @throws Zend_Loader_Exception if version string contains too many dots
  471. */
  472. protected function _getVersionType($version)
  473. {
  474. if (strtolower($version) == 'latest') {
  475. return 'latest';
  476. }
  477. $parts = explode('.', $version);
  478. $count = count($parts);
  479. if (1 == $count) {
  480. return 'major';
  481. }
  482. if (2 == $count) {
  483. return 'minor';
  484. }
  485. if (3 < $count) {
  486. throw new Zend_Loader_Exception('Invalid version string provided');
  487. }
  488. return 'specific';
  489. }
  490. /**
  491. * Get available versions for the version type requested
  492. *
  493. * @param string $path
  494. * @param string $version
  495. * @return array
  496. */
  497. protected function _getAvailableVersions($path, $version)
  498. {
  499. if (!is_dir($path)) {
  500. throw new Zend_Loader_Exception('Invalid ZF path provided');
  501. }
  502. $path = rtrim($path, '/');
  503. $path = rtrim($path, '\\');
  504. $versionLen = strlen($version);
  505. $versions = array();
  506. $dirs = glob("$path/*", GLOB_ONLYDIR);
  507. foreach ($dirs as $dir) {
  508. $dirName = substr($dir, strlen($path) + 1);
  509. if (!preg_match('/^(?:ZendFramework-)?(\d+\.\d+\.\d+((a|b|pl|pr|p|rc)\d+)?)(?:-minimal)?$/i', $dirName, $matches)) {
  510. continue;
  511. }
  512. $matchedVersion = $matches[1];
  513. if (('latest' == $version)
  514. || ((strlen($matchedVersion) >= $versionLen)
  515. && (0 === strpos($matchedVersion, $version)))
  516. ) {
  517. $versions[$matchedVersion] = $dir . '/library';
  518. }
  519. }
  520. uksort($versions, 'version_compare');
  521. return $versions;
  522. }
  523. }