Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Reduced filesize of phar #1101

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/phpDocumentor/PharCompiler.php
Expand Up @@ -114,13 +114,18 @@ 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',
'phpdocumentor/fileset/tests',
'phpdocumentor/graphviz/tests',
'phpdocumentor/reflection-docblock/tests',
'pimple/pimple/tests',
'psr/log/Psr/Log/Test',
'twig/twig/test',
)
);
Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing DocComments may be an issue as the template serializer uses annotations to serialize and deserialize the XML information

$cleanedCode .= $token[1];
}
} else {
$cleanedCode .= $token;
}

}

return $cleanedCode;
}


/**
* Adds the stubs for the CLI and Web interaction to the PHAR archive.
*
Expand Down