ClassLoader.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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
  71. *
  72. * @param string $prefix The classes prefix
  73. * @param array|string $paths The location(s) of the classes
  74. */
  75. public function add($prefix, $paths)
  76. {
  77. if (!$prefix) {
  78. foreach ((array) $paths as $path) {
  79. $this->fallbackDirs[] = $path;
  80. }
  81. return;
  82. }
  83. if (isset($this->prefixes[$prefix])) {
  84. $this->prefixes[$prefix] = array_merge(
  85. $this->prefixes[$prefix],
  86. (array) $paths
  87. );
  88. } else {
  89. $this->prefixes[$prefix] = (array) $paths;
  90. }
  91. }
  92. /**
  93. * Turns on searching the include path for class files.
  94. *
  95. * @param bool $useIncludePath
  96. */
  97. public function setUseIncludePath($useIncludePath)
  98. {
  99. $this->useIncludePath = $useIncludePath;
  100. }
  101. /**
  102. * Can be used to check if the autoloader uses the include path to check
  103. * for classes.
  104. *
  105. * @return bool
  106. */
  107. public function getUseIncludePath()
  108. {
  109. return $this->useIncludePath;
  110. }
  111. /**
  112. * Registers this instance as an autoloader.
  113. *
  114. * @param bool $prepend Whether to prepend the autoloader or not
  115. */
  116. public function register($prepend = false)
  117. {
  118. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  119. }
  120. /**
  121. * Unregisters this instance as an autoloader.
  122. */
  123. public function unregister()
  124. {
  125. spl_autoload_unregister(array($this, 'loadClass'));
  126. }
  127. /**
  128. * Loads the given class or interface.
  129. *
  130. * @param string $class The name of the class
  131. * @return bool|null True, if loaded
  132. */
  133. public function loadClass($class)
  134. {
  135. if ($file = $this->findFile($class)) {
  136. include $file;
  137. return true;
  138. }
  139. }
  140. /**
  141. * Finds the path to the file where the class is defined.
  142. *
  143. * @param string $class The name of the class
  144. *
  145. * @return string|null The path, if found
  146. */
  147. public function findFile($class)
  148. {
  149. if (isset($this->classMap[$class])) {
  150. return $this->classMap[$class];
  151. }
  152. if ('\\' == $class[0]) {
  153. $class = substr($class, 1);
  154. }
  155. if (false !== $pos = strrpos($class, '\\')) {
  156. // namespaced class name
  157. $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR;
  158. $className = substr($class, $pos + 1);
  159. } else {
  160. // PEAR-like class name
  161. $classPath = null;
  162. $className = $class;
  163. }
  164. $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  165. foreach ($this->prefixes as $prefix => $dirs) {
  166. if (0 === strpos($class, $prefix)) {
  167. foreach ($dirs as $dir) {
  168. if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) {
  169. return $dir . DIRECTORY_SEPARATOR . $classPath;
  170. }
  171. }
  172. }
  173. }
  174. foreach ($this->fallbackDirs as $dir) {
  175. if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) {
  176. return $dir . DIRECTORY_SEPARATOR . $classPath;
  177. }
  178. }
  179. if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) {
  180. return $file;
  181. }
  182. }
  183. }