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. if (!$prefix) {
  192. $this->fallbackDirsPsr4 = (array) $paths;
  193. } else {
  194. $length = strlen($prefix);
  195. if ('\\' !== $prefix[$length - 1]) {
  196. throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
  197. }
  198. $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
  199. $this->prefixDirsPsr4[$prefix] = (array) $paths;
  200. }
  201. }
  202. /**
  203. * Turns on searching the include path for class files.
  204. *
  205. * @param bool $useIncludePath
  206. */
  207. public function setUseIncludePath($useIncludePath)
  208. {
  209. $this->useIncludePath = $useIncludePath;
  210. }
  211. /**
  212. * Can be used to check if the autoloader uses the include path to check
  213. * for classes.
  214. *
  215. * @return bool
  216. */
  217. public function getUseIncludePath()
  218. {
  219. return $this->useIncludePath;
  220. }
  221. /**
  222. * Registers this instance as an autoloader.
  223. *
  224. * @param bool $prepend Whether to prepend the autoloader or not
  225. */
  226. public function register($prepend = false)
  227. {
  228. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  229. }
  230. /**
  231. * Unregisters this instance as an autoloader.
  232. */
  233. public function unregister()
  234. {
  235. spl_autoload_unregister(array($this, 'loadClass'));
  236. }
  237. /**
  238. * Loads the given class or interface.
  239. *
  240. * @param string $class The name of the class
  241. * @return bool|null True if loaded, null otherwise
  242. */
  243. public function loadClass($class)
  244. {
  245. if ($file = $this->findFile($class)) {
  246. includeFile($file);
  247. return true;
  248. }
  249. }
  250. /**
  251. * Finds the path to the file where the class is defined.
  252. *
  253. * @param string $class The name of the class
  254. *
  255. * @return string|false The path if found, false otherwise
  256. */
  257. public function findFile($class)
  258. {
  259. // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
  260. if ('\\' == $class[0]) {
  261. $class = substr($class, 1);
  262. }
  263. // class map lookup
  264. if (isset($this->classMap[$class])) {
  265. return $this->classMap[$class];
  266. }
  267. $file = $this->findFileWithExtension($class, '.php');
  268. // Search for Hack files if we are running on HHVM
  269. if ($file === null && defined('HHVM_VERSION')) {
  270. $file = $this->findFileWithExtension($class, '.hh');
  271. }
  272. if ($file === null) {
  273. // Remember that this class does not exist.
  274. return $this->classMap[$class] = false;
  275. }
  276. return $file;
  277. }
  278. private function findFileWithExtension($class, $ext)
  279. {
  280. // PSR-4 lookup
  281. $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
  282. $first = $class[0];
  283. if (isset($this->prefixLengthsPsr4[$first])) {
  284. foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
  285. if (0 === strpos($class, $prefix)) {
  286. foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
  287. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
  288. return $file;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. // PSR-4 fallback dirs
  295. foreach ($this->fallbackDirsPsr4 as $dir) {
  296. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
  297. return $file;
  298. }
  299. }
  300. // PSR-0 lookup
  301. if (false !== $pos = strrpos($class, '\\')) {
  302. // namespaced class name
  303. $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
  304. . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
  305. } else {
  306. // PEAR-like class name
  307. $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
  308. }
  309. if (isset($this->prefixesPsr0[$first])) {
  310. foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
  311. if (0 === strpos($class, $prefix)) {
  312. foreach ($dirs as $dir) {
  313. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
  314. return $file;
  315. }
  316. }
  317. }
  318. }
  319. }
  320. // PSR-0 fallback dirs
  321. foreach ($this->fallbackDirsPsr0 as $dir) {
  322. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
  323. return $file;
  324. }
  325. }
  326. // PSR-0 include paths.
  327. if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
  328. return $file;
  329. }
  330. }
  331. }
  332. /**
  333. * Scope isolated include.
  334. *
  335. * Prevents access to $this/self from included files.
  336. */
  337. function includeFile($file)
  338. {
  339. include $file;
  340. }