Creating a phar
Transcript of Creating a phar
● Software Architect at Linio
● All around nerd● Systems Administrator
for 7 years● @aramonc in all the
places
About Me
like a Java JAR, but for PHP
PHp ARchive
Available since PHP 5.3
● Composer● PHPUnit● Magento● Pear● phpDox● Phing
Used by ...
Common Uses ...
● Command Line Utilities● Libraries
● Whole Applications● Frameworks
● Creating Tar & Zip files
Composed of ...
● A Stub● A manifest describing the contents
● The file contents● A signature (optional, phar format only)
phar.readonly = Off
<?php
$baseDir = realpath(dirname(__DIR__));$saveLoc = $baseDir . “/build/backup.phar”;$alias = “backup”;$flags = FileSystemIterator::CURRENT_AS_FILEINFO |
FileSystemIterator::KEY_AS_PATHNAME;
$phar = new Phar($saveLoc, $flags, $alias);
The Compiler Script
<?php
$phar->startBuffering();
$autoload = $baseDir . '/vendor/autoload.php';$runner = $baseDir . '/bin/backup_runner.php';
$phar->addFile($autoload, '/vendor/autoload.php');$phar->addFile($runner, '/bin/backups.php');
The Compiler Script
<?php
/** @var SPLFileInfo $file */foreach($files as $file) {
$path = $file->getRealPath();
$path = str_replace($baseDir . ‘/’, '', $path);
$path = strtr($path, '\\', '/');
The Compiler Script
<?php
if($file->isDir()) { $phar->addEmptyDir('/' . $path);}
if($file->isFile() && !$file->isDir()) { $phar->addFile($file->getRealPath(), $path);}
}
The Compiler Script
<?php
$phar->buildFromDirectory($baseDir);
// OR
$phar->buildFromIterator($dirIterator);
The Compiler Script … an Alternative
<?php
$stub = file_get_contents($baseDir . '/bin/stub.php');$phar->setStub($stub);
$phar->stopBuffering();
The Compiler Script
#!/usr/bin/env php<?php
set_time_limit(3000);
Phar::mapPhar("backup");
require "phar://backup/bin/backups.php";
__HALT_COMPILER();
The Stub
$> php backup.phar
Using the PHAR
<?phpinclude 'phar://coollibrary.phar/internal/file.php';header('Content-type: image/jpeg');// phars can be accessed by full path or by aliasecho file_get_contents('phar:///fullpath/to/coollibrary.phar/images/wow.jpg');
$> phar help-list
add compress delete extract help
help-list info list meta-del meta-get
meta-set pack sign stub-get
stub-set tree version
Debugging
Creating a Tar or Zip file
● Can be done with phar.readonly = 1● Should not have a stub or alias● Should use tar or zip extensions
<?php
$tar = new Phar($saveLoc, $flags, $alias, Phar::TAR);
$tar->compress(Phar::GZ)
$zip = new Phar($saveLoc, $flags, $alias, Phar::ZIP);
$zip->compress(Phar::BZ2)
Creating a Tar or Zip file
The box Project
● https://github.com/box-project/● Framework for creating Phar files● Lots of utility functions & classes for the
compiler script● Not part of your project● Box 3 has been in development for over a
year
A word on performance
● APC does not support PHAR● Using the PHAR format is faster than Tar or
Zip● Avoid compression if you’re going to use it
on the web
Resources
● PHP.net PHAR Manual● Composer Compiler● WebPhar Tutorial● Working with .phar files● Iterators in PHP - by Jake Smith @jakefolio● Backup Project
Thank you!!