ClassMapGenerator.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /**
  15. * ClassMapGenerator
  16. *
  17. * @author Gyula Sallai <salla016@gmail.com>
  18. */
  19. class ClassMapGenerator
  20. {
  21. /**
  22. * Generate a class map file
  23. *
  24. * @param \Traversable $dirs Directories or a single path to search in
  25. * @param string $file The name of the class map file
  26. */
  27. public static function dump($dirs, $file)
  28. {
  29. $maps = array();
  30. foreach ($dirs as $dir) {
  31. $maps = array_merge($maps, static::createMap($dir));
  32. }
  33. file_put_contents($file, sprintf('<?php return %s;', var_export($maps, true)));
  34. }
  35. /**
  36. * Iterate over all files in the given directory searching for classes
  37. *
  38. * @param \Iterator|string $path The path to search in or an iterator
  39. * @param string $whitelist Regex that matches against the file path
  40. *
  41. * @return array A class map array
  42. *
  43. * @throws \RuntimeException When the path is neither an existing file nor directory
  44. */
  45. public static function createMap($path, $whitelist = null)
  46. {
  47. if (is_string($path)) {
  48. if (is_file($path)) {
  49. $path = array(new \SplFileInfo($path));
  50. } elseif (is_dir($path)) {
  51. $path = Finder::create()->files()->followLinks()->name('/\.(php|inc)$/')->in($path);
  52. } else {
  53. throw new \RuntimeException(
  54. 'Could not scan for classes inside "'.$path.
  55. '" which does not appear to be a file nor a folder'
  56. );
  57. }
  58. }
  59. $map = array();
  60. foreach ($path as $file) {
  61. $filePath = $file->getRealPath();
  62. if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), array('php', 'inc'))) {
  63. continue;
  64. }
  65. if ($whitelist && !preg_match($whitelist, strtr($filePath, '\\', '/'))) {
  66. continue;
  67. }
  68. $classes = self::findClasses($filePath);
  69. foreach ($classes as $class) {
  70. if (array_key_exists($class, $map)) {
  71. throw new \RuntimeException('Ambiguous reference to class "'.$class.'" was found.');
  72. }
  73. $map[$class] = $filePath;
  74. }
  75. }
  76. return $map;
  77. }
  78. /**
  79. * Extract the classes in the given file
  80. *
  81. * @param string $path The file to check
  82. * @throws \RuntimeException
  83. * @return array The found classes
  84. */
  85. private static function findClasses($path)
  86. {
  87. $traits = version_compare(PHP_VERSION, '5.4', '<') ? '' : '|trait';
  88. try {
  89. $contents = php_strip_whitespace($path);
  90. } catch (\Exception $e) {
  91. throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e);
  92. }
  93. // return early if there is no chance of matching anything in this file
  94. if (!preg_match('{\b(?:class|interface'.$traits.')\s}i', $contents)) {
  95. return array();
  96. }
  97. // strip heredocs/nowdocs
  98. $contents = preg_replace('{<<<\'?(\w+)\'?(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\1(?=\r\n|\n|\r|;)}s', 'null', $contents);
  99. // strip strings
  100. $contents = preg_replace('{"[^"\\\\]*(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(\\\\.[^\'\\\\]*)*\'}s', 'null', $contents);
  101. // strip leading non-php code if needed
  102. if (substr($contents, 0, 2) !== '<?') {
  103. $contents = preg_replace('{^.+?<\?}s', '<?', $contents);
  104. }
  105. // strip non-php blocks in the file
  106. $contents = preg_replace('{\?>.+<\?}s', '?><?', $contents);
  107. // strip trailing non-php code if needed
  108. $pos = strrpos($contents, '?>');
  109. if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) {
  110. $contents = substr($contents, 0, $pos);
  111. }
  112. preg_match_all('{
  113. (?:
  114. \b(?<![\$:>])(?P<type>class|interface'.$traits.') \s+ (?P<name>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)
  115. | \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*[\{;]
  116. )
  117. }ix', $contents, $matches);
  118. $classes = array();
  119. $namespace = '';
  120. for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
  121. if (!empty($matches['ns'][$i])) {
  122. $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', $matches['nsname'][$i]) . '\\';
  123. } else {
  124. $classes[] = ltrim($namespace . $matches['name'][$i], '\\');
  125. }
  126. }
  127. return $classes;
  128. }
  129. }