Browse files

Merge branch 'classmap-optim'

  • Loading branch information...
2 parents 62d0ea7 + 4c3e780 commit 2368185774b45edce8dbb67e95b51c44bd253502 @Seldaek Seldaek committed Nov 11, 2012
Showing with 23 additions and 45 deletions.
  1. +23 −45 src/Composer/Autoload/ClassMapGenerator.php
View
68 src/Composer/Autoload/ClassMapGenerator.php
@@ -92,59 +92,37 @@ public static function createMap($dir, $whitelist = null)
*/
private static function findClasses($path)
{
- $contents = file_get_contents($path);
+ $contents = php_strip_whitespace($path);
+ $traits = version_compare(PHP_VERSION, '5.4', '<') ? '' : '|trait';
+
try {
- if (!preg_match('{\b(?:class|interface|trait)\b}i', $contents)) {
+ if (!preg_match('{\b(?:class|interface'.$traits.')\b}i', $contents)) {
return array();
}
- $tokens = token_get_all($contents);
- } catch (\Exception $e) {
- throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e);
- }
- $T_TRAIT = version_compare(PHP_VERSION, '5.4', '<') ? -1 : T_TRAIT;
- $classes = array();
+ // strip heredocs/nowdocs
+ $contents = preg_replace('{<<<\'?(\w+)\'?(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\1(?=\r\n|\n|\r|;)}s', 'null', $contents);
+ // strip strings
+ $contents = preg_replace('{"[^"\\\\]*(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(\\\\.[^\'\\\\]*)*\'}', 'null', $contents);
- $namespace = '';
- for ($i = 0, $max = count($tokens); $i < $max; $i++) {
- $token = $tokens[$i];
+ preg_match_all('{(?:\b(?<![\$:>])(?<type>class|interface'.$traits.')\s+(?<name>\S+)|\b(?<![\$:>])(?<ns>namespace)(?<nsname>\s+[^\s;{}\\\\]+(?:\s*\\\\\s*[^\s;{}\\\\]+)*|\s*\{))}i', $contents, $matches);
+ $classes = array();
- if (is_string($token)) {
- continue;
- }
+ $namespace = '';
- $class = '';
-
- switch ($token[0]) {
- case T_NAMESPACE:
- $namespace = '';
- // If there is a namespace, extract it
- while (($t = $tokens[++$i]) && is_array($t)) {
- if (in_array($t[0], array(T_STRING, T_NS_SEPARATOR))) {
- $namespace .= $t[1];
- }
- }
- $namespace .= '\\';
- break;
- case T_CLASS:
- case T_INTERFACE:
- case $T_TRAIT:
- // Find the classname
- while (($t = $tokens[++$i]) && is_array($t)) {
- if (T_STRING === $t[0]) {
- $class .= $t[1];
- } elseif ($class !== '' && T_WHITESPACE == $t[0]) {
- break;
- }
- }
-
- $classes[] = ltrim($namespace . $class, '\\');
- break;
- default:
- break;
+ for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
+ $name = $matches['name'][$i];
+
+ if (!empty($matches['ns'][$i])) {
+ $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', $matches['nsname'][$i]) . '\\';
+ } else {
+ $classes[] = ltrim($namespace . $matches['name'][$i], '\\');
+ }
}
- }
- return $classes;
+ return $classes;
+ } catch (\Exception $e) {
+ throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e);
+ }
}
}

0 comments on commit 2368185

Please sign in to comment.