Slide 1

Slide 1 text

Phar, The PHP .exe format Helgi Þormar Þorbjörnsson ZendCon, Santa Clara, 23th of Oct 2012

Slide 2

Slide 2 text

Co-founded Orchestra.io Work at EngineYard PEAR Developer From Iceland @h on Twitter Helgi

Slide 3

Slide 3 text

What is Phar?

Slide 4

Slide 4 text

Phar = PHP Archive

Slide 5

Slide 5 text

Similar to JAR ... But for PHP

Slide 6

Slide 6 text

PEAR: PHP_Archive ‣ Reference implementation ‣ User Land Code ‣ Less Powerful ‣ Works on older PHP ‣ Not maintained anymore

Slide 7

Slide 7 text

Phar PHP Extension Built in / default from 5.3 onwards More Powerful than PHP_Archive

Slide 8

Slide 8 text

Phar unravelled

Slide 9

Slide 9 text

Simple yet flexible File Format

Slide 10

Slide 10 text

Pack multiple files into one

Slide 11

Slide 11 text

Similar to Tar

Slide 12

Slide 12 text

The special sauce 1. Stub 2. Manifest 3. File Contents 4. Signature (optional)

Slide 13

Slide 13 text

Manifest List of Files File Permission File Compression Meta Data

Slide 14

Slide 14 text

Compression Whole Archive Per file gzip, bz2 and zip Available Compressions

Slide 15

Slide 15 text

Stub Piece of PHP code: Bootstraping Autoloader

Slide 16

Slide 16 text

Get all the code used in this talk and more! github.com/helgi/phar-talk

Slide 17

Slide 17 text

Going more practical

Slide 18

Slide 18 text

Normal Include include 'awesome.php';

Slide 19

Slide 19 text

Phar Include include 'phar://example2/project.phar/awesome.php';

Slide 20

Slide 20 text

Phar works via Streams

Slide 21

Slide 21 text

Streams Usage fopen / fwrite / fclose file_get_contents opendir / rmdir / mkdir anything that works with streams

Slide 22

Slide 22 text

header('Content-type: text/javascript'); echo file_get_contents('phar://example3/project.phar/js/zepto.js');

Slide 23

Slide 23 text

Does not extract to disk

Slide 24

Slide 24 text

Works on web and CLI

Slide 25

Slide 25 text

Can be ran without the extension

Slide 26

Slide 26 text

Creating a Phar

Slide 27

Slide 27 text

Phar::buildFromDirectory()

Slide 28

Slide 28 text

if (!Phar::canWrite()) { die("Read-only, php -d phar.readonly=0 web/create.php\n"); } $p = new Phar(__DIR__ . '/project.phar', 0, 'project.phar'); $p->buildFromDirectory('/path/to/project'); $stub = <<<'EOD' Stub Goes Here EOD; $p->setStub($stub);

Slide 29

Slide 29 text

Phar::buildFromIterator()

Slide 30

Slide 30 text

$phar = new Phar('project.phar', 0, 'project.phar'); $phar->buildFromIterator( new RecursiveIteratorIterator( new Phar('/path/to/anotherphar.phar') ), 'phar:///path/to/anotherphar.phar/path/to/project'); $stub = $phar->createDefaultStub('cli/index.php', 'index.php'); $phar->setStub($stub);

Slide 31

Slide 31 text

Bit of autoload magic

Slide 32

Slide 32 text

function __autoload ($load) { include "phar://" . __FILE__ . "/$load.php"; } __HALT_COMPILER();

Slide 33

Slide 33 text

a.php b.php

Slide 34

Slide 34 text

include __DIR__ . '/project.phar'; $a = new A;

Slide 35

Slide 35 text

Gotchas apc.enable_cli = On detect_unicode = On Produces ??? ??? ???

Slide 36

Slide 36 text

Why use Phar?

Slide 37

Slide 37 text

❖ Command line ❖ Web applications ❖ Setup tools ❖ Plugins ❖ Themes ❖ Libraries Why use Phar?

Slide 38

Slide 38 text

Pros and Cons

Slide 39

Slide 39 text

Pros Single download with all dependencies Run multiple versions in parallel Upgrades are easy No unpacking Security against modifications

Slide 40

Slide 40 text

Cons Incremental updates (no deltas) Upgrading is a manual process Web server may need changes Extending the application is harder README / INSTALL become hard to reach

Slide 41

Slide 41 text

Stubs

Slide 42

Slide 42 text

Bootstrap for phar

Slide 43

Slide 43 text

Smallest possible Stub

Slide 44

Slide 44 text

__HALT_COMPILER() is a minimum requirement

Slide 45

Slide 45 text

Not used when phar file is used via streams

Slide 46

Slide 46 text

Web Phar

Slide 47

Slide 47 text

Phar::interceptFileFuncs() Phar::mungServer() Phar::WebPhar

Slide 48

Slide 48 text

CLI Phar

Slide 49

Slide 49 text

No Phar::WebPhar() Simple file include Shebang for easy execution

Slide 50

Slide 50 text

Web and CLI Phar

Slide 51

Slide 51 text

CLI + Autoload

Slide 52

Slide 52 text

CLI + Autoload cli.php a.php b.php

Slide 53

Slide 53 text

Phar offers a default stub

Slide 54

Slide 54 text

Will work for most

Slide 55

Slide 55 text

Extracts to temp dir if Phar is not available

Slide 56

Slide 56 text

Performance

Slide 57

Slide 57 text

Works with APC

Slide 58

Slide 58 text

Works on files inside the archive

Slide 59

Slide 59 text

Remember, everything goes via PHP Extract static files to a directory and serve from there

Slide 60

Slide 60 text

Security

Slide 61

Slide 61 text

Phar disallows writing by default

Slide 62

Slide 62 text

Suhosin Enabled by default in Debian and Ubuntu

Slide 63

Slide 63 text

Signatures are used to check the integrity of the archive

Slide 64

Slide 64 text

Signature Hashes: MD5 SHA1 SHA256 SHA512 OpenSSL

Slide 65

Slide 65 text

Hashes By default executable Phar is signed SHA1 Tar / Zip are not unless specified

Slide 66

Slide 66 text

Extra Goodies!

Slide 67

Slide 67 text

Phar::mount() Mount external files, such as config into the phar archive

Slide 68

Slide 68 text

PharData Phar archives can be extracted to disk Can operate on non-phar gzip and tar extract and compress archives Like PDO for archives! :-)

Slide 69

Slide 69 text

Useful Tools phar-util github.com/koto/phar-util empir empir.sourceforge.net

Slide 70

Slide 70 text

Questions? @h [email protected] Please rate at joind.in/6880 github.com/helgi/phar-talk