ClassMapGenerator.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. $map[$class] = $filePath;
  71. }
  72. }
  73. return $map;
  74. }
  75. /**
  76. * Extract the classes in the given file
  77. *
  78. * @param string $path The file to check
  79. * @throws \RuntimeException
  80. * @return array The found classes
  81. */
  82. private static function findClasses($path)
  83. {
  84. $traits = version_compare(PHP_VERSION, '5.4', '<') ? '' : '|trait';
  85. try {
  86. $contents = php_strip_whitespace($path);
  87. } catch (\Exception $e) {
  88. throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e);
  89. }
  90. // return early if there is no chance of matching anything in this file
  91. if (!preg_match('{\b(?:class|interface'.$traits.')\s}i', $contents)) {
  92. return array();
  93. }
  94. // strip heredocs/nowdocs
  95. $contents = preg_replace('{<<<\'?(\w+)\'?(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\1(?=\r\n|\n|\r|;)}s', 'null', $contents);
  96. // strip strings
  97. $contents = preg_replace('{"[^"\\\\]*(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(\\\\.[^\'\\\\]*)*\'}s', 'null', $contents);
  98. // strip leading non-php code if needed
  99. if (substr($contents, 0, 2) !== '<?') {
  100. $contents = preg_replace('{^.+?<\?}s', '<?', $contents);
  101. }
  102. // strip non-php blocks in the file
  103. $contents = preg_replace('{\?>.+<\?}s', '?><?', $contents);
  104. // strip trailing non-php code if needed
  105. $pos = strrpos($contents, '?>');
  106. if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) {
  107. $contents = substr($contents, 0, $pos);
  108. }
  109. preg_match_all('{
  110. (?:
  111. \b(?<![\$:>])(?P<type>class|interface'.$traits.') \s+ (?P<name>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)
  112. | \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*[\{;]
  113. )
  114. }ix', $contents, $matches);
  115. $classes = array();
  116. $namespace = '';
  117. for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
  118. if (!empty($matches['ns'][$i])) {
  119. $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', $matches['nsname'][$i]) . '\\';
  120. } else {
  121. $classes[] = ltrim($namespace . $matches['name'][$i], '\\');
  122. }
  123. }
  124. return $classes;
  125. }
  126. }