ClassMapGenerator.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. *
  50. * @throws \RuntimeException When the path is neither an existing file nor directory
  51. * @return array A class map array
  52. */
  53. public static function createMap($path, $blacklist = null, IOInterface $io = null, $namespace = null)
  54. {
  55. if (is_string($path)) {
  56. if (is_file($path)) {
  57. $path = array(new \SplFileInfo($path));
  58. } elseif (is_dir($path)) {
  59. $path = Finder::create()->files()->followLinks()->name('/\.(php|inc|hh)$/')->in($path);
  60. } else {
  61. throw new \RuntimeException(
  62. 'Could not scan for classes inside "'.$path.
  63. '" which does not appear to be a file nor a folder'
  64. );
  65. }
  66. }
  67. $map = array();
  68. $filesystem = new Filesystem();
  69. $cwd = realpath(getcwd());
  70. foreach ($path as $file) {
  71. $filePath = $file->getPathname();
  72. if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), array('php', 'inc', 'hh'))) {
  73. continue;
  74. }
  75. if (!$filesystem->isAbsolutePath($filePath)) {
  76. $filePath = $cwd . '/' . $filePath;
  77. $filePath = $filesystem->normalizePath($filePath);
  78. } else {
  79. $filePath = preg_replace('{[\\\\/]{2,}}', '/', $filePath);
  80. }
  81. // 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
  82. if ($blacklist && preg_match($blacklist, strtr(realpath($filePath), '\\', '/'))) {
  83. continue;
  84. }
  85. // check non-realpath of file for directories symlink in project dir
  86. if ($blacklist && preg_match($blacklist, strtr($filePath, '\\', '/'))) {
  87. continue;
  88. }
  89. $classes = self::findClasses($filePath);
  90. foreach ($classes as $class) {
  91. // skip classes not within the given namespace prefix
  92. if (null !== $namespace && 0 !== strpos($class, $namespace)) {
  93. continue;
  94. }
  95. if (!isset($map[$class])) {
  96. $map[$class] = $filePath;
  97. } elseif ($io && $map[$class] !== $filePath && !preg_match('{/(test|fixture|example|stub)s?/}i', strtr($map[$class].' '.$filePath, '\\', '/'))) {
  98. $io->writeError(
  99. '<warning>Warning: Ambiguous class resolution, "'.$class.'"'.
  100. ' was found in both "'.$map[$class].'" and "'.$filePath.'", the first will be used.</warning>'
  101. );
  102. }
  103. }
  104. }
  105. return $map;
  106. }
  107. /**
  108. * Extract the classes in the given file
  109. *
  110. * @param string $path The file to check
  111. * @throws \RuntimeException
  112. * @return array The found classes
  113. */
  114. private static function findClasses($path)
  115. {
  116. $extraTypes = PHP_VERSION_ID < 50400 ? '' : '|trait';
  117. if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>=')) {
  118. $extraTypes .= '|enum';
  119. }
  120. // Use @ here instead of Silencer to actively suppress 'unhelpful' output
  121. // @link https://github.com/composer/composer/pull/4886
  122. $contents = @php_strip_whitespace($path);
  123. if (!$contents) {
  124. if (!file_exists($path)) {
  125. $message = 'File at "%s" does not exist, check your classmap definitions';
  126. } elseif (!is_readable($path)) {
  127. $message = 'File at "%s" is not readable, check its permissions';
  128. } elseif ('' === trim(file_get_contents($path))) {
  129. // The input file was really empty and thus contains no classes
  130. return array();
  131. } else {
  132. $message = 'File at "%s" could not be parsed as PHP, it may be binary or corrupted';
  133. }
  134. $error = error_get_last();
  135. if (isset($error['message'])) {
  136. $message .= PHP_EOL . 'The following message may be helpful:' . PHP_EOL . $error['message'];
  137. }
  138. throw new \RuntimeException(sprintf($message, $path));
  139. }
  140. // return early if there is no chance of matching anything in this file
  141. if (!preg_match('{\b(?:class|interface'.$extraTypes.')\s}i', $contents)) {
  142. return array();
  143. }
  144. // strip heredocs/nowdocs
  145. $contents = preg_replace('{<<<[ \t]*([\'"]?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)(?:\s*)\\2(?=\s+|[;,.)])}s', 'null', $contents);
  146. // strip strings
  147. $contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents);
  148. // strip leading non-php code if needed
  149. if (substr($contents, 0, 2) !== '<?') {
  150. $contents = preg_replace('{^.+?<\?}s', '<?', $contents, 1, $replacements);
  151. if ($replacements === 0) {
  152. return array();
  153. }
  154. }
  155. // strip non-php blocks in the file
  156. $contents = preg_replace('{\?>(?:[^<]++|<(?!\?))*+<\?}s', '?><?', $contents);
  157. // strip trailing non-php code if needed
  158. $pos = strrpos($contents, '?>');
  159. if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) {
  160. $contents = substr($contents, 0, $pos);
  161. }
  162. // strip comments if short open tags are in the file
  163. if (preg_match('{(<\?)(?!(php|hh))}i', $contents)) {
  164. $contents = preg_replace('{//.* | /\*(?:[^*]++|\*(?!/))*\*/}x', '', $contents);
  165. }
  166. preg_match_all('{
  167. (?:
  168. \b(?<![\$:>])(?P<type>class|interface'.$extraTypes.') \s++ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+)
  169. | \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*+ [\{;]
  170. )
  171. }ix', $contents, $matches);
  172. $classes = array();
  173. $namespace = '';
  174. for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
  175. if (!empty($matches['ns'][$i])) {
  176. $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', $matches['nsname'][$i]) . '\\';
  177. } else {
  178. $name = $matches['name'][$i];
  179. // skip anon classes extending/implementing
  180. if ($name === 'extends' || $name === 'implements') {
  181. continue;
  182. }
  183. if ($name[0] === ':') {
  184. // This is an XHP class, https://github.com/facebook/xhp
  185. $name = 'xhp'.substr(str_replace(array('-', ':'), array('_', '__'), $name), 1);
  186. } elseif ($matches['type'][$i] === 'enum') {
  187. // In Hack, something like:
  188. // enum Foo: int { HERP = '123'; }
  189. // The regex above captures the colon, which isn't part of
  190. // the class name.
  191. $name = rtrim($name, ':');
  192. }
  193. $classes[] = ltrim($namespace . $name, '\\');
  194. }
  195. }
  196. return $classes;
  197. }
  198. }