打包phar文件过大的问题。

根据一个开源工具得到的灵感,使用流打包,并使用token_get_all移除了所用PHP文件的空白。现在打包出来只有93k了。谢谢关注。

我一个简单的文件,加上一个symfony的process包,打包出来竟有125M之巨,而composer那么多文件打包出来只有1.6M,百思不得其解。附上打包代码:

<?php

$pharFilename = 'deploy.phar';

if (file_exists($pharFilename)) {
    e('remove old file...');
    unlink($pharFilename);
}

$ignoreFiles = [
    'readme.md',
    'composer.json',
    'composer.lock',
    '.gitignore',
    basename(__FILE__),
];

e('putting files...');

try {
    $phar = new Phar(
        __DIR__.DIRECTORY_SEPARATOR.$pharFilename,
        FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME,
        $pharFilename
    );
} catch (UnexpectedValueException $e) {
    echo $e->getMessage(), PHP_EOL;
} catch (BadMethodCallException $e) {
    echo $e->getMessage(), PHP_EOL;
}

e('building...');
$phar->buildFromDirectory(__DIR__);

e('remove unused files...');
array_walk($ignoreFiles, function ($file) use ($phar) {
    $phar->delete($file);
});

e('set stub...');
$phar->setStub($phar->createDefaultStub('deploy.php', 'deploy.php'));

e('compress...');
$phar->compressFiles(Phar::BZ2);

e('build done.');

function e($string)
{
    echo $string, PHP_EOL;
}

打包phar文件过大的问题。 >> php

这个答案描述的挺清楚的:
http://www.goodpm.net/postreply/php/1010000007306359/打包phar文件过大的问题.html
原文地址:https://www.cnblogs.com/scrumme/p/8018600.html