ClassMapGenerator.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /*
  3. * This file is copied from the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. *
  10. * @license MIT
  11. */
  12. namespace Composer\Autoload;
  13. use Symfony\Component\Finder\Finder;
  14. use Composer\IO\IOInterface;
  15. /**
  16. * ClassMapGenerator
  17. *
  18. * @author Gyula Sallai <salla016@gmail.com>
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class ClassMapGenerator
  22. {
  23. /**
  24. * Generate a class map file
  25. *
  26. * @param \Traversable $dirs Directories or a single path to search in
  27. * @param string $file The name of the class map file
  28. */
  29. public static function dump($dirs, $file)
  30. {
  31. $maps = array();
  32. foreach ($dirs as $dir) {
  33. $maps = array_merge($maps, static::createMap($dir));
  34. }
  35. file_put_contents($file, sprintf('<?php return %s;', var_export($maps, true)));
  36. }
  37. /**
  38. * Iterate over all files in the given directory searching for classes
  39. *
  40. * @param \Iterator|string $path The path to search in or an iterator
  41. * @param string $blacklist Regex that matches against the file path that exclude from the classmap.
  42. * @param IOInterface $io IO object
  43. * @param string $namespace Optional namespace prefix to filter by
  44. *
  45. * @return array A class map array
  46. *
  47. * @throws \RuntimeException When the path is neither an existing file nor directory
  48. */
  49. public static function createMap($path, $blacklist = '', IOInterface $io = null, $namespace = null)
  50. {
  51. if (is_string($path)) {
  52. if (is_file($path)) {
  53. $path = array(new \SplFileInfo($path));
  54. } elseif (is_dir($path)) {
  55. $path = Finder::create()->files()->followLinks()->name('/\.(php|inc|hh)$/')->in($path);
  56. } else {
  57. throw new \RuntimeException(
  58. 'Could not scan for classes inside "'.$path.
  59. '" which does not appear to be a file nor a folder'
  60. );
  61. }
  62. }
  63. $map = array();
  64. foreach ($path as $file) {
  65. $filePath = $file->getRealPath();
  66. if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), array('php', 'inc', 'hh'))) {
  67. continue;
  68. }
  69. if ($blacklist && preg_match($blacklist, strtr($filePath, '\\', '/'))) {
  70. continue;
  71. }
  72. $classes = self::findClasses($filePath);
  73. foreach ($classes as $class) {
  74. // skip classes not within the given namespace prefix
  75. if (null !== $namespace && 0 !== strpos($class, $namespace)) {
  76. continue;
  77. }
  78. if (!isset($map[$class])) {
  79. $map[$class] = $filePath;
  80. } elseif ($io && $map[$class] !== $filePath && !preg_match('{/(test|fixture|example)s?/}i', strtr($map[$class].' '.$filePath, '\\', '/'))) {
  81. $io->writeError(
  82. '<warning>Warning: Ambiguous class resolution, "'.$class.'"'.
  83. ' was found in both "'.$map[$class].'" and "'.$filePath.'", the first will be used.</warning>'
  84. );
  85. }
  86. }
  87. }
  88. return $map;
  89. }
  90. /**
  91. * Extract the classes in the given file
  92. *
  93. * @param string $path The file to check
  94. * @throws \RuntimeException
  95. * @return array The found classes
  96. */
  97. private static function findClasses($path)
  98. {
  99. $extraTypes = version_compare(PHP_VERSION, '5.4', '<') ? '' : '|trait';
  100. if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>=')) {
  101. $extraTypes .= '|enum';
  102. }
  103. try {
  104. $contents = @php_strip_whitespace($path);
  105. if (!$contents) {
  106. if (!file_exists($path)) {
  107. throw new \Exception('File does not exist');
  108. }
  109. if (!is_readable($path)) {
  110. throw new \Exception('File is not readable');
  111. }
  112. }
  113. } catch (\Exception $e) {
  114. throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e);
  115. }
  116. // return early if there is no chance of matching anything in this file
  117. if (!preg_match('{\b(?:class|interface'.$extraTypes.')\s}i', $contents)) {
  118. return array();
  119. }
  120. // strip heredocs/nowdocs
  121. $contents = preg_replace('{<<<\s*(\'?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\2(?=\r\n|\n|\r|;)}s', 'null', $contents);
  122. // strip strings
  123. $contents = preg_replace('{"[^"\\\\]*(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(\\\\.[^\'\\\\]*)*\'}s', 'null', $contents);
  124. // strip leading non-php code if needed
  125. if (substr($contents, 0, 2) !== '<?') {
  126. $contents = preg_replace('{^.+?<\?}s', '<?', $contents, 1, $replacements);
  127. if ($replacements === 0) {
  128. return array();
  129. }
  130. }
  131. // strip non-php blocks in the file
  132. $contents = preg_replace('{\?>.+<\?}s', '?><?', $contents);
  133. // strip trailing non-php code if needed
  134. $pos = strrpos($contents, '?>');
  135. if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) {
  136. $contents = substr($contents, 0, $pos);
  137. }
  138. preg_match_all('{
  139. (?:
  140. \b(?<![\$:>])(?P<type>class|interface'.$extraTypes.') \s+ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*)
  141. | \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*[\{;]
  142. )
  143. }ix', $contents, $matches);
  144. $classes = array();
  145. $namespace = '';
  146. for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
  147. if (!empty($matches['ns'][$i])) {
  148. $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', $matches['nsname'][$i]) . '\\';
  149. } else {
  150. $name = $matches['name'][$i];
  151. if ($name[0] === ':') {
  152. // This is an XHP class, https://github.com/facebook/xhp
  153. $name = 'xhp'.substr(str_replace(array('-', ':'), array('_', '__'), $name), 1);
  154. } else if ($matches['type'][$i] === 'enum') {
  155. // In Hack, something like:
  156. // enum Foo: int { HERP = '123'; }
  157. // The regex above captures the colon, which isn't part of
  158. // the class name.
  159. $name = rtrim($name, ':');
  160. }
  161. $classes[] = ltrim($namespace . $name, '\\');
  162. }
  163. }
  164. return $classes;
  165. }
  166. }