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

The problem of packaging phar files that are too large.

WBOY
Release: 2023-03-02 11:24:02
Original
1109 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

Hope it can help you

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template