[HttpKernel] fix public Kernel::stripComments()

This commit is contained in:
Victor Berchet 2012-12-13 17:56:02 +01:00
parent 7200703089
commit aab60e33a9
2 changed files with 51 additions and 6 deletions

View File

@ -730,17 +730,26 @@ abstract class Kernel implements KernelInterface, TerminableInterface
return $source;
}
$rawChunk = '';
$output = '';
foreach (token_get_all($source) as $token) {
$tokens = token_get_all($source);
for (reset($tokens); false !== $token = current($tokens); next($tokens)) {
if (is_string($token)) {
$output .= $token;
$rawChunk .= $token;
} elseif (T_START_HEREDOC === $token[0]) {
$output .= preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $rawChunk) . $token[1];
do {
$token = next($tokens);
$output .= $token[1];
} while ($token[0] !== T_END_HEREDOC);
$rawChunk = '';
} elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
$output .= $token[1];
$rawChunk .= $token[1];
}
}
// replace multiple new lines with a single newline
$output = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $output);
$output .= preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $rawChunk);
return $output;
}

View File

@ -223,9 +223,28 @@ class KernelTest extends \PHPUnit_Framework_TestCase
return;
}
$source = <<<EOF
$source = <<<'EOF'
<?php
$string = 'string should not be modified';
$heredoc = <<<HD
Heredoc should not be modified
HD;
$nowdoc = <<<'ND'
Nowdoc should not be modified
ND;
/**
* some class comments to strip
*/
@ -240,8 +259,25 @@ class TestClass
}
}
EOF;
$expected = <<<EOF
$expected = <<<'EOF'
<?php
$string = 'string should not be modified';
$heredoc =
<<<HD
Heredoc should not be modified
HD;
$nowdoc =
<<<'ND'
Nowdoc should not be modified
ND;
class TestClass
{
public function doStuff()