Minimal PHP Phar stub

I am currently experimenting with an extension mechanism for my structure. Each module consists of at least one PHP file (defining one class) and an XSL stylesheet, but several other files could potentially be involved, so I immediately thought about using Phars.

Everything plays well together, but I noticed that if I did not use createDefaultStub() and instead created Phar, as in the following fragment, then the result will be a quarter of the size - and smaller than the compressed version.

 $phar = new Phar('Example.phar', 0, 'Example.phar'); $phar->buildFromDirectory(__DIR__ . '/src'); $phar->setStub('<?php __HALT_COMPILER();'); $phar->setSignatureAlgorithm(Phar::SHA256); $phar->compress(Phar::GZ); 

Example file size:

 8799 14 Dec 09:37 ExampleCog.phar (using createDefaultStub()) 2143 14 Dec 10:08 ExampleCog.phar (using __HALT_COMPILER()) 3373 14 Dec 10:08 ExampleCog.phar.gz (consistent with either method) 

Phar will simply be used to store the files associated with the module in the kit and will be included in the structure. Working offline will not make any sense in this context. I think my question is what I missed - if anything - using a minimal stub code? And why is the compressed version always the same size?

+6
source share
1 answer

I think my question is what I missed - if anything - using a minimal stub code?

In the file format documentation , the default stub is described as:

The default piece for phar phar archives contains approximately 7k code to extract the phar contents and execute them.

He then points to Phar :: createDefaultStub , which says:

This method provides a simple and easy way to create a stub that will run the boot file from the phar archive. In addition, different files may be specified for launching the phar archive from the command line, depending on the web server. The loader also calls Phar :: interceptFileFuncs () to simplify the union of the PHP application that accesses the file system. If the phar extension is missing, the bootloader stub will extract the phar archive to a temporary directory and then work with the files. The shutdown function erases temporary files on exit.

Emphasis has been added, as the reason why the default stub is so large. If you can assume that you will always be running PHP 5.3 or later, you probably won’t need a default stub and can stick to the minimum __HALT_COMPILER

And why is the compressed version always the same size?

Diving is again in the file format documentation, there is a comparison between archive formats , which explains that Phar performs archive compression for each file. You probably see similar compression sizes because gzip cannot compress the data further. This is an assumption.

+7
source

All Articles