📜 ⬆️ ⬇️

.phar - executable PHP archives

I want to devote this article to one interesting innovation in PHP 5.2 (since version 5.3 is included in the standard PHP package) - PHAR.
image
PHAR is a utility for creating executable archives in PHP, an analogue of JAR in Java.
PHAR allows you to pack a lot of files into one, with the result that your application can work with a whole library as with a single file.
PHAR can create, read, write and convert formats such as TAR, ZIP and, in fact, PHAR.
Access to files in the archive is carried out directly, without the need to unpack the archive, through PHP Stream Wrapper, that is, all the functions that support PHP Stream Wrapper work with the files from the archive.

Requirements


In order to work with the PHAR archives, you need PHP at least version 5.2 with the extensions PHAR, zlib and bzip2 (if you have PHP 5.3, then PHAR is already installed by default).
Security
By default, PHAR archives have only read access. If desired, you can set phar.readonly = 0 in php.ini.
In addition, PHAR archives can only be executed by the PHP interpreter.

Example

Create an archive with a text file:
$phar = new Phar('test.phar'); $phar[hello_habr.txt] = 'Hello Habr!'; 


And now we read its contents:
 echo file_get_contents('phar://test.phar/hello_habr.txt'); 

')

Stub file


A “stub” is a file that is read first when a PHAR archive is connected; this is a kind of Bootstrap. This file is interpreted only if the archive is fully connected. If the script uses only specific files from the archive, the stub file is not interpreted.
Let's see an example:
 //-   include_once('some_archive.phar'); //-    include_once('phar://some_archive.phar/somefile.php'); 


When creating a stub file, use the __HALT_COMPILER () call at the end of the file.
Access to the stub file can be obtained by calling the getStub () method, as in the example:

 $phar = new Phar('some_archive.phar'); $stub = $phar->getStub(); 


To set the default stub file, use the setDefaultStub () method:
 $phar = new Phar('some_archive.phar'); $phar->setDefaultStub('cli.php', 'web/index.php'); 


Creating archive content


IMPORTANT: set phar.readonly = 0 in php.ini

There are several ways to record content:


A few examples:
 $phar = new Phar('habr.phar'); $phar['index.php'] = file_get_contents('some/path/to/file.php'); //          (ArrayAccess) 

 $phar = new Phar('habr.phar'); $phar->addFile('http://habrahabr.ru', 'HabrHomePage.html'); $phar->addFromString('HabrHomePage', file_get_contents('http://habrahabr.ru')); $phar = new Phar('habr.phar'); $phar->addEmptyDir('temporary/'); $phar->buildFromDirectory('some_directory/'); 


 $phar = new Phar('habr'); $dir = 'somedir/'; $phar->buildFromIterator( new RecursiveDirectoryIterator($dir) ); 


Reading from the archive


As with recording, there are several ways:


Signature PHAR archive


The signature is used to validate the data.
PHAR supports four algorithms for generating signatures:


To set the signature, use the Phar :: setSignatureAlgorithm () method, which takes two parameters:


Code example:
 $phar=new Phar('habr.phar'); $phar->buildFromDirectory('habr/'); $signatures=Phar::getSupportedSignatures(); if (in_array(PHAR::SHA512,$signatures)) { $phar->setSignatureAlgorithm(PHAR::SHA512); } elseif (in_array(PHAR::SHA256,$signatures)) { $phar->setSignatureAlgorithm(PHAR::SHA256); } elseif (in_array(PHAR::SHA1,$signatures)) { $phar->setSignatureAlgorithm(PHAR::SHA1); } elseif (in_array(PHAR::MD5,$signatures)) { $phar->setSignatureAlgorithm(PHAR::MD5); } 


and for OpenSSL:
 $phar=new Phar('habr.phar'); $phar->buildFromDirectory('habr/'); $OSSLPrivateKey=openssl_get_privatekey(file_get_contents('private.pem')); $OSSLPKey=''; openssl_pkey_export($OSSLPrivateKey,$OSSLPKey); $phar->setSignatureAlgorithm(Phar::OPENSSL,$OSSLPKey); 


ZIP and TAR


PHAR supports reading ZIP and TAR archives. In this case, reading occurs as if it is a regular PHAR archive. However, it should be remembered that the length of the name should not exceed 255 bytes, including the path to the file, and also that for the archive to be executable, it must contain in its name '.phar' (for example, habr.phar. gz)
Archives are compressed using one of two algorithms: gzip or bzip2.
Please note that ZIP and TAR archives can be created even if phar.readonly = 1 in php.ini, but in this case can not contain a stub file or '.phar' in the name.
Archive Format Conversion
The archive conversion can be done in two ways:

1. Phar :: ConvertToData (), which takes three parameters: format (Phar :: TAR, Phar :: ZIP), compression (Phar :: NONE, Phar :: GZ, Phar :: BZ2) and extension (.tar, .tar.bz2, .tar.gz, .zip).

2. Phar :: ConvertToExecutable (), which accepts all the same parameters except the last one - extensions. There are many more options available that are still based on the PHAR, ZIP or TAR formats, respectively: .phar, .phar.gz, .phar.bz2, .phar.tar, .phar.tar.gz, .phar.tar.bz2 .phar.zip

Compression

As I wrote above, there are two compression methods available: Gzip and Bzip2, while compressing both the archive itself and the files inside it.
To compress the archive, use the compress () method, which takes two parameters - the type of compression and the file extension:
 $phar=new Phar('habr.phar'); $phar->buildFromDirectory('habr/'); if (Phar::canCompress(Phar::GZ)) { $phar->compress(Phar::GZ,'.phar.gz'); } else if (Phar::canCompress(Phar::BZ2)) { $phar->compress(Phar::BZ2,'.phar.bz2'); } 


For file compression - compressFiles () with one parameter - the type of compression:
 $phar=new Phar('habr.phar'); $phar->buildFromDirectory('habr/'); if (Phar::canCompress(Phar::GZ)) { $phar->compressFiles(Phar::GZ); } else if (Phar::canCompress(Phar::BZ2)) { $phar->compressFiles(Phar::BZ2); } 


Performance


When using APC PHAR performance increases up to 6 times.
Without caching - almost does not differ from the option without using PHAR.

Results


And so, to summarize:

Source: https://habr.com/ru/post/118269/


All Articles