autoload.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * Autoloads files for PHP_CodeSniffer and tracks what has been loaded.
  4. *
  5. * Due to different namespaces being used for custom coding standards,
  6. * the autoloader keeps track of what class is loaded after a file is included,
  7. * even if the file is ultimately included by another autoloader (such as composer).
  8. *
  9. * This allows PHP_CodeSniffer to request the class name after loading a class
  10. * when it only knows the filename, without having to parse the file to find it.
  11. *
  12. * @author Greg Sherwood <gsherwood@squiz.net>
  13. * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  14. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  15. */
  16. namespace PHP_CodeSniffer;
  17. if (class_exists('PHP_CodeSniffer\Autoload', false) === false) {
  18. class Autoload
  19. {
  20. /**
  21. * The composer autoloader.
  22. *
  23. * @var Composer\Autoload\ClassLoader
  24. */
  25. private static $composerAutoloader = null;
  26. /**
  27. * A mapping of file names to class names.
  28. *
  29. * @var array<string, string>
  30. */
  31. private static $loadedClasses = [];
  32. /**
  33. * A mapping of class names to file names.
  34. *
  35. * @var array<string, string>
  36. */
  37. private static $loadedFiles = [];
  38. /**
  39. * A list of additional directories to search during autoloading.
  40. *
  41. * This is typically a list of coding standard directories.
  42. *
  43. * @var string[]
  44. */
  45. private static $searchPaths = [];
  46. /**
  47. * Loads a class.
  48. *
  49. * This method only loads classes that exist in the PHP_CodeSniffer namespace.
  50. * All other classes are ignored and loaded by subsequent autoloaders.
  51. *
  52. * @param string $class The name of the class to load.
  53. *
  54. * @return bool
  55. */
  56. public static function load($class)
  57. {
  58. // Include the composer autoloader if there is one, but re-register it
  59. // so this autoloader runs before the composer one as we need to include
  60. // all files so we can figure out what the class/interface/trait name is.
  61. if (self::$composerAutoloader === null) {
  62. // Make sure we don't try to load any of Composer's classes
  63. // while the autoloader is being setup.
  64. if (strpos($class, 'Composer\\') === 0) {
  65. return;
  66. }
  67. if (strpos(__DIR__, 'phar://') !== 0
  68. && file_exists(__DIR__.'/../../autoload.php') === true
  69. ) {
  70. self::$composerAutoloader = include __DIR__.'/../../autoload.php';
  71. if (self::$composerAutoloader instanceof \Composer\Autoload\ClassLoader) {
  72. self::$composerAutoloader->unregister();
  73. self::$composerAutoloader->register();
  74. } else {
  75. // Something went wrong, so keep going without the autoloader
  76. // although namespaced sniffs might error.
  77. self::$composerAutoloader = false;
  78. }
  79. } else {
  80. self::$composerAutoloader = false;
  81. }
  82. }//end if
  83. $ds = DIRECTORY_SEPARATOR;
  84. $path = false;
  85. if (substr($class, 0, 16) === 'PHP_CodeSniffer\\') {
  86. if (substr($class, 0, 22) === 'PHP_CodeSniffer\Tests\\') {
  87. $isInstalled = !is_dir(__DIR__.$ds.'tests');
  88. if ($isInstalled === false) {
  89. $path = __DIR__.$ds.'tests';
  90. } else {
  91. $path = '@test_dir@'.$ds.'PHP_CodeSniffer'.$ds.'CodeSniffer';
  92. }
  93. $path .= $ds.substr(str_replace('\\', $ds, $class), 22).'.php';
  94. } else {
  95. $path = __DIR__.$ds.'src'.$ds.substr(str_replace('\\', $ds, $class), 16).'.php';
  96. }
  97. }
  98. // See if the composer autoloader knows where the class is.
  99. if ($path === false && self::$composerAutoloader !== false) {
  100. $path = self::$composerAutoloader->findFile($class);
  101. }
  102. // See if the class is inside one of our alternate search paths.
  103. if ($path === false) {
  104. foreach (self::$searchPaths as $searchPath => $nsPrefix) {
  105. $className = $class;
  106. if ($nsPrefix !== '' && substr($class, 0, strlen($nsPrefix)) === $nsPrefix) {
  107. $className = substr($class, (strlen($nsPrefix) + 1));
  108. }
  109. $path = $searchPath.$ds.str_replace('\\', $ds, $className).'.php';
  110. if (is_file($path) === true) {
  111. break;
  112. }
  113. $path = false;
  114. }
  115. }
  116. if ($path !== false && is_file($path) === true) {
  117. self::loadFile($path);
  118. return true;
  119. }
  120. return false;
  121. }//end load()
  122. /**
  123. * Includes a file and tracks what class or interface was loaded as a result.
  124. *
  125. * @param string $path The path of the file to load.
  126. *
  127. * @return string The fully qualified name of the class in the loaded file.
  128. */
  129. public static function loadFile($path)
  130. {
  131. if (strpos(__DIR__, 'phar://') !== 0) {
  132. $path = realpath($path);
  133. if ($path === false) {
  134. return false;
  135. }
  136. }
  137. if (isset(self::$loadedClasses[$path]) === true) {
  138. return self::$loadedClasses[$path];
  139. }
  140. $classes = get_declared_classes();
  141. $interfaces = get_declared_interfaces();
  142. $traits = get_declared_traits();
  143. include $path;
  144. $className = null;
  145. $newClasses = array_reverse(array_diff(get_declared_classes(), $classes));
  146. foreach ($newClasses as $name) {
  147. if (isset(self::$loadedFiles[$name]) === false) {
  148. $className = $name;
  149. break;
  150. }
  151. }
  152. if ($className === null) {
  153. $newClasses = array_reverse(array_diff(get_declared_interfaces(), $interfaces));
  154. foreach ($newClasses as $name) {
  155. if (isset(self::$loadedFiles[$name]) === false) {
  156. $className = $name;
  157. break;
  158. }
  159. }
  160. }
  161. if ($className === null) {
  162. $newClasses = array_reverse(array_diff(get_declared_traits(), $traits));
  163. foreach ($newClasses as $name) {
  164. if (isset(self::$loadedFiles[$name]) === false) {
  165. $className = $name;
  166. break;
  167. }
  168. }
  169. }
  170. self::$loadedClasses[$path] = $className;
  171. self::$loadedFiles[$className] = $path;
  172. return self::$loadedClasses[$path];
  173. }//end loadFile()
  174. /**
  175. * Adds a directory to search during autoloading.
  176. *
  177. * @param string $path The path to the directory to search.
  178. * @param string $nsPrefix The namespace prefix used by files under this path.
  179. *
  180. * @return void
  181. */
  182. public static function addSearchPath($path, $nsPrefix='')
  183. {
  184. self::$searchPaths[$path] = rtrim(trim((string) $nsPrefix), '\\');
  185. }//end addSearchPath()
  186. /**
  187. * Retrieve the namespaces and paths registered by external standards.
  188. *
  189. * @return array
  190. */
  191. public static function getSearchPaths()
  192. {
  193. return self::$searchPaths;
  194. }//end getSearchPaths()
  195. /**
  196. * Gets the class name for the given file path.
  197. *
  198. * @param string $path The name of the file.
  199. *
  200. * @throws \Exception If the file path has not been loaded.
  201. * @return string
  202. */
  203. public static function getLoadedClassName($path)
  204. {
  205. if (isset(self::$loadedClasses[$path]) === false) {
  206. throw new \Exception("Cannot get class name for $path; file has not been included");
  207. }
  208. return self::$loadedClasses[$path];
  209. }//end getLoadedClassName()
  210. /**
  211. * Gets the file path for the given class name.
  212. *
  213. * @param string $class The name of the class.
  214. *
  215. * @throws \Exception If the class name has not been loaded
  216. * @return string
  217. */
  218. public static function getLoadedFileName($class)
  219. {
  220. if (isset(self::$loadedFiles[$class]) === false) {
  221. throw new \Exception("Cannot get file name for $class; class has not been included");
  222. }
  223. return self::$loadedFiles[$class];
  224. }//end getLoadedFileName()
  225. /**
  226. * Gets the mapping of file names to class names.
  227. *
  228. * @return array<string, string>
  229. */
  230. public static function getLoadedClasses()
  231. {
  232. return self::$loadedClasses;
  233. }//end getLoadedClasses()
  234. /**
  235. * Gets the mapping of class names to file names.
  236. *
  237. * @return array<string, string>
  238. */
  239. public static function getLoadedFiles()
  240. {
  241. return self::$loadedFiles;
  242. }//end getLoadedFiles()
  243. }//end class
  244. // Register the autoloader before any existing autoloaders to ensure
  245. // it gets a chance to hear about every autoload request, and record
  246. // the file and class name for it.
  247. spl_autoload_register(__NAMESPACE__.'\Autoload::load', true, true);
  248. }//end if