Commit 19b7503d authored by Fabien Potencier's avatar Fabien Potencier

merged branch Seldaek/patch-1 (PR #281)

Commits
-------

570f475d Improve phar compression

Discussion
----------

Improve phar compression

Just did those changes for composer, with this patch added I only get a 4% size increase from the fully stripped version. Without I had a 33% size increase by adding the "stripComments" method from Silex which kinda sucked. As far as I can tell it doesn't break anything.
parents b35806a7 570f475d
...@@ -87,7 +87,7 @@ class Compiler ...@@ -87,7 +87,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 = self::stripComments($content); $content = self::stripWhitespace($content);
} }
$content = str_replace('@package_version@', $this->version, $content); $content = str_replace('@package_version@', $this->version, $content);
...@@ -147,15 +147,15 @@ EOF; ...@@ -147,15 +147,15 @@ EOF;
} }
/** /**
* Removes comments from a PHP source string. * Removes whitespace from a PHP source string while preserving line numbers.
* *
* Based on Kernel::stripComments(), but keeps line numbers intact. * Based on Kernel::stripComments(), but keeps line numbers intact.
* *
* @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
*/ */
static public function stripComments($source) static public function stripWhitespace($source)
{ {
if (!function_exists('token_get_all')) { if (!function_exists('token_get_all')) {
return $source; return $source;
...@@ -167,6 +167,14 @@ EOF; ...@@ -167,6 +167,14 @@ EOF;
$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];
} }
......
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