Autoloader.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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-2011 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-2011 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. if ((false === $namespace) || (strlen($ns) > strlen($namespace))) {
  305. $namespace = $ns;
  306. $autoloaders = $this->getNamespaceAutoloaders($ns);
  307. }
  308. }
  309. }
  310. // Add internal namespaced autoloader
  311. foreach ($this->getRegisteredNamespaces() as $ns) {
  312. if (0 === strpos($class, $ns)) {
  313. $namespace = $ns;
  314. $autoloaders[] = $this->_internalAutoloader;
  315. break;
  316. }
  317. }
  318. // Add non-namespaced autoloaders
  319. $autoloaders = $autoloaders + $this->getNamespaceAutoloaders('');
  320. // Add fallback autoloader
  321. if (!$namespace && $this->isFallbackAutoloader()) {
  322. $autoloaders[] = $this->_internalAutoloader;
  323. }
  324. return $autoloaders;
  325. }
  326. /**
  327. * Add an autoloader to the beginning of the stack
  328. *
  329. * @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
  330. * @param string|array $namespace Specific namespace(s) under which to register callback
  331. * @return Zend_Loader_Autoloader
  332. */
  333. public function unshiftAutoloader($callback, $namespace = '')
  334. {
  335. $autoloaders = $this->getAutoloaders();
  336. array_unshift($autoloaders, $callback);
  337. $this->setAutoloaders($autoloaders);
  338. $namespace = (array) $namespace;
  339. foreach ($namespace as $ns) {
  340. $autoloaders = $this->getNamespaceAutoloaders($ns);
  341. array_unshift($autoloaders, $callback);
  342. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  343. }
  344. return $this;
  345. }
  346. /**
  347. * Append an autoloader to the autoloader stack
  348. *
  349. * @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
  350. * @param string|array $namespace Specific namespace(s) under which to register callback
  351. * @return Zend_Loader_Autoloader
  352. */
  353. public function pushAutoloader($callback, $namespace = '')
  354. {
  355. $autoloaders = $this->getAutoloaders();
  356. array_push($autoloaders, $callback);
  357. $this->setAutoloaders($autoloaders);
  358. $namespace = (array) $namespace;
  359. foreach ($namespace as $ns) {
  360. $autoloaders = $this->getNamespaceAutoloaders($ns);
  361. array_push($autoloaders, $callback);
  362. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  363. }
  364. return $this;
  365. }
  366. /**
  367. * Remove an autoloader from the autoloader stack
  368. *
  369. * @param object|array|string $callback PHP callback or Zend_Loader_Autoloader_Interface implementation
  370. * @param null|string|array $namespace Specific namespace(s) from which to remove autoloader
  371. * @return Zend_Loader_Autoloader
  372. */
  373. public function removeAutoloader($callback, $namespace = null)
  374. {
  375. if (null === $namespace) {
  376. $autoloaders = $this->getAutoloaders();
  377. if (false !== ($index = array_search($callback, $autoloaders, true))) {
  378. unset($autoloaders[$index]);
  379. $this->setAutoloaders($autoloaders);
  380. }
  381. foreach ($this->_namespaceAutoloaders as $ns => $autoloaders) {
  382. if (false !== ($index = array_search($callback, $autoloaders, true))) {
  383. unset($autoloaders[$index]);
  384. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  385. }
  386. }
  387. } else {
  388. $namespace = (array) $namespace;
  389. foreach ($namespace as $ns) {
  390. $autoloaders = $this->getNamespaceAutoloaders($ns);
  391. if (false !== ($index = array_search($callback, $autoloaders, true))) {
  392. unset($autoloaders[$index]);
  393. $this->_setNamespaceAutoloaders($autoloaders, $ns);
  394. }
  395. }
  396. }
  397. return $this;
  398. }
  399. /**
  400. * Constructor
  401. *
  402. * Registers instance with spl_autoload stack
  403. *
  404. * @return void
  405. */
  406. protected function __construct()
  407. {
  408. spl_autoload_register(array(__CLASS__, 'autoload'));
  409. $this->_internalAutoloader = array($this, '_autoload');
  410. }
  411. /**
  412. * Internal autoloader implementation
  413. *
  414. * @param string $class
  415. * @return bool
  416. */
  417. protected function _autoload($class)
  418. {
  419. $callback = $this->getDefaultAutoloader();
  420. try {
  421. if ($this->suppressNotFoundWarnings()) {
  422. @call_user_func($callback, $class);
  423. } else {
  424. call_user_func($callback, $class);
  425. }
  426. return $class;
  427. } catch (Zend_Exception $e) {
  428. return false;
  429. }
  430. }
  431. /**
  432. * Set autoloaders for a specific namespace
  433. *
  434. * @param array $autoloaders
  435. * @param string $namespace
  436. * @return Zend_Loader_Autoloader
  437. */
  438. protected function _setNamespaceAutoloaders(array $autoloaders, $namespace = '')
  439. {
  440. $namespace = (string) $namespace;
  441. $this->_namespaceAutoloaders[$namespace] = $autoloaders;
  442. return $this;
  443. }
  444. /**
  445. * Retrieve the filesystem path for the requested ZF version
  446. *
  447. * @param string $path
  448. * @param string $version
  449. * @return void
  450. */
  451. protected function _getVersionPath($path, $version)
  452. {
  453. $type = $this->_getVersionType($version);
  454. if ($type == 'latest') {
  455. $version = 'latest';
  456. }
  457. $availableVersions = $this->_getAvailableVersions($path, $version);
  458. if (empty($availableVersions)) {
  459. throw new Zend_Loader_Exception('No valid ZF installations discovered');
  460. }
  461. $matchedVersion = array_pop($availableVersions);
  462. return $matchedVersion;
  463. }
  464. /**
  465. * Retrieve the ZF version type
  466. *
  467. * @param string $version
  468. * @return string "latest", "major", "minor", or "specific"
  469. * @throws Zend_Loader_Exception if version string contains too many dots
  470. */
  471. protected function _getVersionType($version)
  472. {
  473. if (strtolower($version) == 'latest') {
  474. return 'latest';
  475. }
  476. $parts = explode('.', $version);
  477. $count = count($parts);
  478. if (1 == $count) {
  479. return 'major';
  480. }
  481. if (2 == $count) {
  482. return 'minor';
  483. }
  484. if (3 < $count) {
  485. throw new Zend_Loader_Exception('Invalid version string provided');
  486. }
  487. return 'specific';
  488. }
  489. /**
  490. * Get available versions for the version type requested
  491. *
  492. * @param string $path
  493. * @param string $version
  494. * @return array
  495. */
  496. protected function _getAvailableVersions($path, $version)
  497. {
  498. if (!is_dir($path)) {
  499. throw new Zend_Loader_Exception('Invalid ZF path provided');
  500. }
  501. $path = rtrim($path, '/');
  502. $path = rtrim($path, '\\');
  503. $versionLen = strlen($version);
  504. $versions = array();
  505. $dirs = glob("$path/*", GLOB_ONLYDIR);
  506. foreach ((array) $dirs as $dir) {
  507. $dirName = substr($dir, strlen($path) + 1);
  508. if (!preg_match('/^(?:ZendFramework-)?(\d+\.\d+\.\d+((a|b|pl|pr|p|rc)\d+)?)(?:-minimal)?$/i', $dirName, $matches)) {
  509. continue;
  510. }
  511. $matchedVersion = $matches[1];
  512. if (('latest' == $version)
  513. || ((strlen($matchedVersion) >= $versionLen)
  514. && (0 === strpos($matchedVersion, $version)))
  515. ) {
  516. $versions[$matchedVersion] = $dir . '/library';
  517. }
  518. }
  519. uksort($versions, 'version_compare');
  520. return $versions;
  521. }
  522. }