Commit 554425d7 authored by Igor Wiedler's avatar Igor Wiedler

maintain line numbers when minifying php for the phar

parent 20eeadbc
...@@ -80,7 +80,7 @@ class Compiler ...@@ -80,7 +80,7 @@ class Compiler
$path = str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath()); $path = str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath());
$content = file_get_contents($file); $content = file_get_contents($file);
if ($strip) { if ($strip) {
$content = Kernel::stripComments($content); $content = self::stripComments($content);
} }
$content = str_replace('@package_version@', $this->version, $content); $content = str_replace('@package_version@', $this->version, $content);
...@@ -138,4 +138,33 @@ if ('cli' === php_sapi_name() && basename(__FILE__) === basename($_SERVER['argv' ...@@ -138,4 +138,33 @@ if ('cli' === php_sapi_name() && basename(__FILE__) === basename($_SERVER['argv'
__HALT_COMPILER(); __HALT_COMPILER();
EOF; EOF;
} }
/**
* Removes comments from a PHP source string.
*
* Based on Kernel::stripComments(), but keeps line numbers intact.
*
* @param string $source A PHP string
*
* @return string The PHP string with the comments removed
*/
static public 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 .= str_repeat("\n", substr_count($token[1], "\n"));
} else {
$output .= $token[1];
}
}
return $output;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment