Creating a phar

23
Creating a PHAR for Greater Good! http://bit.ly/1jnCGcu

Transcript of Creating a phar

Page 1: Creating a phar

Creating a PHARfor Greater Good!

http://bit.ly/1jnCGcu

Page 2: Creating a phar

● Software Architect at Linio

● All around nerd● Systems Administrator

for 7 years● @aramonc in all the

places

About Me

Page 3: Creating a phar

like a Java JAR, but for PHP

PHp ARchive

Page 4: Creating a phar

Available since PHP 5.3

Page 5: Creating a phar

● Composer● PHPUnit● Magento● Pear● phpDox● Phing

Used by ...

Page 6: Creating a phar

Common Uses ...

● Command Line Utilities● Libraries

● Whole Applications● Frameworks

● Creating Tar & Zip files

Page 7: Creating a phar

Composed of ...

● A Stub● A manifest describing the contents

● The file contents● A signature (optional, phar format only)

Page 8: Creating a phar

phar.readonly = Off

Page 9: Creating a phar

<?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

Page 10: Creating a phar

<?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

Page 11: Creating a phar

<?php

/** @var SPLFileInfo $file */foreach($files as $file) {

$path = $file->getRealPath();

$path = str_replace($baseDir . ‘/’, '', $path);

$path = strtr($path, '\\', '/');

The Compiler Script

Page 12: Creating a phar

<?php

if($file->isDir()) { $phar->addEmptyDir('/' . $path);}

if($file->isFile() && !$file->isDir()) { $phar->addFile($file->getRealPath(), $path);}

}

The Compiler Script

Page 13: Creating a phar

<?php

$phar->buildFromDirectory($baseDir);

// OR

$phar->buildFromIterator($dirIterator);

The Compiler Script … an Alternative

Page 14: Creating a phar

<?php

$stub = file_get_contents($baseDir . '/bin/stub.php');$phar->setStub($stub);

$phar->stopBuffering();

The Compiler Script

Page 15: Creating a phar

#!/usr/bin/env php<?php

set_time_limit(3000);

Phar::mapPhar("backup");

require "phar://backup/bin/backups.php";

__HALT_COMPILER();

The Stub

Page 16: Creating a phar

$> 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');

Page 17: Creating a phar

$> 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

Page 18: Creating a phar

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

Page 19: Creating a phar

<?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

Page 20: Creating a phar

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

Page 21: Creating a phar

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

Page 23: Creating a phar

Thank you!!