2
0

StandardAutoloader.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. * Constructor
  54. *
  55. * @param null|array|Traversable $options
  56. * @return void
  57. */
  58. public function __construct($options = null)
  59. {
  60. $this->registerPrefix('Zend', dirname(dirname(__FILE__)));
  61. $zfDir = dirname(dirname(dirname(__FILE__))) . '/Zend';
  62. if (file_exists($zfDir)) {
  63. $this->registerPrefix('Zend', $zfDir);
  64. }
  65. if (null !== $options) {
  66. $this->setOptions($options);
  67. }
  68. }
  69. /**
  70. * Configure autoloader
  71. *
  72. * Allows specifying both "namespace" and "prefix" pairs, using the
  73. * following structure:
  74. * <code>
  75. * array(
  76. * 'namespaces' => array(
  77. * 'Zend' => '/path/to/Zend/library',
  78. * 'Doctrine' => '/path/to/Doctrine/library',
  79. * ),
  80. * 'prefixes' => array(
  81. * 'Phly_' => '/path/to/Phly/library',
  82. * ),
  83. * 'fallback_autoloader' => true,
  84. * )
  85. * </code>
  86. *
  87. * @param array|Traversable $options
  88. * @return Zend_Loader_StandardAutoloader
  89. */
  90. public function setOptions($options)
  91. {
  92. if (!is_array($options) && !($options instanceof Traversable)) {
  93. require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
  94. throw new Zend_Loader_Exception_InvalidArgumentException('Options must be either an array or Traversable');
  95. }
  96. foreach ($options as $type => $pairs) {
  97. switch ($type) {
  98. case self::LOAD_NS:
  99. if (is_array($pairs) || $pairs instanceof Traversable) {
  100. $this->registerNamespaces($pairs);
  101. }
  102. break;
  103. case self::LOAD_PREFIX:
  104. if (is_array($pairs) || $pairs instanceof Traversable) {
  105. $this->registerPrefixes($pairs);
  106. }
  107. break;
  108. case self::ACT_AS_FALLBACK:
  109. $this->setFallbackAutoloader($pairs);
  110. break;
  111. default:
  112. // ignore
  113. }
  114. }
  115. return $this;
  116. }
  117. /**
  118. * Set flag indicating fallback autoloader status
  119. *
  120. * @param bool $flag
  121. * @return Zend_Loader_StandardAutoloader
  122. */
  123. public function setFallbackAutoloader($flag)
  124. {
  125. $this->fallbackAutoloaderFlag = (bool) $flag;
  126. return $this;
  127. }
  128. /**
  129. * Is this autoloader acting as a fallback autoloader?
  130. *
  131. * @return bool
  132. */
  133. public function isFallbackAutoloader()
  134. {
  135. return $this->fallbackAutoloaderFlag;
  136. }
  137. /**
  138. * Register a namespace/directory pair
  139. *
  140. * @param string $namespace
  141. * @param string $directory
  142. * @return Zend_Loader_StandardAutoloader
  143. */
  144. public function registerNamespace($namespace, $directory)
  145. {
  146. $namespace = rtrim($namespace, self::NS_SEPARATOR). self::NS_SEPARATOR;
  147. $this->namespaces[$namespace] = $this->normalizeDirectory($directory);
  148. return $this;
  149. }
  150. /**
  151. * Register many namespace/directory pairs at once
  152. *
  153. * @param array $namespaces
  154. * @return Zend_Loader_StandardAutoloader
  155. */
  156. public function registerNamespaces($namespaces)
  157. {
  158. if (!is_array($namespaces) && !$namespaces instanceof Traversable) {
  159. require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
  160. throw new Zend_Loader_Exception_InvalidArgumentException('Namespace pairs must be either an array or Traversable');
  161. }
  162. foreach ($namespaces as $namespace => $directory) {
  163. $this->registerNamespace($namespace, $directory);
  164. }
  165. return $this;
  166. }
  167. /**
  168. * Register a prefix/directory pair
  169. *
  170. * @param string $prefix
  171. * @param string $directory
  172. * @return Zend_Loader_StandardAutoloader
  173. */
  174. public function registerPrefix($prefix, $directory)
  175. {
  176. $prefix = rtrim($prefix, self::PREFIX_SEPARATOR). self::PREFIX_SEPARATOR;
  177. $this->prefixes[$prefix] = $this->normalizeDirectory($directory);
  178. return $this;
  179. }
  180. /**
  181. * Register many namespace/directory pairs at once
  182. *
  183. * @param array $prefixes
  184. * @return Zend_Loader_StandardAutoloader
  185. */
  186. public function registerPrefixes($prefixes)
  187. {
  188. if (!is_array($prefixes) && !$prefixes instanceof Traversable) {
  189. require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
  190. throw new Zend_Loader_Exception_InvalidArgumentException('Prefix pairs must be either an array or Traversable');
  191. }
  192. foreach ($prefixes as $prefix => $directory) {
  193. $this->registerPrefix($prefix, $directory);
  194. }
  195. return $this;
  196. }
  197. /**
  198. * Defined by Autoloadable; autoload a class
  199. *
  200. * @param string $class
  201. * @return false|string
  202. */
  203. public function autoload($class)
  204. {
  205. $isFallback = $this->isFallbackAutoloader();
  206. if (false !== strpos($class, self::NS_SEPARATOR)) {
  207. if ($this->loadClass($class, self::LOAD_NS)) {
  208. return $class;
  209. } elseif ($isFallback) {
  210. return $this->loadClass($class, self::ACT_AS_FALLBACK);
  211. }
  212. return false;
  213. }
  214. if (false !== strpos($class, self::PREFIX_SEPARATOR)) {
  215. if ($this->loadClass($class, self::LOAD_PREFIX)) {
  216. return $class;
  217. } elseif ($isFallback) {
  218. return $this->loadClass($class, self::ACT_AS_FALLBACK);
  219. }
  220. return false;
  221. }
  222. if ($isFallback) {
  223. return $this->loadClass($class, self::ACT_AS_FALLBACK);
  224. }
  225. return false;
  226. }
  227. /**
  228. * Register the autoloader with spl_autoload
  229. *
  230. * @return void
  231. */
  232. public function register()
  233. {
  234. spl_autoload_register(array($this, 'autoload'));
  235. }
  236. /**
  237. * Transform the class name to a filename
  238. *
  239. * @param string $class
  240. * @param string $directory
  241. * @return string
  242. */
  243. protected function transformClassNameToFilename($class, $directory)
  244. {
  245. // $class may contain a namespace portion, in which case we need
  246. // to preserve any underscores in that portion.
  247. $matches = array();
  248. preg_match('/(?P<namespace>.+\\\)?(?P<class>[^\\\]+$)/', $class, $matches);
  249. $class = (isset($matches['class'])) ? $matches['class'] : '';
  250. $namespace = (isset($matches['namespace'])) ? $matches['namespace'] : '';
  251. return $directory
  252. . str_replace(self::NS_SEPARATOR, '/', $namespace)
  253. . str_replace(self::PREFIX_SEPARATOR, '/', $class)
  254. . '.php';
  255. }
  256. /**
  257. * Load a class, based on its type (namespaced or prefixed)
  258. *
  259. * @param string $class
  260. * @param string $type
  261. * @return void
  262. */
  263. protected function loadClass($class, $type)
  264. {
  265. if (!in_array($type, array(self::LOAD_NS, self::LOAD_PREFIX, self::ACT_AS_FALLBACK))) {
  266. require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
  267. throw new Zend_Loader_Exception_InvalidArgumentException();
  268. }
  269. // Fallback autoloading
  270. if ($type === self::ACT_AS_FALLBACK) {
  271. // create filename
  272. $filename = $this->transformClassNameToFilename($class, '');
  273. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  274. $resolvedName = stream_resolve_include_path($filename);
  275. if ($resolvedName !== false) {
  276. return include $resolvedName;
  277. }
  278. return false;
  279. }
  280. return include $filename;
  281. }
  282. // Namespace and/or prefix autoloading
  283. foreach ($this->$type as $leader => $path) {
  284. if (0 === strpos($class, $leader)) {
  285. // Trim off leader (namespace or prefix)
  286. $trimmedClass = substr($class, strlen($leader));
  287. // create filename
  288. $filename = $this->transformClassNameToFilename($trimmedClass, $path);
  289. if (file_exists($filename)) {
  290. return include $filename;
  291. }
  292. return false;
  293. }
  294. }
  295. return false;
  296. }
  297. /**
  298. * Normalize the directory to include a trailing directory separator
  299. *
  300. * @param string $directory
  301. * @return string
  302. */
  303. protected function normalizeDirectory($directory)
  304. {
  305. $last = $directory[strlen($directory) - 1];
  306. if (in_array($last, array('/', '\\'))) {
  307. $directory[strlen($directory) - 1] = DIRECTORY_SEPARATOR;
  308. return $directory;
  309. }
  310. $directory .= DIRECTORY_SEPARATOR;
  311. return $directory;
  312. }
  313. }