PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Composer/Autoload/ClassMapGenerator.php

http://github.com/composer/composer
PHP | 307 lines | 240 code | 21 blank | 46 comment | 25 complexity | 2056f6b4f28b3c5bbfffa586f3f845e5 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. /*
  12. * This file is copied from the Symfony package.
  13. *
  14. * (c) Fabien Potencier <fabien@symfony.com>
  15. */
  16. namespace Composer\Autoload;
  17. use Symfony\Component\Finder\Finder;
  18. use Composer\IO\IOInterface;
  19. use Composer\Util\Filesystem;
  20. /**
  21. * ClassMapGenerator
  22. *
  23. * @author Gyula Sallai <salla016@gmail.com>
  24. * @author Jordi Boggiano <j.boggiano@seld.be>
  25. */
  26. class ClassMapGenerator
  27. {
  28. /**
  29. * Generate a class map file
  30. *
  31. * @param \Traversable $dirs Directories or a single path to search in
  32. * @param string $file The name of the class map file
  33. */
  34. public static function dump($dirs, $file)
  35. {
  36. $maps = array();
  37. foreach ($dirs as $dir) {
  38. $maps = array_merge($maps, static::createMap($dir));
  39. }
  40. file_put_contents($file, sprintf('<?php return %s;', var_export($maps, true)));
  41. }
  42. /**
  43. * Iterate over all files in the given directory searching for classes
  44. *
  45. * @param \Iterator|string $path The path to search in or an iterator
  46. * @param string $blacklist Regex that matches against the file path that exclude from the classmap.
  47. * @param IOInterface $io IO object
  48. * @param string $namespace Optional namespace prefix to filter by
  49. * @param string $autoloadType psr-0|psr-4 Optional autoload standard to use mapping rules
  50. *
  51. * @throws \RuntimeException When the path is neither an existing file nor directory
  52. * @return array A class map array
  53. */
  54. public static function createMap($path, $blacklist = null, IOInterface $io = null, $namespace = null, $autoloadType = null, &$scannedFiles = array())
  55. {
  56. $basePath = $path;
  57. if (is_string($path)) {
  58. if (is_file($path)) {
  59. $path = array(new \SplFileInfo($path));
  60. } elseif (is_dir($path)) {
  61. $path = Finder::create()->files()->followLinks()->name('/\.(php|inc|hh)$/')->in($path);
  62. } else {
  63. throw new \RuntimeException(
  64. 'Could not scan for classes inside "'.$path.
  65. '" which does not appear to be a file nor a folder'
  66. );
  67. }
  68. } elseif (null !== $autoloadType) {
  69. throw new \RuntimeException('Path must be a string when specifying an autoload type');
  70. }
  71. $map = array();
  72. $filesystem = new Filesystem();
  73. $cwd = realpath(getcwd());
  74. foreach ($path as $file) {
  75. $filePath = $file->getPathname();
  76. if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), array('php', 'inc', 'hh'))) {
  77. continue;
  78. }
  79. if (!$filesystem->isAbsolutePath($filePath)) {
  80. $filePath = $cwd . '/' . $filePath;
  81. $filePath = $filesystem->normalizePath($filePath);
  82. } else {
  83. $filePath = preg_replace('{[\\\\/]{2,}}', '/', $filePath);
  84. }
  85. $realPath = realpath($filePath);
  86. // if a list of scanned files is given, avoid scanning twice the same file to save cycles and avoid generating warnings
  87. // in case a PSR-0/4 declaration follows another more specific one, or a classmap declaration, which covered this file already
  88. if (isset($scannedFiles[$realPath])) {
  89. continue;
  90. }
  91. // check the realpath of the file against the blacklist as the path might be a symlink and the blacklist is realpath'd so symlink are resolved
  92. if ($blacklist && preg_match($blacklist, strtr($realPath, '\\', '/'))) {
  93. continue;
  94. }
  95. // check non-realpath of file for directories symlink in project dir
  96. if ($blacklist && preg_match($blacklist, strtr($filePath, '\\', '/'))) {
  97. continue;
  98. }
  99. $classes = self::findClasses($filePath);
  100. if (null !== $autoloadType) {
  101. $classes = self::filterByNamespace($classes, $filePath, $namespace, $autoloadType, $basePath, $io);
  102. // if no valid class was found in the file then we do not mark it as scanned as it might still be matched by another rule later
  103. if ($classes) {
  104. $scannedFiles[$realPath] = true;
  105. }
  106. } else {
  107. // classmap autoload rules always collect all classes so for these we definitely do not want to scan again
  108. $scannedFiles[$realPath] = true;
  109. }
  110. foreach ($classes as $class) {
  111. // skip classes not within the given namespace prefix
  112. if (null === $autoloadType && null !== $namespace && '' !== $namespace && 0 !== strpos($class, $namespace)) {
  113. continue;
  114. }
  115. if (!isset($map[$class])) {
  116. $map[$class] = $filePath;
  117. } elseif ($io && $map[$class] !== $filePath && !preg_match('{/(test|fixture|example|stub)s?/}i', strtr($map[$class].' '.$filePath, '\\', '/'))) {
  118. $io->writeError(
  119. '<warning>Warning: Ambiguous class resolution, "'.$class.'"'.
  120. ' was found in both "'.$map[$class].'" and "'.$filePath.'", the first will be used.</warning>'
  121. );
  122. }
  123. }
  124. }
  125. return $map;
  126. }
  127. /**
  128. * Remove classes which could not have been loaded by namespace autoloaders
  129. *
  130. * @param array $classes found classes in given file
  131. * @param string $filePath current file
  132. * @param string $baseNamespace prefix of given autoload mapping
  133. * @param string $namespaceType psr-0|psr-4
  134. * @param string $basePath root directory of given autoload mapping
  135. * @param IOInterface $io IO object
  136. * @return array valid classes
  137. */
  138. private static function filterByNamespace($classes, $filePath, $baseNamespace, $namespaceType, $basePath, $io)
  139. {
  140. $validClasses = array();
  141. $rejectedClasses = array();
  142. $realSubPath = substr($filePath, strlen($basePath) + 1);
  143. $realSubPath = substr($realSubPath, 0, strrpos($realSubPath, '.'));
  144. foreach ($classes as $class) {
  145. // silently skip if ns doesn't have common root
  146. if ('' !== $baseNamespace && 0 !== strpos($class, $baseNamespace)) {
  147. continue;
  148. }
  149. // transform class name to file path and validate
  150. if ('psr-0' === $namespaceType) {
  151. $namespaceLength = strrpos($class, '\\');
  152. if (false !== $namespaceLength) {
  153. $namespace = substr($class, 0, $namespaceLength + 1);
  154. $className = substr($class, $namespaceLength + 1);
  155. $subPath = str_replace('\\', DIRECTORY_SEPARATOR, $namespace)
  156. . str_replace('_', DIRECTORY_SEPARATOR, $className);
  157. }
  158. else {
  159. $subPath = str_replace('_', DIRECTORY_SEPARATOR, $class);
  160. }
  161. } elseif ('psr-4' === $namespaceType) {
  162. $subNamespace = ('' !== $baseNamespace) ? substr($class, strlen($baseNamespace)) : $class;
  163. $subPath = str_replace('\\', DIRECTORY_SEPARATOR, $subNamespace);
  164. } else {
  165. throw new \RuntimeException("namespaceType must be psr-0 or psr-4, $namespaceType given");
  166. }
  167. if ($subPath === $realSubPath) {
  168. $validClasses[] = $class;
  169. } else {
  170. $rejectedClasses[] = $class;
  171. }
  172. }
  173. // warn only if no valid classes, else silently skip invalid
  174. if (empty($validClasses)) {
  175. foreach ($rejectedClasses as $class) {
  176. if ($io) {
  177. $io->writeError("<warning>Class $class located in ".preg_replace('{^'.preg_quote(getcwd()).'}', '.', $filePath, 1)." does not comply with $namespaceType autoloading standard. Skipping.</warning>");
  178. }
  179. }
  180. return array();
  181. }
  182. return $validClasses;
  183. }
  184. /**
  185. * Extract the classes in the given file
  186. *
  187. * @param string $path The file to check
  188. * @throws \RuntimeException
  189. * @return array The found classes
  190. */
  191. private static function findClasses($path)
  192. {
  193. $extraTypes = PHP_VERSION_ID < 50400 ? '' : '|trait';
  194. if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>=')) {
  195. $extraTypes .= '|enum';
  196. }
  197. // Use @ here instead of Silencer to actively suppress 'unhelpful' output
  198. // @link https://github.com/composer/composer/pull/4886
  199. $contents = @php_strip_whitespace($path);
  200. if (!$contents) {
  201. if (!file_exists($path)) {
  202. $message = 'File at "%s" does not exist, check your classmap definitions';
  203. } elseif (!is_readable($path)) {
  204. $message = 'File at "%s" is not readable, check its permissions';
  205. } elseif ('' === trim(file_get_contents($path))) {
  206. // The input file was really empty and thus contains no classes
  207. return array();
  208. } else {
  209. $message = 'File at "%s" could not be parsed as PHP, it may be binary or corrupted';
  210. }
  211. $error = error_get_last();
  212. if (isset($error['message'])) {
  213. $message .= PHP_EOL . 'The following message may be helpful:' . PHP_EOL . $error['message'];
  214. }
  215. throw new \RuntimeException(sprintf($message, $path));
  216. }
  217. // return early if there is no chance of matching anything in this file
  218. if (!preg_match('{\b(?:class|interface'.$extraTypes.')\s}i', $contents)) {
  219. return array();
  220. }
  221. // strip heredocs/nowdocs
  222. $contents = preg_replace('{<<<[ \t]*([\'"]?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)(?:\s*)\\2(?=\s+|[;,.)])}s', 'null', $contents);
  223. // strip strings
  224. $contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents);
  225. // strip leading non-php code if needed
  226. if (substr($contents, 0, 2) !== '<?') {
  227. $contents = preg_replace('{^.+?<\?}s', '<?', $contents, 1, $replacements);
  228. if ($replacements === 0) {
  229. return array();
  230. }
  231. }
  232. // strip non-php blocks in the file
  233. $contents = preg_replace('{\?>(?:[^<]++|<(?!\?))*+<\?}s', '?><?', $contents);
  234. // strip trailing non-php code if needed
  235. $pos = strrpos($contents, '?>');
  236. if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) {
  237. $contents = substr($contents, 0, $pos);
  238. }
  239. // strip comments if short open tags are in the file
  240. if (preg_match('{(<\?)(?!(php|hh))}i', $contents)) {
  241. $contents = preg_replace('{//.* | /\*(?:[^*]++|\*(?!/))*\*/}x', '', $contents);
  242. }
  243. preg_match_all('{
  244. (?:
  245. \b(?<![\$:>])(?P<type>class|interface'.$extraTypes.') \s++ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+)
  246. | \b(?<![\$:>])(?P<ns>namespace) (?P<nsname>\s++[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\s*+\\\\\s*+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+)? \s*+ [\{;]
  247. )
  248. }ix', $contents, $matches);
  249. $classes = array();
  250. $namespace = '';
  251. for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
  252. if (!empty($matches['ns'][$i])) {
  253. $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', $matches['nsname'][$i]) . '\\';
  254. } else {
  255. $name = $matches['name'][$i];
  256. // skip anon classes extending/implementing
  257. if ($name === 'extends' || $name === 'implements') {
  258. continue;
  259. }
  260. if ($name[0] === ':') {
  261. // This is an XHP class, https://github.com/facebook/xhp
  262. $name = 'xhp'.substr(str_replace(array('-', ':'), array('_', '__'), $name), 1);
  263. } elseif ($matches['type'][$i] === 'enum') {
  264. // In Hack, something like:
  265. // enum Foo: int { HERP = '123'; }
  266. // The regex above captures the colon, which isn't part of
  267. // the class name.
  268. $name = rtrim($name, ':');
  269. }
  270. $classes[] = ltrim($namespace . $name, '\\');
  271. }
  272. }
  273. return $classes;
  274. }
  275. }