From 8355ef997893e744f089981f496ce041aed2854e Mon Sep 17 00:00:00 2001 From: Siad Ardroumli Date: Sat, 28 Dec 2013 01:27:06 +0100 Subject: [PATCH 1/2] Reduced filesize of phar --- src/phpDocumentor/PharCompiler.php | 44 ++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/phpDocumentor/PharCompiler.php b/src/phpDocumentor/PharCompiler.php index de689de9b8..36ec061116 100644 --- a/src/phpDocumentor/PharCompiler.php +++ b/src/phpDocumentor/PharCompiler.php @@ -114,6 +114,10 @@ protected function getFiles() 'output', 'behat', 'cilex/cilex/tests', + 'jms/metadata/tests', + 'jms/parser-lib/tests', + 'jms/serializer/tests', + 'monolog/monolog/tests', 'nikic/php-parser/doc', 'nikic/php-parser/test', 'nikic/php-parser/test_old', @@ -121,6 +125,7 @@ protected function getFiles() 'phpdocumentor/graphviz/tests', 'phpdocumentor/reflection-docblock/tests', 'pimple/pimple/tests', + 'psr/log/Psr/Log/Test', 'twig/twig/test', ) ); @@ -165,12 +170,45 @@ protected function addFilesToPharArchive(array $files, \Phar $phar) */ protected function addFileToPharArchive($file, \Phar $phar) { - $path = str_replace(__DIR__ . '/', '', $file); - $file_contents = file_get_contents($file); - $file_contents = str_replace('#!/usr/bin/env php', '', $file_contents); + $path = strtr(str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath()), '\\', '/'); + $handle = fopen($file, 'r'); + $file_contents = @fread($handle, filesize($file)); + fclose($handle); + + if ($path === 'bin/phpdoc' || $path === 'bin/phpdoc.php') { + $file_contents = str_replace('#!/usr/bin/env php', '', $file_contents); + } + + $file_contents = $this->minifyFile($file_contents); $phar->addFromString($path, $file_contents); } + /** + * Reduce the filesize of the PHAR removing whitespace and comments. + * + * @param string $fileContent + * + * @return string + */ + protected function minifyFile($fileContent) + { + $tokens = token_get_all($fileContent); + $cleanedCode = ""; + foreach ($tokens as $token) { + if (is_array($token)) { + if ($token[0] != T_COMMENT && $token[0] != T_DOC_COMMENT) { + $cleanedCode .= $token[1]; + } + } else { + $cleanedCode .= $token; + } + + } + + return $cleanedCode; + } + + /** * Adds the stubs for the CLI and Web interaction to the PHAR archive. * From d920ebe03048e36f697ae88cb215460673793550 Mon Sep 17 00:00:00 2001 From: Siad Ardroumli Date: Sat, 28 Dec 2013 23:37:17 +0100 Subject: [PATCH 2/2] Excluded templates --- src/phpDocumentor/PharCompiler.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/phpDocumentor/PharCompiler.php b/src/phpDocumentor/PharCompiler.php index 36ec061116..91763fa711 100644 --- a/src/phpDocumentor/PharCompiler.php +++ b/src/phpDocumentor/PharCompiler.php @@ -102,13 +102,14 @@ protected function initializePharArchive($pharFile) */ protected function getFiles() { - $files = array('LICENSE', 'README.md', 'VERSION'); - $finder = new Finder(); $iterator = $finder->files() + ->ignoreVCS(true) + ->ignoreDotFiles(true) ->in(array('bin', 'data', 'src', 'vendor')) ->notName('*.rst') ->notName('*.md') + ->size('> 0k') ->exclude( array( 'output', @@ -128,9 +129,10 @@ protected function getFiles() 'psr/log/Psr/Log/Test', 'twig/twig/test', ) - ); + ) + ->append(array('LICENSE', 'README.md', 'VERSION')); - return array_merge($files, iterator_to_array($iterator)); + return iterator_to_array($iterator); } /** @@ -149,7 +151,7 @@ protected function addFilesToPharArchive(array $files, \Phar $phar) foreach ($files as $file) { echo '.'; $counter++; - if ($counter % 70 == 0) { + if ($counter % 67 == 0) { echo ' [' . $counter . '/' . count($files) . ']' . PHP_EOL; } $this->addFileToPharArchive($file, $phar); @@ -172,14 +174,17 @@ protected function addFileToPharArchive($file, \Phar $phar) { $path = strtr(str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath()), '\\', '/'); $handle = fopen($file, 'r'); - $file_contents = @fread($handle, filesize($file)); + $file_contents = fread($handle, filesize($file)); fclose($handle); if ($path === 'bin/phpdoc' || $path === 'bin/phpdoc.php') { $file_contents = str_replace('#!/usr/bin/env php', '', $file_contents); } - $file_contents = $this->minifyFile($file_contents); + if (strpos($path, 'vendor') === 0 || strpos($path, 'src') === 0) { + $file_contents = $this->minifyFile($file_contents); + } + $phar->addFromString($path, $file_contents); }