ClassMapGenerator.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 $whitelist Regex that matches against the file path
  42. *
  43. * @return array A class map array
  44. *
  45. * @throws \RuntimeException When the path is neither an existing file nor directory
  46. */
  47. public static function createMap($path, $whitelist = null, IOInterface $io = null)
  48. {
  49. if (is_string($path)) {
  50. if (is_file($path)) {
  51. $path = array(new \SplFileInfo($path));
  52. } elseif (is_dir($path)) {
  53. $path = Finder::create()->files()->followLinks()->name('/\.(php|inc|hh)$/')->in($path);
  54. } else {
  55. throw new \RuntimeException(
  56. 'Could not scan for classes inside "'.$path.
  57. '" which does not appear to be a file nor a folder'
  58. );
  59. }
  60. }
  61. $map = array();
  62. foreach ($path as $file) {
  63. $filePath = $file->getRealPath();
  64. if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), array('php', 'inc', 'hh'))) {
  65. continue;
  66. }
  67. if ($whitelist && !preg_match($whitelist, strtr($filePath, '\\', '/'))) {
  68. continue;
  69. }
  70. $classes = self::findClasses($filePath);
  71. foreach ($classes as $class) {
  72. if (!isset($map[$class])) {
  73. $map[$class] = $filePath;
  74. } elseif ($io && $map[$class] !== $filePath) {
  75. $io->write(
  76. '<warning>Warning: Ambiguous class resolution, "'.$class.'"'.
  77. ' was found in both "'.$map[$class].'" and "'.$filePath.'", the first will be used.</warning>'
  78. );
  79. }
  80. }
  81. }
  82. return $map;
  83. }
  84. /**
  85. * Extract the classes in the given file
  86. *
  87. * @param string $path The file to check
  88. * @throws \RuntimeException
  89. * @return array The found classes
  90. */
  91. private static function findClasses($path)
  92. {
  93. $traits = version_compare(PHP_VERSION, '5.4', '<') ? '' : '|trait';
  94. try {
  95. $contents = php_strip_whitespace($path);
  96. } catch (\Exception $e) {
  97. throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e);
  98. }
  99. // return early if there is no chance of matching anything in this file
  100. if (!preg_match('{\b(?:class|interface'.$traits.')\s}i', $contents)) {
  101. return array();
  102. }
  103. // strip heredocs/nowdocs
  104. $contents = preg_replace('{<<<\'?(\w+)\'?(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\1(?=\r\n|\n|\r|;)}s', 'null', $contents);
  105. // strip strings
  106. $contents = preg_replace('{"[^"\\\\]*(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(\\\\.[^\'\\\\]*)*\'}s', 'null', $contents);
  107. // strip leading non-php code if needed
  108. if (substr($contents, 0, 2) !== '<?') {
  109. $contents = preg_replace('{^.+?<\?}s', '<?', $contents);
  110. }
  111. // strip non-php blocks in the file
  112. $contents = preg_replace('{\?>.+<\?}s', '?><?', $contents);
  113. // strip trailing non-php code if needed
  114. $pos = strrpos($contents, '?>');
  115. if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) {
  116. $contents = substr($contents, 0, $pos);
  117. }
  118. preg_match_all('{
  119. (?:
  120. \b(?<![\$:>])(?P<type>class|interface'.$traits.') \s+ (?P<name>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)
  121. | \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*[\{;]
  122. )
  123. }ix', $contents, $matches);
  124. $classes = array();
  125. $namespace = '';
  126. for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
  127. if (!empty($matches['ns'][$i])) {
  128. $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', $matches['nsname'][$i]) . '\\';
  129. } else {
  130. $classes[] = ltrim($namespace . $matches['name'][$i], '\\');
  131. }
  132. }
  133. return $classes;
  134. }
  135. }