UniversalClassLoader.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ClassLoader;
  11. /**
  12. * UniversalClassLoader implements a "universal" autoloader for PHP 5.3.
  13. *
  14. * It is able to load classes that use either:
  15. *
  16. * * The technical interoperability standards for PHP 5.3 namespaces and
  17. * class names (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md);
  18. *
  19. * * The PEAR naming convention for classes (http://pear.php.net/).
  20. *
  21. * Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be
  22. * looked for in a list of locations to ease the vendoring of a sub-set of
  23. * classes for large projects.
  24. *
  25. * Example usage:
  26. *
  27. * $loader = new UniversalClassLoader();
  28. *
  29. * // register classes with namespaces
  30. * $loader->registerNamespaces(array(
  31. * 'Symfony\Component' => __DIR__.'/component',
  32. * 'Symfony' => __DIR__.'/framework',
  33. * 'Sensio' => array(__DIR__.'/src', __DIR__.'/vendor'),
  34. * ));
  35. *
  36. * // register a library using the PEAR naming convention
  37. * $loader->registerPrefixes(array(
  38. * 'Swift_' => __DIR__.'/Swift',
  39. * ));
  40. *
  41. *
  42. * // to enable searching the include path (eg. for PEAR packages)
  43. * $loader->useIncludePath(true);
  44. *
  45. * // activate the autoloader
  46. * $loader->register();
  47. *
  48. * In this example, if you try to use a class in the Symfony\Component
  49. * namespace or one of its children (Symfony\Component\Console for instance),
  50. * the autoloader will first look for the class under the component/
  51. * directory, and it will then fallback to the framework/ directory if not
  52. * found before giving up.
  53. *
  54. * @author Fabien Potencier <fabien@symfony.com>
  55. *
  56. * @api
  57. */
  58. class UniversalClassLoader
  59. {
  60. private $namespaces = array();
  61. private $prefixes = array();
  62. private $namespaceFallbacks = array();
  63. private $prefixFallbacks = array();
  64. private $useIncludePath = false;
  65. /**
  66. * Turns on searching the include for class files. Allows easy loading
  67. * of installed PEAR packages
  68. *
  69. * @param Boolean $useIncludePath
  70. */
  71. public function useIncludePath($useIncludePath)
  72. {
  73. $this->useIncludePath = $useIncludePath;
  74. }
  75. /**
  76. * Can be used to check if the autoloader uses the include path to check
  77. * for classes.
  78. *
  79. * @return Boolean
  80. */
  81. public function getUseIncludePath()
  82. {
  83. return $this->useIncludePath;
  84. }
  85. /**
  86. * Gets the configured namespaces.
  87. *
  88. * @return array A hash with namespaces as keys and directories as values
  89. */
  90. public function getNamespaces()
  91. {
  92. return $this->namespaces;
  93. }
  94. /**
  95. * Gets the configured class prefixes.
  96. *
  97. * @return array A hash with class prefixes as keys and directories as values
  98. */
  99. public function getPrefixes()
  100. {
  101. return $this->prefixes;
  102. }
  103. /**
  104. * Gets the directory(ies) to use as a fallback for namespaces.
  105. *
  106. * @return array An array of directories
  107. */
  108. public function getNamespaceFallbacks()
  109. {
  110. return $this->namespaceFallbacks;
  111. }
  112. /**
  113. * Gets the directory(ies) to use as a fallback for class prefixes.
  114. *
  115. * @return array An array of directories
  116. */
  117. public function getPrefixFallbacks()
  118. {
  119. return $this->prefixFallbacks;
  120. }
  121. /**
  122. * Registers the directory to use as a fallback for namespaces.
  123. *
  124. * @param array $dirs An array of directories
  125. *
  126. * @api
  127. */
  128. public function registerNamespaceFallbacks(array $dirs)
  129. {
  130. $this->namespaceFallbacks = $dirs;
  131. }
  132. /**
  133. * Registers a directory to use as a fallback for namespaces.
  134. *
  135. * @param string $dir A directory
  136. */
  137. public function registerNamespaceFallback($dir)
  138. {
  139. $this->namespaceFallbacks[] = $dir;
  140. }
  141. /**
  142. * Registers directories to use as a fallback for class prefixes.
  143. *
  144. * @param array $dirs An array of directories
  145. *
  146. * @api
  147. */
  148. public function registerPrefixFallbacks(array $dirs)
  149. {
  150. $this->prefixFallbacks = $dirs;
  151. }
  152. /**
  153. * Registers a directory to use as a fallback for class prefixes.
  154. *
  155. * @param string $dir A directory
  156. */
  157. public function registerPrefixFallback($dir)
  158. {
  159. $this->prefixFallbacks[] = $dir;
  160. }
  161. /**
  162. * Registers an array of namespaces
  163. *
  164. * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
  165. *
  166. * @api
  167. */
  168. public function registerNamespaces(array $namespaces)
  169. {
  170. foreach ($namespaces as $namespace => $locations) {
  171. $this->namespaces[$namespace] = (array) $locations;
  172. }
  173. }
  174. /**
  175. * Registers a namespace.
  176. *
  177. * @param string $namespace The namespace
  178. * @param array|string $paths The location(s) of the namespace
  179. *
  180. * @api
  181. */
  182. public function registerNamespace($namespace, $paths)
  183. {
  184. $this->namespaces[$namespace] = (array) $paths;
  185. }
  186. /**
  187. * Registers an array of classes using the PEAR naming convention.
  188. *
  189. * @param array $classes An array of classes (prefixes as keys and locations as values)
  190. *
  191. * @api
  192. */
  193. public function registerPrefixes(array $classes)
  194. {
  195. foreach ($classes as $prefix => $locations) {
  196. $this->prefixes[$prefix] = (array) $locations;
  197. }
  198. }
  199. /**
  200. * Registers a set of classes using the PEAR naming convention.
  201. *
  202. * @param string $prefix The classes prefix
  203. * @param array|string $paths The location(s) of the classes
  204. *
  205. * @api
  206. */
  207. public function registerPrefix($prefix, $paths)
  208. {
  209. $this->prefixes[$prefix] = (array) $paths;
  210. }
  211. /**
  212. * Registers this instance as an autoloader.
  213. *
  214. * @param Boolean $prepend Whether to prepend the autoloader or not
  215. *
  216. * @api
  217. */
  218. public function register($prepend = false)
  219. {
  220. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  221. }
  222. /**
  223. * Loads the given class or interface.
  224. *
  225. * @param string $class The name of the class
  226. */
  227. public function loadClass($class)
  228. {
  229. if ($file = $this->findFile($class)) {
  230. require $file;
  231. }
  232. }
  233. /**
  234. * Finds the path to the file where the class is defined.
  235. *
  236. * @param string $class The name of the class
  237. *
  238. * @return string|null The path, if found
  239. */
  240. public function findFile($class)
  241. {
  242. if ('\\' == $class[0]) {
  243. $class = substr($class, 1);
  244. }
  245. if (false !== $pos = strrpos($class, '\\')) {
  246. // namespaced class name
  247. $namespace = substr($class, 0, $pos);
  248. $className = substr($class, $pos + 1);
  249. $normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
  250. foreach ($this->namespaces as $ns => $dirs) {
  251. if (0 !== strpos($namespace, $ns)) {
  252. continue;
  253. }
  254. foreach ($dirs as $dir) {
  255. $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
  256. if (is_file($file)) {
  257. return $file;
  258. }
  259. }
  260. }
  261. foreach ($this->namespaceFallbacks as $dir) {
  262. $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
  263. if (is_file($file)) {
  264. return $file;
  265. }
  266. }
  267. } else {
  268. // PEAR-like class name
  269. $normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  270. foreach ($this->prefixes as $prefix => $dirs) {
  271. if (0 !== strpos($class, $prefix)) {
  272. continue;
  273. }
  274. foreach ($dirs as $dir) {
  275. $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
  276. if (is_file($file)) {
  277. return $file;
  278. }
  279. }
  280. }
  281. foreach ($this->prefixFallbacks as $dir) {
  282. $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
  283. if (is_file($file)) {
  284. return $file;
  285. }
  286. }
  287. }
  288. if ($this->useIncludePath && $file = stream_resolve_include_path($normalizedClass)) {
  289. return $file;
  290. }
  291. }
  292. }