ClassLoader.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Autoload;
  12. /**
  13. * ClassLoader implements a PSR-0 class loader
  14. *
  15. * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
  16. *
  17. * $loader = new \Composer\Autoload\ClassLoader();
  18. *
  19. * // register classes with namespaces
  20. * $loader->add('Symfony\Component', __DIR__.'/component');
  21. * $loader->add('Symfony', __DIR__.'/framework');
  22. *
  23. * // activate the autoloader
  24. * $loader->register();
  25. *
  26. * // to enable searching the include path (eg. for PEAR packages)
  27. * $loader->setUseIncludePath(true);
  28. *
  29. * In this example, if you try to use a class in the Symfony\Component
  30. * namespace or one of its children (Symfony\Component\Console for instance),
  31. * the autoloader will first look for the class under the component/
  32. * directory, and it will then fallback to the framework/ directory if not
  33. * found before giving up.
  34. *
  35. * This class is loosely based on the Symfony UniversalClassLoader.
  36. *
  37. * @author Fabien Potencier <fabien@symfony.com>
  38. * @author Jordi Boggiano <j.boggiano@seld.be>
  39. */
  40. class ClassLoader
  41. {
  42. // PSR-4
  43. private $prefixLengthsPsr4 = array();
  44. private $prefixDirsPsr4 = array();
  45. private $fallbackDirsPsr4 = array();
  46. // PSR-0
  47. private $prefixesPsr0 = array();
  48. private $fallbackDirsPsr0 = array();
  49. private $useIncludePath = false;
  50. private $classMap = array();
  51. public function getPrefixes()
  52. {
  53. return call_user_func_array('array_merge', $this->prefixesPsr0);
  54. }
  55. public function getPrefixesPsr4()
  56. {
  57. return $this->prefixDirsPsr4;
  58. }
  59. public function getFallbackDirs()
  60. {
  61. return $this->fallbackDirsPsr0;
  62. }
  63. public function getFallbackDirsPsr4()
  64. {
  65. return $this->fallbackDirsPsr4;
  66. }
  67. public function getClassMap()
  68. {
  69. return $this->classMap;
  70. }
  71. /**
  72. * @param array $classMap Class to filename map
  73. */
  74. public function addClassMap(array $classMap)
  75. {
  76. if ($this->classMap) {
  77. $this->classMap = array_merge($this->classMap, $classMap);
  78. } else {
  79. $this->classMap = $classMap;
  80. }
  81. }
  82. /**
  83. * Registers a set of PSR-4 directories for a given namespace, either
  84. * appending or prepending to the ones previously set for this namespace.
  85. *
  86. * @param string $prefix The namespace, with trailing '\\'.
  87. * @param array|string $paths The location(s) of the classes
  88. * @param bool $prepend Prepend the location(s)
  89. */
  90. public function add($prefix, $paths, $prepend = false)
  91. {
  92. if (!$prefix) {
  93. if ($prepend) {
  94. $this->fallbackDirsPsr0 = array_merge(
  95. (array) $paths,
  96. $this->fallbackDirsPsr0
  97. );
  98. } else {
  99. $this->fallbackDirsPsr0 = array_merge(
  100. $this->fallbackDirsPsr0,
  101. (array) $paths
  102. );
  103. }
  104. return;
  105. }
  106. $first = $prefix[0];
  107. if (!isset($this->prefixesPsr0[$first][$prefix])) {
  108. $this->prefixesPsr0[$first][$prefix] = (array) $paths;
  109. return;
  110. }
  111. if ($prepend) {
  112. $this->prefixesPsr0[$first][$prefix] = array_merge(
  113. (array) $paths,
  114. $this->prefixesPsr0[$first][$prefix]
  115. );
  116. } else {
  117. $this->prefixesPsr0[$first][$prefix] = array_merge(
  118. $this->prefixesPsr0[$first][$prefix],
  119. (array) $paths
  120. );
  121. }
  122. }
  123. /**
  124. * Registers a set of classes, merging with any others previously set.
  125. *
  126. * @param string $prefix The namespace, with trailing '\\'.
  127. * @param array|string $paths The location(s) of the classes
  128. * @param bool $prepend Prepend the location(s)
  129. */
  130. public function addPsr4($prefix, $paths, $prepend = false)
  131. {
  132. if (!$prefix) {
  133. // Register directories for the root namespace.
  134. if ($prepend) {
  135. $this->fallbackDirsPsr4 = array_merge(
  136. (array) $paths,
  137. $this->fallbackDirsPsr4
  138. );
  139. } else {
  140. $this->fallbackDirsPsr4 = array_merge(
  141. $this->fallbackDirsPsr4,
  142. (array) $paths
  143. );
  144. }
  145. } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
  146. // Register directories for a new namespace.
  147. $length = strlen($prefix);
  148. if ('\\' !== $prefix[$length - 1]) {
  149. throw new \Exception("A non-empty PSR-4 prefix must end with a namespace separator.");
  150. }
  151. $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
  152. $this->prefixDirsPsr4[$prefix] = (array) $paths;
  153. } elseif ($prepend) {
  154. // Prepend directories for an already registered namespace.
  155. $this->prefixDirsPsr4[$prefix] = array_merge(
  156. (array) $paths,
  157. $this->prefixDirsPsr4[$prefix]
  158. );
  159. } else {
  160. // Append directories for an already registered namespace.
  161. $this->prefixDirsPsr4[$prefix] = array_merge(
  162. $this->prefixDirsPsr4[$prefix],
  163. (array) $paths
  164. );
  165. }
  166. }
  167. /**
  168. * Registers a set of classes, replacing any others previously set.
  169. *
  170. * @param string $prefix The classes prefix
  171. * @param array|string $paths The location(s) of the classes
  172. */
  173. public function set($prefix, $paths)
  174. {
  175. if (!$prefix) {
  176. $this->fallbackDirsPsr0 = (array) $paths;
  177. } else {
  178. $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
  179. }
  180. }
  181. /**
  182. * Registers a set of PSR-4 directories for a given namespace,
  183. * replacing any others previously set for this namespace.
  184. *
  185. * @param string $prefix The namespace, with trailing slash.
  186. * @param array|string $paths The location(s) of the classes
  187. */
  188. public function setPsr4($prefix, $paths) {
  189. if (!$prefix) {
  190. $this->fallbackDirsPsr4 = (array) $paths;
  191. } else {
  192. $length = strlen($prefix);
  193. if ('\\' !== $prefix[$length - 1]) {
  194. throw new \Exception("A non-empty PSR-4 prefix must end with a namespace separator.");
  195. }
  196. $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
  197. $this->prefixDirsPsr4[$prefix] = (array) $paths;
  198. }
  199. }
  200. /**
  201. * Turns on searching the include path for class files.
  202. *
  203. * @param bool $useIncludePath
  204. */
  205. public function setUseIncludePath($useIncludePath)
  206. {
  207. $this->useIncludePath = $useIncludePath;
  208. }
  209. /**
  210. * Can be used to check if the autoloader uses the include path to check
  211. * for classes.
  212. *
  213. * @return bool
  214. */
  215. public function getUseIncludePath()
  216. {
  217. return $this->useIncludePath;
  218. }
  219. /**
  220. * Registers this instance as an autoloader.
  221. *
  222. * @param bool $prepend Whether to prepend the autoloader or not
  223. */
  224. public function register($prepend = false)
  225. {
  226. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  227. }
  228. /**
  229. * Unregisters this instance as an autoloader.
  230. */
  231. public function unregister()
  232. {
  233. spl_autoload_unregister(array($this, 'loadClass'));
  234. }
  235. /**
  236. * Loads the given class or interface.
  237. *
  238. * @param string $class The name of the class
  239. * @return bool|null True if loaded, null otherwise
  240. */
  241. public function loadClass($class)
  242. {
  243. if ($file = $this->findFile($class)) {
  244. include $file;
  245. return true;
  246. }
  247. }
  248. /**
  249. * Finds the path to the file where the class is defined.
  250. *
  251. * @param string $class The name of the class
  252. *
  253. * @return string|false The path if found, false otherwise
  254. */
  255. public function findFile($class)
  256. {
  257. // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
  258. if ('\\' == $class[0]) {
  259. $class = substr($class, 1);
  260. }
  261. // class map lookup
  262. if (isset($this->classMap[$class])) {
  263. return $this->classMap[$class];
  264. }
  265. // PSR-4 lookup
  266. $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php';
  267. $first = $class[0];
  268. if (isset($this->prefixLengthsPsr4[$first])) {
  269. foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
  270. if (0 === strpos($class, $prefix)) {
  271. foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
  272. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
  273. return $file;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. // PSR-4 fallback dirs
  280. foreach ($this->fallbackDirsPsr4 as $dir) {
  281. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
  282. return $file;
  283. }
  284. }
  285. // PSR-0 lookup
  286. if (false !== $pos = strrpos($class, '\\')) {
  287. // namespaced class name
  288. $logicalPathPsr0
  289. = substr($logicalPathPsr4, 0, $pos + 1)
  290. . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR)
  291. ;
  292. } else {
  293. // PEAR-like class name
  294. $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR);
  295. }
  296. if (isset($this->prefixesPsr0[$first])) {
  297. foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
  298. if (0 === strpos($class, $prefix)) {
  299. foreach ($dirs as $dir) {
  300. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
  301. return $file;
  302. }
  303. }
  304. }
  305. }
  306. }
  307. // PSR-0 fallback dirs
  308. foreach ($this->fallbackDirsPsr0 as $dir) {
  309. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
  310. return $file;
  311. }
  312. }
  313. // PSR-0 include paths.
  314. if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
  315. return $file;
  316. }
  317. // Remember that this class does not exist.
  318. return $this->classMap[$class] = false;
  319. }
  320. }