Skip to content

Commit

Permalink
merged branch bamarni/classcollectionloader-globalnamespace (PR #5120)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Commits
-------

adeadfb fixed comment striping on global namespace classes

Discussion
----------

[ClassCollectionLoader] fixed comment striping on global namespace classes

previously #4792, I've removed the multiple blank lines removal not to break heredocs.

---------------------------------------------------------------------------

by stof at 2012-10-13T18:04:56Z

@fabpot is there anything left to merge this ?

---------------------------------------------------------------------------

by bamarni at 2012-10-14T11:47:23Z

I've added a space when faking a namespace, so that it stils works without the tokenizer (if #5747 gets merged)
  • Loading branch information
fabpot committed Oct 27, 2012
2 parents d7a02b5 + adeadfb commit 537760f
Showing 1 changed file with 5 additions and 36 deletions.
41 changes: 5 additions & 36 deletions src/Symfony/Component/ClassLoader/ClassCollectionLoader.php
Expand Up @@ -103,14 +103,14 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =

$c = preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($class->getFileName()));

// add namespace declaration for global code
// fakes namespace declaration for global code
if (!$class->inNamespace()) {
$c = "\nnamespace\n{\n".self::stripComments($c)."\n}\n";
} else {
$c = self::fixNamespaceDeclarations('<?php '.$c);
$c = preg_replace('/^\s*<\?php/', '', $c);
$c = "\nnamespace ;".$c;
}

$c = self::fixNamespaceDeclarations('<?php '.$c);
$c = preg_replace('/^\s*<\?php/', '', $c);

$content .= $c;
}

Expand Down Expand Up @@ -200,37 +200,6 @@ private static function writeCacheFile($file, $content)
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
}

/**
* Removes comments from a PHP source string.
*
* We don't use the PHP php_strip_whitespace() function
* as we want the content to be readable and well-formatted.
*
* @param string $source A PHP string
*
* @return string The PHP string with the comments removed
*/
private static function stripComments($source)
{
if (!function_exists('token_get_all')) {
return $source;
}

$output = '';
foreach (token_get_all($source) as $token) {
if (is_string($token)) {
$output .= $token;
} elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
$output .= $token[1];
}
}

// replace multiple new lines with a single newline
$output = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $output);

return $output;
}

/**
* Gets an ordered array of passed classes including all their dependencies.
*
Expand Down

0 comments on commit 537760f

Please sign in to comment.