ClassLoader.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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-0 directories for a given prefix, either
  84. * appending or prepending to the ones previously set for this prefix.
  85. *
  86. * @param string $prefix The prefix
  87. * @param array|string $paths The PSR-0 root directories
  88. * @param bool $prepend Whether to prepend the directories
  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 PSR-4 directories for a given namespace, either
  125. * appending or prepending to the ones previously set for this namespace.
  126. *
  127. * @param string $prefix The prefix/namespace, with trailing '\\'
  128. * @param array|string $paths The PSR-0 base directories
  129. * @param bool $prepend Whether to prepend the directories
  130. */
  131. public function addPsr4($prefix, $paths, $prepend = false)
  132. {
  133. if (!$prefix) {
  134. // Register directories for the root namespace.
  135. if ($prepend) {
  136. $this->fallbackDirsPsr4 = array_merge(
  137. (array) $paths,
  138. $this->fallbackDirsPsr4
  139. );
  140. } else {
  141. $this->fallbackDirsPsr4 = array_merge(
  142. $this->fallbackDirsPsr4,
  143. (array) $paths
  144. );
  145. }
  146. } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
  147. // Register directories for a new namespace.
  148. $length = strlen($prefix);
  149. if ('\\' !== $prefix[$length - 1]) {
  150. throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
  151. }
  152. $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
  153. $this->prefixDirsPsr4[$prefix] = (array) $paths;
  154. } elseif ($prepend) {
  155. // Prepend directories for an already registered namespace.
  156. $this->prefixDirsPsr4[$prefix] = array_merge(
  157. (array) $paths,
  158. $this->prefixDirsPsr4[$prefix]
  159. );
  160. } else {
  161. // Append directories for an already registered namespace.
  162. $this->prefixDirsPsr4[$prefix] = array_merge(
  163. $this->prefixDirsPsr4[$prefix],
  164. (array) $paths
  165. );
  166. }
  167. }
  168. /**
  169. * Registers a set of PSR-0 directories for a given prefix,
  170. * replacing any others previously set for this prefix.
  171. *
  172. * @param string $prefix The prefix
  173. * @param array|string $paths The PSR-0 base directories
  174. */
  175. public function set($prefix, $paths)
  176. {
  177. if (!$prefix) {
  178. $this->fallbackDirsPsr0 = (array) $paths;
  179. } else {
  180. $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
  181. }
  182. }
  183. /**
  184. * Registers a set of PSR-4 directories for a given namespace,
  185. * replacing any others previously set for this namespace.
  186. *
  187. * @param string $prefix The prefix/namespace, with trailing '\\'
  188. * @param array|string $paths The PSR-4 base directories
  189. */
  190. public function setPsr4($prefix, $paths)
  191. {
  192. if (!$prefix) {
  193. $this->fallbackDirsPsr4 = (array) $paths;
  194. } else {
  195. $length = strlen($prefix);
  196. if ('\\' !== $prefix[$length - 1]) {
  197. throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
  198. }
  199. $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
  200. $this->prefixDirsPsr4[$prefix] = (array) $paths;
  201. }
  202. }
  203. /**
  204. * Turns on searching the include path for class files.
  205. *
  206. * @param bool $useIncludePath
  207. */
  208. public function setUseIncludePath($useIncludePath)
  209. {
  210. $this->useIncludePath = $useIncludePath;
  211. }
  212. /**
  213. * Can be used to check if the autoloader uses the include path to check
  214. * for classes.
  215. *
  216. * @return bool
  217. */
  218. public function getUseIncludePath()
  219. {
  220. return $this->useIncludePath;
  221. }
  222. /**
  223. * Registers this instance as an autoloader.
  224. *
  225. * @param bool $prepend Whether to prepend the autoloader or not
  226. */
  227. public function register($prepend = false)
  228. {
  229. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  230. }
  231. /**
  232. * Unregisters this instance as an autoloader.
  233. */
  234. public function unregister()
  235. {
  236. spl_autoload_unregister(array($this, 'loadClass'));
  237. }
  238. /**
  239. * Loads the given class or interface.
  240. *
  241. * @param string $class The name of the class
  242. * @return bool|null True if loaded, null otherwise
  243. */
  244. public function loadClass($class)
  245. {
  246. if ($file = $this->findFile($class)) {
  247. includeFile($file);
  248. return true;
  249. }
  250. }
  251. /**
  252. * Finds the path to the file where the class is defined.
  253. *
  254. * @param string $class The name of the class
  255. *
  256. * @return string|false The path if found, false otherwise
  257. */
  258. public function findFile($class)
  259. {
  260. // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
  261. if ('\\' == $class[0]) {
  262. $class = substr($class, 1);
  263. }
  264. // class map lookup
  265. if (isset($this->classMap[$class])) {
  266. return $this->classMap[$class];
  267. }
  268. $file = $this->findFileWithExtension($class, '.php');
  269. // Search for Hack files if we are running on HHVM
  270. if ($file === null && defined('HHVM_VERSION')) {
  271. $file = $this->findFileWithExtension($class, '.hh');
  272. }
  273. if ($file === null) {
  274. // Remember that this class does not exist.
  275. return $this->classMap[$class] = false;
  276. }
  277. return $file;
  278. }
  279. private function findFileWithExtension($class, $ext)
  280. {
  281. // PSR-4 lookup
  282. $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
  283. $first = $class[0];
  284. if (isset($this->prefixLengthsPsr4[$first])) {
  285. foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
  286. if (0 === strpos($class, $prefix)) {
  287. foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
  288. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
  289. return $file;
  290. }
  291. }
  292. }
  293. }
  294. }
  295. // PSR-4 fallback dirs
  296. foreach ($this->fallbackDirsPsr4 as $dir) {
  297. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
  298. return $file;
  299. }
  300. }
  301. // PSR-0 lookup
  302. if (false !== $pos = strrpos($class, '\\')) {
  303. // namespaced class name
  304. $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
  305. . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
  306. } else {
  307. // PEAR-like class name
  308. $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
  309. }
  310. if (isset($this->prefixesPsr0[$first])) {
  311. foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
  312. if (0 === strpos($class, $prefix)) {
  313. foreach ($dirs as $dir) {
  314. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
  315. return $file;
  316. }
  317. }
  318. }
  319. }
  320. }
  321. // PSR-0 fallback dirs
  322. foreach ($this->fallbackDirsPsr0 as $dir) {
  323. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
  324. return $file;
  325. }
  326. }
  327. // PSR-0 include paths.
  328. if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
  329. return $file;
  330. }
  331. }
  332. }
  333. /**
  334. * Scope isolated include.
  335. *
  336. * Prevents access to $this/self from included files.
  337. */
  338. function includeFile($file)
  339. {
  340. include $file;
  341. }