Using Phar Archives: IntroductionPhar archives are similar in concept to Java JAR archives, but are tailored to the needs and to the flexibility of PHP applications. A Phar archive is used to distribute a complete PHP application or library in a single file. A Phar archive application is used exactly like any other PHP application: php coolapplication.phar Using a Phar archive library is identical to using any other PHP library:
The
The Phar class implements advanced functionality for accessing files and for creating phar archives. The Phar class is explained in detail here.
In addition, verification of phar file contents can be done using any of the supported symmetric hash algorithms (MD5, SHA1, SHA256 and SHA512 if ext/hash is enabled) and using asymmetric public/private key signing using OpenSSL. To take advantage of OpenSSL signing, you need to generate a public/private key pair, and use the private key to set the signature using Phar::setSignatureAlgorithm. In addition, the public key as extracted using this code:
/path/to/my.phar , the public key must be saved
as /path/to/my.phar.pubkey , or phar will be unable to verify
the OpenSSL signature.
The Phar class also provides 3 static methods, Phar::webPhar, Phar::mungServer and Phar::interceptFileFuncs that are crucial to packaging up PHP applications designed for usage on regular filesystems and for web-based applications. Phar::webPhar implements a front controller that routes HTTP calls to the correct location within the phar archive. Phar::mungServer is used to modify the values of the $_SERVER array to trick applications that process these values. Phar::interceptFileFuncs instructs Phar to intercept calls to fopen, file_get_contents, opendir, and all of the stat-based functions (file_exists, is_readable and so on) and route all relative paths to locations within the phar archive.
As an example, packaging up a release of the popular phpMyAdmin application for use as a phar archive requires
only this simple script and then
|