Home  >  Article  >  Backend Development  >  The problem of packaging phar files that are too large.

The problem of packaging phar files that are too large.

WBOY
WBOYOriginal
2016-12-01 01:27:311082browse

Based on inspiration from an open source tool, stream packaging is used, and token_get_all is used to remove the blank spaces in the PHP files used. Now the packaged price is only 93k. Thank you for your attention.

I have a simple file, plus a symfony process package, and the packaged size is 125M, while the composer packaged so many files is only 1.6M, which is puzzling. Attached is the packaging code:

<code><?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;
}
</code>

Reply content:

Based on inspiration from an open source tool, stream packaging is used, and token_get_all is used to remove the blank spaces in the PHP files used. Now the packaged price is only 93k. Thank you for your attention.

I have a simple file, plus a symfony process package, and the packaged size is 125M, while the composer packaged so many files is only 1.6M, which is puzzling. Attached is the packaging code:

<code><?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;
}
</code>

I think you can convert the format to zip; then view the archive content through compression software; this way you can intuitively see the size of each component in the archive;

<code class="php">$phar->convertToExecutable(Phar::ZIP);</code>

Hope it can help you

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn