PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Associates/Whoops/vendor/composer/ClassLoader.php

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