1
0
Fork 0

Fix line numbers in phars

pull/447/merge
Jordi Boggiano 2012-03-14 15:23:11 +01:00
parent e50fbf378b
commit 32adc8908d
1 changed files with 29 additions and 4 deletions

View File

@ -102,12 +102,11 @@ class Compiler
{
$path = str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath());
$content = file_get_contents($file);
if ($strip) {
$content = php_strip_whitespace($file);
$content = $this->stripComments($content);
} elseif ('LICENSE' === basename($file)) {
$content = "\n".file_get_contents($file)."\n";
} else {
$content = file_get_contents($file);
$content = "\n".$content."\n";
}
$content = str_replace('@package_version@', $this->version, $content);
@ -122,6 +121,32 @@ class Compiler
$phar->addFromString('bin/composer', $content);
}
/**
* Removes comments from a PHP source string.
*
* @param string $source A PHP string
* @return string The PHP string with the comments removed
*/
private 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;
}
private function getStub()
{
return <<<'EOF'