ComposerAutoloadClassLoader

ClassLoader implements a PSR-0 class loader.

Defined (1)

The class is defined in the following location(s).

/vendor/composer/ClassLoader.php  
  1. class ClassLoader 
  2. // PSR-4 
  3. private $prefixLengthsPsr4 = array(); 
  4. private $prefixDirsPsr4 = array(); 
  5. private $fallbackDirsPsr4 = array(); 
  6.  
  7. // PSR-0 
  8. private $prefixesPsr0 = array(); 
  9. private $fallbackDirsPsr0 = array(); 
  10.  
  11. private $useIncludePath = false; 
  12. private $classMap = array(); 
  13. private $classMapAuthoritative = false; 
  14. private $missingClasses = array(); 
  15.  
  16. public function getPrefixes() 
  17. if (!empty($this->prefixesPsr0)) { 
  18. return call_user_func_array('array_merge', $this->prefixesPsr0); 
  19.  
  20. return array(); 
  21.  
  22. public function getPrefixesPsr4() 
  23. return $this->prefixDirsPsr4; 
  24.  
  25. public function getFallbackDirs() 
  26. return $this->fallbackDirsPsr0; 
  27.  
  28. public function getFallbackDirsPsr4() 
  29. return $this->fallbackDirsPsr4; 
  30.  
  31. public function getClassMap() 
  32. return $this->classMap; 
  33.  
  34. /** 
  35. * @param array $classMap Class to filename map 
  36. */ 
  37. public function addClassMap(array $classMap) 
  38. if ($this->classMap) { 
  39. $this->classMap = array_merge($this->classMap, $classMap); 
  40. } else { 
  41. $this->classMap = $classMap; 
  42.  
  43. /** 
  44. * Registers a set of PSR-0 directories for a given prefix, either 
  45. * appending or prepending to the ones previously set for this prefix. 
  46. * @param string $prefix The prefix 
  47. * @param array|string $paths The PSR-0 root directories 
  48. * @param bool $prepend Whether to prepend the directories 
  49. */ 
  50. public function add($prefix, $paths, $prepend = false) 
  51. if (!$prefix) { 
  52. if ($prepend) { 
  53. $this->fallbackDirsPsr0 = array_merge( 
  54. (array) $paths,  
  55. $this->fallbackDirsPsr0 
  56. ); 
  57. } else { 
  58. $this->fallbackDirsPsr0 = array_merge( 
  59. $this->fallbackDirsPsr0,  
  60. (array) $paths 
  61. ); 
  62.  
  63. return; 
  64.  
  65. $first = $prefix[0]; 
  66. if (!isset($this->prefixesPsr0[$first][$prefix])) { 
  67. $this->prefixesPsr0[$first][$prefix] = (array) $paths; 
  68.  
  69. return; 
  70. if ($prepend) { 
  71. $this->prefixesPsr0[$first][$prefix] = array_merge( 
  72. (array) $paths,  
  73. $this->prefixesPsr0[$first][$prefix] 
  74. ); 
  75. } else { 
  76. $this->prefixesPsr0[$first][$prefix] = array_merge( 
  77. $this->prefixesPsr0[$first][$prefix],  
  78. (array) $paths 
  79. ); 
  80.  
  81. /** 
  82. * Registers a set of PSR-4 directories for a given namespace, either 
  83. * appending or prepending to the ones previously set for this namespace. 
  84. * @param string $prefix The prefix/namespace, with trailing '\\' 
  85. * @param array|string $paths The PSR-4 base directories 
  86. * @param bool $prepend Whether to prepend the directories 
  87. * @throws \InvalidArgumentException 
  88. */ 
  89. public function addPsr4($prefix, $paths, $prepend = false) 
  90. if (!$prefix) { 
  91. // Register directories for the root namespace. 
  92. if ($prepend) { 
  93. $this->fallbackDirsPsr4 = array_merge( 
  94. (array) $paths,  
  95. $this->fallbackDirsPsr4 
  96. ); 
  97. } else { 
  98. $this->fallbackDirsPsr4 = array_merge( 
  99. $this->fallbackDirsPsr4,  
  100. (array) $paths 
  101. ); 
  102. } elseif (!isset($this->prefixDirsPsr4[$prefix])) { 
  103. // Register directories for a new namespace. 
  104. $length = strlen($prefix); 
  105. if ('\\' !== $prefix[$length - 1]) { 
  106. throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); 
  107. $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; 
  108. $this->prefixDirsPsr4[$prefix] = (array) $paths; 
  109. } elseif ($prepend) { 
  110. // Prepend directories for an already registered namespace. 
  111. $this->prefixDirsPsr4[$prefix] = array_merge( 
  112. (array) $paths,  
  113. $this->prefixDirsPsr4[$prefix] 
  114. ); 
  115. } else { 
  116. // Append directories for an already registered namespace. 
  117. $this->prefixDirsPsr4[$prefix] = array_merge( 
  118. $this->prefixDirsPsr4[$prefix],  
  119. (array) $paths 
  120. ); 
  121.  
  122. /** 
  123. * Registers a set of PSR-0 directories for a given prefix,  
  124. * replacing any others previously set for this prefix. 
  125. * @param string $prefix The prefix 
  126. * @param array|string $paths The PSR-0 base directories 
  127. */ 
  128. public function set($prefix, $paths) 
  129. if (!$prefix) { 
  130. $this->fallbackDirsPsr0 = (array) $paths; 
  131. } else { 
  132. $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; 
  133.  
  134. /** 
  135. * Registers a set of PSR-4 directories for a given namespace,  
  136. * replacing any others previously set for this namespace. 
  137. * @param string $prefix The prefix/namespace, with trailing '\\' 
  138. * @param array|string $paths The PSR-4 base directories 
  139. * @throws \InvalidArgumentException 
  140. */ 
  141. public function setPsr4($prefix, $paths) 
  142. if (!$prefix) { 
  143. $this->fallbackDirsPsr4 = (array) $paths; 
  144. } else { 
  145. $length = strlen($prefix); 
  146. if ('\\' !== $prefix[$length - 1]) { 
  147. throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); 
  148. $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; 
  149. $this->prefixDirsPsr4[$prefix] = (array) $paths; 
  150.  
  151. /** 
  152. * Turns on searching the include path for class files. 
  153. * @param bool $useIncludePath 
  154. */ 
  155. public function setUseIncludePath($useIncludePath) 
  156. $this->useIncludePath = $useIncludePath; 
  157.  
  158. /** 
  159. * Can be used to check if the autoloader uses the include path to check 
  160. * for classes. 
  161. * @return bool 
  162. */ 
  163. public function getUseIncludePath() 
  164. return $this->useIncludePath; 
  165.  
  166. /** 
  167. * Turns off searching the prefix and fallback directories for classes 
  168. * that have not been registered with the class map. 
  169. * @param bool $classMapAuthoritative 
  170. */ 
  171. public function setClassMapAuthoritative($classMapAuthoritative) 
  172. $this->classMapAuthoritative = $classMapAuthoritative; 
  173.  
  174. /** 
  175. * Should class lookup fail if not found in the current class map? 
  176. * @return bool 
  177. */ 
  178. public function isClassMapAuthoritative() 
  179. return $this->classMapAuthoritative; 
  180.  
  181. /** 
  182. * Registers this instance as an autoloader. 
  183. * @param bool $prepend Whether to prepend the autoloader or not 
  184. */ 
  185. public function register($prepend = false) 
  186. spl_autoload_register(array($this, 'loadClass'), true, $prepend); 
  187.  
  188. /** 
  189. * Unregisters this instance as an autoloader. 
  190. */ 
  191. public function unregister() 
  192. spl_autoload_unregister(array($this, 'loadClass')); 
  193.  
  194. /** 
  195. * Loads the given class or interface. 
  196. * @param string $class The name of the class 
  197. * @return bool|null True if loaded, null otherwise 
  198. */ 
  199. public function loadClass($class) 
  200. if ($file = $this->findFile($class)) { 
  201. includeFile($file); 
  202.  
  203. return true; 
  204.  
  205. /** 
  206. * Finds the path to the file where the class is defined. 
  207. * @param string $class The name of the class 
  208. * @return string|false The path if found, false otherwise 
  209. */ 
  210. public function findFile($class) 
  211. // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731 
  212. if ('\\' == $class[0]) { 
  213. $class = substr($class, 1); 
  214.  
  215. // class map lookup 
  216. if (isset($this->classMap[$class])) { 
  217. return $this->classMap[$class]; 
  218. if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { 
  219. return false; 
  220.  
  221. $file = $this->findFileWithExtension($class, '.php'); 
  222.  
  223. // Search for Hack files if we are running on HHVM 
  224. if (false === $file && defined('HHVM_VERSION')) { 
  225. $file = $this->findFileWithExtension($class, '.hh'); 
  226.  
  227. if (false === $file) { 
  228. // Remember that this class does not exist. 
  229. $this->missingClasses[$class] = true; 
  230.  
  231. return $file; 
  232.  
  233. private function findFileWithExtension($class, $ext) 
  234. // PSR-4 lookup 
  235. $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; 
  236.  
  237. $first = $class[0]; 
  238. if (isset($this->prefixLengthsPsr4[$first])) { 
  239. foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) { 
  240. if (0 === strpos($class, $prefix)) { 
  241. foreach ($this->prefixDirsPsr4[$prefix] as $dir) { 
  242. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) { 
  243. return $file; 
  244.  
  245. // PSR-4 fallback dirs 
  246. foreach ($this->fallbackDirsPsr4 as $dir) { 
  247. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { 
  248. return $file; 
  249.  
  250. // PSR-0 lookup 
  251. if (false !== $pos = strrpos($class, '\\')) { 
  252. // namespaced class name 
  253. $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) 
  254. . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); 
  255. } else { 
  256. // PEAR-like class name 
  257. $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; 
  258.  
  259. if (isset($this->prefixesPsr0[$first])) { 
  260. foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { 
  261. if (0 === strpos($class, $prefix)) { 
  262. foreach ($dirs as $dir) { 
  263. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { 
  264. return $file; 
  265.  
  266. // PSR-0 fallback dirs 
  267. foreach ($this->fallbackDirsPsr0 as $dir) { 
  268. if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { 
  269. return $file; 
  270.  
  271. // PSR-0 include paths. 
  272. if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { 
  273. return $file; 
  274.  
  275. return false;