Improve stripping
parent
32adc8908d
commit
4b24b972a7
|
@ -104,7 +104,7 @@ class Compiler
|
||||||
|
|
||||||
$content = file_get_contents($file);
|
$content = file_get_contents($file);
|
||||||
if ($strip) {
|
if ($strip) {
|
||||||
$content = $this->stripComments($content);
|
$content = $this->stripWhitespace($content);
|
||||||
} elseif ('LICENSE' === basename($file)) {
|
} elseif ('LICENSE' === basename($file)) {
|
||||||
$content = "\n".$content."\n";
|
$content = "\n".$content."\n";
|
||||||
}
|
}
|
||||||
|
@ -122,12 +122,12 @@ class Compiler
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes comments from a PHP source string.
|
* Removes whitespace from a PHP source string while preserving line numbers.
|
||||||
*
|
*
|
||||||
* @param string $source A PHP string
|
* @param string $source A PHP string
|
||||||
* @return string The PHP string with the comments removed
|
* @return string The PHP string with the whitespace removed
|
||||||
*/
|
*/
|
||||||
private function stripComments($source)
|
private function stripWhitespace($source)
|
||||||
{
|
{
|
||||||
if (!function_exists('token_get_all')) {
|
if (!function_exists('token_get_all')) {
|
||||||
return $source;
|
return $source;
|
||||||
|
@ -139,6 +139,14 @@ class Compiler
|
||||||
$output .= $token;
|
$output .= $token;
|
||||||
} elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
|
} elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
|
||||||
$output .= str_repeat("\n", substr_count($token[1], "\n"));
|
$output .= str_repeat("\n", substr_count($token[1], "\n"));
|
||||||
|
} elseif (T_WHITESPACE === $token[0]) {
|
||||||
|
// reduce wide spaces
|
||||||
|
$whitespace = preg_replace('{[ \t]+}', ' ', $token[1]);
|
||||||
|
// normalize newlines to \n
|
||||||
|
$whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace);
|
||||||
|
// trim leading spaces
|
||||||
|
$whitespace = preg_replace('{\n +}', "\n", $whitespace);
|
||||||
|
$output .= $whitespace;
|
||||||
} else {
|
} else {
|
||||||
$output .= $token[1];
|
$output .= $token[1];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue