Igor
Cheledinov

.phar

What is .phar

PHP Archive

  • Similar to Jar

All Phar archives contain

  • a stub
  • a manifest describing the contents
  • the file contents
  • [optional] a signature (phar file format only)

Why .phar

All in One

  • aggregate many php files
  • metadata and resources (text, images, etc.)
  • has signature
  • compression

Easy 

  • distribute

  • usage

Examples

  • composer.phar

  • phing.phar

  • drush.phar

  • phpunit.phar

How to use .phar

CLI

php coolapplication.phar

Library

<?php
include 'coollibrary.phar';
?>

Stream wrapper

<?php
include 'phar://coollibrary.phar/internal/file.php';
header('Content-type: image/jpeg');
// phars can be accessed by full path or by alias
echo file_get_contents('phar:///fullpath/to/coollibrary.phar/images/wow.jpg');
?>

How to create .phar

Phar::setStub

Phar::createDefaultStub

Phar::buildFromIterator

<?php
// create with alias "project.phar"
$phar = new Phar('project.phar', 0, 'project.phar');
$phar->buildFromIterator(
    new ArrayIterator(
     array(
        'internal/file.php' => dirname(__FILE__) . '/somefile.php',
        'another/file.jpg' => fopen('/path/to/bigfile.jpg', 'rb'),
     )));
$phar->setStub($phar->createDefaultStub('cli/index.php', 'www/index.php'));
?>

Phar::buildFromDirectory

<?php
// create with alias "project.phar"
$phar = new Phar('project.phar', 0, 'project.phar');
// add all files in the project
$phar->buildFromDirectory(dirname(__FILE__) . '/project');
$phar->setStub($phar->createDefaultStub('cli/index.php', 'www/index.php'));
?>
<?php
$phar2 = new Phar('project2.phar', 0, 'project2.phar');
// add all files in the project, only include php files
$phar2->buildFromDirectory(dirname(__FILE__) . '/project', '/\.php$/');
$phar2->setStub($phar->createDefaultStub('cli/index.php', 'www/index.php'));
?>

Phar::buildFromDirectory

Phar::createDefaultStub

<?php
try {
    $phar = new Phar('myphar.phar');
    $phar->setStub($phar->createDefaultStub('cli.php', 'web/index.php'));
} catch (Exception $e) {
    // handle errors
}
?>

Archive any app

  • cache
  • logs
  • tmp

Slim Framework

php composer.phar create-project slim/slim-skeleton project

<?php

// create with alias "project.phar"
$phar = new Phar('project.phar', 0, 'project.phar');
// add all files in the project
$phar->buildFromDirectory(dirname(__FILE__) . '/project');
$phar->setStub($phar->createDefaultStub('public/index.php'));

?>

php -d phar.readonly=0 compiler.php 

Run!

php -S localhost:8080 project.phar
PHP 7.3.8 Development Server started at Fri Aug 23 01:11:18 2019
Listening on http://localhost:8080
Document root is /Users/iche/projects/MY/test
Press Ctrl-C to quit.

Symfony

php -S localhost:8080 project.phar

Web .phar

Phar::webPhar

Phar::mungServer

Phar::createDefaultStub

Example

<?php
// creating the phar archive:
try {
    $phar = new Phar('myphar.phar');
    $phar['index.php'] = '<?php echo "Hello World"; ?>';
    $phar->setStub('<?php
Phar::mungServer(array(\'REQUEST_URI\'));
Phar::webPhar();
__HALT_COMPILER(); ?>');
} catch (Exception $e) {
    // handle error here
}

// GET /myphar.phar/index.php
?>

Works with

  • php-fpm
  • mod-php
  • cgi
  • fast-cgi
  • reactphp?

More about .pahr

Compression
 

// Create new file
$phar->compress(Phar::GZ,'.phar.gz'); // file.phar.gz

if (Phar::canCompress(Phar::GZ))
{
   $phar->compress(Phar::GZ,'.phar.gz');
}

// Compress files in archive
$phar->compressFiles(Phar::GZ); // same name

if (Phar::canCompress(Phar::GZ))
{
   $phar->compressFiles(Phar::GZ);
}

Signature

/**
 * Phar::MD5
 * Phar::SHA1
 * Phar::SHA256
 * Phar::SHA512
 * Phar::OPENSSL
 */

$signatures=Phar::getSupportedSignatures();
if (in_array(PHAR::SHA512,$signatures))
{
   $phar->setSignatureAlgorithm(PHAR::SHA512);
}

?

Links

Thank you

Made with Slides.com