ClassLoader.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. private $prefixes = array();
  43. private $fallbackDirs = array();
  44. private $useIncludePath = false;
  45. private $classMap = array();
  46. public function getPrefixes()
  47. {
  48. return $this->prefixes;
  49. }
  50. public function getFallbackDirs()
  51. {
  52. return $this->fallbackDirs;
  53. }
  54. public function getClassMap()
  55. {
  56. return $this->classMap;
  57. }
  58. /**
  59. * @param array $classMap Class to filename map
  60. */
  61. public function addClassMap(array $classMap)
  62. {
  63. if ($this->classMap) {
  64. $this->classMap = array_merge($this->classMap, $classMap);
  65. } else {
  66. $this->classMap = $classMap;
  67. }
  68. }
  69. /**
  70. * Registers a set of classes, merging with any others previously set.
  71. *
  72. * @param string $prefix The classes prefix
  73. * @param array|string $paths The location(s) of the classes
  74. * @param bool $prepend Prepend the location(s)
  75. */
  76. public function add($prefix, $paths, $prepend = false)
  77. {
  78. if (!$prefix) {
  79. if ($prepend) {
  80. $this->fallbackDirs = array_merge(
  81. (array) $paths,
  82. $this->fallbackDirs
  83. );
  84. } else {
  85. $this->fallbackDirs = array_merge(
  86. $this->fallbackDirs,
  87. (array) $paths
  88. );
  89. }
  90. return;
  91. }
  92. if (!isset($this->prefixes[$prefix])) {
  93. $this->prefixes[$prefix] = (array) $paths;
  94. return;
  95. }
  96. if ($prepend) {
  97. $this->prefixes[$prefix] = array_merge(
  98. (array) $paths,
  99. $this->prefixes[$prefix]
  100. );
  101. } else {
  102. $this->prefixes[$prefix] = array_merge(
  103. $this->prefixes[$prefix],
  104. (array) $paths
  105. );
  106. }
  107. }
  108. /**
  109. * Registers a set of classes, replacing any others previously set.
  110. *
  111. * @param string $prefix The classes prefix
  112. * @param array|string $paths The location(s) of the classes
  113. */
  114. public function set($prefix, $paths)
  115. {
  116. if (!$prefix) {
  117. $this->fallbackDirs = (array) $paths;
  118. return;
  119. }
  120. $this->prefixes[$prefix] = (array) $paths;
  121. }
  122. /**
  123. * Turns on searching the include path for class files.
  124. *
  125. * @param bool $useIncludePath
  126. */
  127. public function setUseIncludePath($useIncludePath)
  128. {
  129. $this->useIncludePath = $useIncludePath;
  130. }
  131. /**
  132. * Can be used to check if the autoloader uses the include path to check
  133. * for classes.
  134. *
  135. * @return bool
  136. */
  137. public function getUseIncludePath()
  138. {
  139. return $this->useIncludePath;
  140. }
  141. /**
  142. * Registers this instance as an autoloader.
  143. *
  144. * @param bool $prepend Whether to prepend the autoloader or not
  145. */
  146. public function register($prepend = false)
  147. {
  148. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  149. }
  150. /**
  151. * Unregisters this instance as an autoloader.
  152. */
  153. public function unregister()
  154. {
  155. spl_autoload_unregister(array($this, 'loadClass'));
  156. }
  157. /**
  158. * Loads the given class or interface.
  159. *
  160. * @param string $class The name of the class
  161. * @return bool|null True, if loaded
  162. */
  163. public function loadClass($class)
  164. {
  165. if ($file = $this->findFile($class)) {
  166. include $file;
  167. return true;
  168. }
  169. }
  170. /**
  171. * Finds the path to the file where the class is defined.
  172. *
  173. * @param string $class The name of the class
  174. *
  175. * @return string|null The path, if found
  176. */
  177. public function findFile($class)
  178. {
  179. if ('\\' == $class[0]) {
  180. $class = substr($class, 1);
  181. }
  182. if (isset($this->classMap[$class])) {
  183. return $this->classMap[$class];
  184. }
  185. if (false !== $pos = strrpos($class, '\\')) {
  186. // namespaced class name
  187. $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR;
  188. $className = substr($class, $pos + 1);
  189. } else {
  190. // PEAR-like class name
  191. $classPath = null;
  192. $className = $class;
  193. }
  194. $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  195. foreach ($this->prefixes as $prefix => $dirs) {
  196. if (0 === strpos($class, $prefix)) {
  197. foreach ($dirs as $dir) {
  198. if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) {
  199. return $dir . DIRECTORY_SEPARATOR . $classPath;
  200. }
  201. }
  202. }
  203. }
  204. foreach ($this->fallbackDirs as $dir) {
  205. if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) {
  206. return $dir . DIRECTORY_SEPARATOR . $classPath;
  207. }
  208. }
  209. if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) {
  210. return $file;
  211. }
  212. return $this->classMap[$class] = false;
  213. }
  214. }