StandardAutoloader.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. // Grab SplAutoloader interface
  21. require_once dirname(__FILE__) . '/SplAutoloader.php';
  22. /**
  23. * PSR-0 compliant autoloader
  24. *
  25. * Allows autoloading both namespaced and vendor-prefixed classes. Class
  26. * lookups are performed on the filesystem. If a class file for the referenced
  27. * class is not found, a PHP warning will be raised by include().
  28. *
  29. * @package Zend_Loader
  30. * @copyright Copyright (c) 2005-2012 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_StandardAutoloader implements Zend_Loader_SplAutoloader
  34. {
  35. const NS_SEPARATOR = '\\';
  36. const PREFIX_SEPARATOR = '_';
  37. const LOAD_NS = 'namespaces';
  38. const LOAD_PREFIX = 'prefixes';
  39. const ACT_AS_FALLBACK = 'fallback_autoloader';
  40. /**
  41. * @var array Namespace/directory pairs to search; ZF library added by default
  42. */
  43. protected $namespaces = array();
  44. /**
  45. * @var array Prefix/directory pairs to search
  46. */
  47. protected $prefixes = array();
  48. /**
  49. * @var bool Whether or not the autoloader should also act as a fallback autoloader
  50. */
  51. protected $fallbackAutoloaderFlag = false;
  52. /**
  53. * @var bool
  54. */
  55. protected $error;
  56. /**
  57. * Constructor
  58. *
  59. * @param null|array|Traversable $options
  60. * @return void
  61. */
  62. public function __construct($options = null)
  63. {
  64. $this->registerPrefix('Zend', dirname(dirname(__FILE__)));
  65. $zfDir = dirname(dirname(dirname(__FILE__))) . '/Zend';
  66. if (file_exists($zfDir)) {
  67. $this->registerPrefix('Zend', $zfDir);
  68. }
  69. if (null !== $options) {
  70. $this->setOptions($options);
  71. }
  72. }
  73. /**
  74. * Configure autoloader
  75. *
  76. * Allows specifying both "namespace" and "prefix" pairs, using the
  77. * following structure:
  78. * <code>
  79. * array(
  80. * 'namespaces' => array(
  81. * 'Zend' => '/path/to/Zend/library',
  82. * 'Doctrine' => '/path/to/Doctrine/library',
  83. * ),
  84. * 'prefixes' => array(
  85. * 'Phly_' => '/path/to/Phly/library',
  86. * ),
  87. * 'fallback_autoloader' => true,
  88. * )
  89. * </code>
  90. *
  91. * @param array|Traversable $options
  92. * @return Zend_Loader_StandardAutoloader
  93. */
  94. public function setOptions($options)
  95. {
  96. if (!is_array($options) && !($options instanceof Traversable)) {
  97. require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
  98. throw new Zend_Loader_Exception_InvalidArgumentException('Options must be either an array or Traversable');
  99. }
  100. foreach ($options as $type => $pairs) {
  101. switch ($type) {
  102. case self::LOAD_NS:
  103. if (is_array($pairs) || $pairs instanceof Traversable) {
  104. $this->registerNamespaces($pairs);
  105. }
  106. break;
  107. case self::LOAD_PREFIX:
  108. if (is_array($pairs) || $pairs instanceof Traversable) {
  109. $this->registerPrefixes($pairs);
  110. }
  111. break;
  112. case self::ACT_AS_FALLBACK:
  113. $this->setFallbackAutoloader($pairs);
  114. break;
  115. default:
  116. // ignore
  117. }
  118. }
  119. return $this;
  120. }
  121. /**
  122. * Set flag indicating fallback autoloader status
  123. *
  124. * @param bool $flag
  125. * @return Zend_Loader_StandardAutoloader
  126. */
  127. public function setFallbackAutoloader($flag)
  128. {
  129. $this->fallbackAutoloaderFlag = (bool) $flag;
  130. return $this;
  131. }
  132. /**
  133. * Is this autoloader acting as a fallback autoloader?
  134. *
  135. * @return bool
  136. */
  137. public function isFallbackAutoloader()
  138. {
  139. return $this->fallbackAutoloaderFlag;
  140. }
  141. /**
  142. * Register a namespace/directory pair
  143. *
  144. * @param string $namespace
  145. * @param string $directory
  146. * @return Zend_Loader_StandardAutoloader
  147. */
  148. public function registerNamespace($namespace, $directory)
  149. {
  150. $namespace = rtrim($namespace, self::NS_SEPARATOR). self::NS_SEPARATOR;
  151. $this->namespaces[$namespace] = $this->normalizeDirectory($directory);
  152. return $this;
  153. }
  154. /**
  155. * Register many namespace/directory pairs at once
  156. *
  157. * @param array $namespaces
  158. * @return Zend_Loader_StandardAutoloader
  159. */
  160. public function registerNamespaces($namespaces)
  161. {
  162. if (!is_array($namespaces) && !$namespaces instanceof Traversable) {
  163. require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
  164. throw new Zend_Loader_Exception_InvalidArgumentException('Namespace pairs must be either an array or Traversable');
  165. }
  166. foreach ($namespaces as $namespace => $directory) {
  167. $this->registerNamespace($namespace, $directory);
  168. }
  169. return $this;
  170. }
  171. /**
  172. * Register a prefix/directory pair
  173. *
  174. * @param string $prefix
  175. * @param string $directory
  176. * @return Zend_Loader_StandardAutoloader
  177. */
  178. public function registerPrefix($prefix, $directory)
  179. {
  180. $prefix = rtrim($prefix, self::PREFIX_SEPARATOR). self::PREFIX_SEPARATOR;
  181. $this->prefixes[$prefix] = $this->normalizeDirectory($directory);
  182. return $this;
  183. }
  184. /**
  185. * Register many namespace/directory pairs at once
  186. *
  187. * @param array $prefixes
  188. * @return Zend_Loader_StandardAutoloader
  189. */
  190. public function registerPrefixes($prefixes)
  191. {
  192. if (!is_array($prefixes) && !$prefixes instanceof Traversable) {
  193. require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
  194. throw new Zend_Loader_Exception_InvalidArgumentException('Prefix pairs must be either an array or Traversable');
  195. }
  196. foreach ($prefixes as $prefix => $directory) {
  197. $this->registerPrefix($prefix, $directory);
  198. }
  199. return $this;
  200. }
  201. /**
  202. * Defined by Autoloadable; autoload a class
  203. *
  204. * @param string $class
  205. * @return false|string
  206. */
  207. public function autoload($class)
  208. {
  209. $isFallback = $this->isFallbackAutoloader();
  210. if (false !== strpos($class, self::NS_SEPARATOR)) {
  211. if ($this->loadClass($class, self::LOAD_NS)) {
  212. return $class;
  213. } elseif ($isFallback) {
  214. return $this->loadClass($class, self::ACT_AS_FALLBACK);
  215. }
  216. return false;
  217. }
  218. if (false !== strpos($class, self::PREFIX_SEPARATOR)) {
  219. if ($this->loadClass($class, self::LOAD_PREFIX)) {
  220. return $class;
  221. } elseif ($isFallback) {
  222. return $this->loadClass($class, self::ACT_AS_FALLBACK);
  223. }
  224. return false;
  225. }
  226. if ($isFallback) {
  227. return $this->loadClass($class, self::ACT_AS_FALLBACK);
  228. }
  229. return false;
  230. }
  231. /**
  232. * Register the autoloader with spl_autoload
  233. *
  234. * @return void
  235. */
  236. public function register()
  237. {
  238. spl_autoload_register(array($this, 'autoload'));
  239. }
  240. /**
  241. * Error handler
  242. *
  243. * Used by {@link loadClass} during fallback autoloading in PHP versions
  244. * prior to 5.3.0.
  245. *
  246. * @param mixed $errno
  247. * @param mixed $errstr
  248. * @return void
  249. */
  250. public function handleError($errno, $errstr)
  251. {
  252. $this->error = true;
  253. }
  254. /**
  255. * Transform the class name to a filename
  256. *
  257. * @param string $class
  258. * @param string $directory
  259. * @return string
  260. */
  261. protected function transformClassNameToFilename($class, $directory)
  262. {
  263. // $class may contain a namespace portion, in which case we need
  264. // to preserve any underscores in that portion.
  265. $matches = array();
  266. preg_match('/(?P<namespace>.+\\\)?(?P<class>[^\\\]+$)/', $class, $matches);
  267. $class = (isset($matches['class'])) ? $matches['class'] : '';
  268. $namespace = (isset($matches['namespace'])) ? $matches['namespace'] : '';
  269. return $directory
  270. . str_replace(self::NS_SEPARATOR, '/', $namespace)
  271. . str_replace(self::PREFIX_SEPARATOR, '/', $class)
  272. . '.php';
  273. }
  274. /**
  275. * Load a class, based on its type (namespaced or prefixed)
  276. *
  277. * @param string $class
  278. * @param string $type
  279. * @return void
  280. */
  281. protected function loadClass($class, $type)
  282. {
  283. if (!in_array($type, array(self::LOAD_NS, self::LOAD_PREFIX, self::ACT_AS_FALLBACK))) {
  284. require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
  285. throw new Zend_Loader_Exception_InvalidArgumentException();
  286. }
  287. // Fallback autoloading
  288. if ($type === self::ACT_AS_FALLBACK) {
  289. // create filename
  290. $filename = $this->transformClassNameToFilename($class, '');
  291. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  292. $resolvedName = stream_resolve_include_path($filename);
  293. if ($resolvedName !== false) {
  294. return include $resolvedName;
  295. }
  296. return false;
  297. }
  298. $this->error = false;
  299. set_error_handler(array($this, 'handleError'), E_WARNING);
  300. include $filename;
  301. restore_error_handler();
  302. if ($this->error) {
  303. return false;
  304. }
  305. return class_exists($class, false);
  306. }
  307. // Namespace and/or prefix autoloading
  308. foreach ($this->$type as $leader => $path) {
  309. if (0 === strpos($class, $leader)) {
  310. // Trim off leader (namespace or prefix)
  311. $trimmedClass = substr($class, strlen($leader));
  312. // create filename
  313. $filename = $this->transformClassNameToFilename($trimmedClass, $path);
  314. if (file_exists($filename)) {
  315. return include $filename;
  316. }
  317. return false;
  318. }
  319. }
  320. return false;
  321. }
  322. /**
  323. * Normalize the directory to include a trailing directory separator
  324. *
  325. * @param string $directory
  326. * @return string
  327. */
  328. protected function normalizeDirectory($directory)
  329. {
  330. $last = $directory[strlen($directory) - 1];
  331. if (in_array($last, array('/', '\\'))) {
  332. $directory[strlen($directory) - 1] = DIRECTORY_SEPARATOR;
  333. return $directory;
  334. }
  335. $directory .= DIRECTORY_SEPARATOR;
  336. return $directory;
  337. }
  338. }