Slide 1

Slide 1 text

Phar, The PHP .exe format Helgi Þormar Þorbjörnsson PHPDay, Verona, 18th of May 2012

Slide 2

Slide 2 text

Who am I?

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

What is Phar?

Slide 5

Slide 5 text

Phar = PHP Archive

Slide 6

Slide 6 text

Similar to JAR ... But for PHP

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

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

Slide 9

Slide 9 text

Phar unravelled

Slide 10

Slide 10 text

Simple yet flexible File Format

Slide 11

Slide 11 text

Pack multiple files into one

Slide 12

Slide 12 text

Phar

Slide 13

Slide 13 text

Similar to Tar

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Manifest List of Files File Permission File Compression Meta Data

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Stub Piece of PHP code: Bootstraping Autoloader

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Going more practical

Slide 20

Slide 20 text

Normal Include

Slide 21

Slide 21 text

Phar Include

Slide 22

Slide 22 text

Phar works via Streams

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Does not extract to disk

Slide 26

Slide 26 text

Works on web and CLI

Slide 27

Slide 27 text

Can be ran without the extension

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Why use Phar?

Slide 30

Slide 30 text

Full Applications

Slide 31

Slide 31 text

CLI Applications Pyrus PHPUnit Composer

Slide 32

Slide 32 text

Web Applications

Slide 33

Slide 33 text

go-pear.phar PEAR installation in PHP builds Setup Tools

Slide 34

Slide 34 text

Supporting Code

Slide 35

Slide 35 text

Plugins Lithium

Slide 36

Slide 36 text

Themes

Slide 37

Slide 37 text

Silex Libraries

Slide 38

Slide 38 text

Handy Packaging!

Slide 39

Slide 39 text

Executable file for applications!

Slide 40

Slide 40 text

Pros and Cons

Slide 41

Slide 41 text

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

Slide 42

Slide 42 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 43

Slide 43 text

Stubs

Slide 44

Slide 44 text

Bootstrap for phar

Slide 45

Slide 45 text

Smallest possible Stub

Slide 46

Slide 46 text

__HALT_COMPILER() is a minimum requirement

Slide 47

Slide 47 text

Not used when phar file is used via streams

Slide 48

Slide 48 text

Web Phar

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

CLI Phar

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

Web and CLI Phar

Slide 53

Slide 53 text

CLI + Autoload

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

Phar offers a default stub

Slide 56

Slide 56 text

Will work for most

Slide 57

Slide 57 text

Extracts to temp dir if Phar is not available

Slide 58

Slide 58 text

Performance

Slide 59

Slide 59 text

Works with APC

Slide 60

Slide 60 text

Works on files inside the archive

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

Security

Slide 63

Slide 63 text

Phar disallows writing by default

Slide 64

Slide 64 text

Suhosin Enabled by default in Debian and Ubuntu

Slide 65

Slide 65 text

Signatures are used to check the integrity of the archive

Slide 66

Slide 66 text

Signature Hashes: MD5 SHA1 SHA256 SHA512 OpenSSL

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

Extra Goodies!

Slide 69

Slide 69 text

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

Slide 70

Slide 70 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 71

Slide 71 text

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

Slide 72

Slide 72 text

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