2
0

Autoloader.php 16 KB

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