Autoloader.php 16 KB

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