/lib/Jelix/External/ClassMapGenerator.php

https://github.com/gmarrot/jelix · PHP · 154 lines · 95 code · 18 blank · 41 comment · 21 complexity · 444135e725e7fdc9e8310d12f1d1e016 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is copied from Composer package, initially 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 Jelix\External;
  13. /**
  14. * ClassMapGenerator
  15. *
  16. * @author Gyula Sallai <salla016@gmail.com>
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. * @contributor Laurent Jouanneau <laurent@jelix.org>
  19. */
  20. class ClassMapGenerator
  21. {
  22. /**
  23. * Iterate over all files in the given directory searching for classes
  24. *
  25. * @param \Iterator|string $path The path to search in or an iterator
  26. * @param string $whitelist Regex that matches against the file path
  27. *
  28. * @return array A class map array
  29. *
  30. * @throws \RuntimeException When the path is neither an existing file nor directory
  31. */
  32. public static function createMap($path, $whitelist = null)
  33. {
  34. if (is_string($path)) {
  35. if (is_file($path)) {
  36. $path = array(new \SplFileInfo($path));
  37. } elseif (is_dir($path)) {
  38. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
  39. $path = array();
  40. foreach($iterator as $k=>$item) {
  41. $ext = substr($item->getFilename(),-4);
  42. if ($ext == '.php' || $ext == '.inc') {
  43. $path[] = $item;
  44. }
  45. }
  46. } else {
  47. throw new \RuntimeException(
  48. 'Could not scan for classes inside "'.$path.
  49. '" which does not appear to be a file nor a folder'
  50. );
  51. }
  52. }
  53. $map = array();
  54. foreach ($path as $file) {
  55. $filePath = $file->getRealPath();
  56. if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), array('php', 'inc', 'hh'))) {
  57. continue;
  58. }
  59. if ($whitelist && !preg_match($whitelist, strtr($filePath, '\\', '/'))) {
  60. continue;
  61. }
  62. $classes = self::findClasses($filePath);
  63. foreach ($classes as $class) {
  64. if (!isset($map[$class])) {
  65. $map[$class] = $filePath;
  66. } elseif ($map[$class] !== $filePath && !preg_match('{/(test|fixture|example)s?/}i', strtr($map[$class].' '.$filePath, '\\', '/'))) {
  67. trigger_error(
  68. '<warning>Warning: Ambiguous class resolution, "'.$class.'"'.
  69. ' was found in both "'.$map[$class].'" and "'.$filePath.'", the first will be used.</warning>',
  70. E_USER_WARNING
  71. );
  72. }
  73. }
  74. }
  75. return $map;
  76. }
  77. /**
  78. * Extract the classes in the given file
  79. *
  80. * @param string $path The file to check
  81. * @throws \RuntimeException
  82. * @return array The found classes
  83. */
  84. private static function findClasses($path)
  85. {
  86. $traits = version_compare(PHP_VERSION, '5.4', '<') ? '' : '|trait';
  87. try {
  88. $contents = php_strip_whitespace($path);
  89. } catch (\Exception $e) {
  90. throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e);
  91. }
  92. // return early if there is no chance of matching anything in this file
  93. if (!preg_match('{\b(?:class|interface'.$traits.')\s}i', $contents)) {
  94. return array();
  95. }
  96. // strip heredocs/nowdocs
  97. $contents = preg_replace('{<<<\'?(\w+)\'?(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\1(?=\r\n|\n|\r|;)}s', 'null', $contents);
  98. // strip strings
  99. $contents = preg_replace('{"[^"\\\\]*(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(\\\\.[^\'\\\\]*)*\'}s', 'null', $contents);
  100. // strip leading non-php code if needed
  101. if (substr($contents, 0, 2) !== '<?') {
  102. $contents = preg_replace('{^.+?<\?}s', '<?', $contents, 1, $replacements);
  103. if ($replacements === 0) {
  104. return array();
  105. }
  106. }
  107. // strip non-php blocks in the file
  108. $contents = preg_replace('{\?>.+<\?}s', '?><?', $contents);
  109. // strip trailing non-php code if needed
  110. $pos = strrpos($contents, '?>');
  111. if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) {
  112. $contents = substr($contents, 0, $pos);
  113. }
  114. preg_match_all('{
  115. (?:
  116. \b(?<![\$:>])(?P<type>class|interface'.$traits.') \s+ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:]*)
  117. | \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*[\{;]
  118. )
  119. }ix', $contents, $matches);
  120. $classes = array();
  121. $namespace = '';
  122. for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
  123. if (!empty($matches['ns'][$i])) {
  124. $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', $matches['nsname'][$i]) . '\\';
  125. } else {
  126. $name = $matches['name'][$i];
  127. if ($name[0] === ':') {
  128. // This is an XHP class, https://github.com/facebook/xhp
  129. $name = 'xhp'.substr(str_replace(array('-', ':'), array('_', '__'), $name), 1);
  130. }
  131. $classes[] = ltrim($namespace . $name, '\\');
  132. }
  133. }
  134. return $classes;
  135. }
  136. }