phar, the php .exe format

73
Phar, The PHP .exe format Helgi Þormar Þorbjörnsson PHP Benelux, 27th of January 2012 Friday, 27 January 12

Upload: helgi-bormar-borbjoernsson

Post on 27-Jan-2015

132 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Phar, The PHP .exe Format

Phar, The PHP .exe format

Helgi Þormar ÞorbjörnssonPHP Benelux, 27th of January 2012

Friday, 27 January 12

Page 2: Phar, The PHP .exe Format

Who am I?

Friday, 27 January 12

Page 3: Phar, The PHP .exe Format

Co-founded Orchestra.io

Work at EngineYard

PEAR Developer

From Iceland

@h on Twitter

Helgi

Friday, 27 January 12

Page 4: Phar, The PHP .exe Format

What is Phar?

Friday, 27 January 12

Page 5: Phar, The PHP .exe Format

Phar=

PHP Archive

Friday, 27 January 12

Page 6: Phar, The PHP .exe Format

Similar to JAR

... But for PHP

Friday, 27 January 12

Page 7: Phar, The PHP .exe Format

Phar

PHP Extension

Built in / default from 5.3 onwards

More Powerful than PHP_Archive

Friday, 27 January 12

Page 8: Phar, The PHP .exe Format

PEAR: PHP_Archive

Reference implementation

User Land Code

Less Powerful

Works on older PHP

Not maintained anymore

Friday, 27 January 12

Page 9: Phar, The PHP .exe Format

Phar unravelled

Friday, 27 January 12

Page 10: Phar, The PHP .exe Format

Simple yet flexibleFile Format

Friday, 27 January 12

Page 11: Phar, The PHP .exe Format

Pack multiple files into one

Friday, 27 January 12

Page 12: Phar, The PHP .exe Format

Phar

Friday, 27 January 12

Page 13: Phar, The PHP .exe Format

Similar to Tar

Friday, 27 January 12

Page 14: Phar, The PHP .exe Format

The special sauce

1. Stub

2. Manifest

3. File Contents

4. Signature (optional)

Friday, 27 January 12

Page 15: Phar, The PHP .exe Format

Manifest

List of Files

File Permission

File Compression

Meta Data

Friday, 27 January 12

Page 16: Phar, The PHP .exe Format

Compression

Whole Archive

Per file

gzip, bz2 and zip

Available Compressions

Friday, 27 January 12

Page 17: Phar, The PHP .exe Format

Stub

Piece of PHP code

Bootstraping

Autoloader

Friday, 27 January 12

Page 18: Phar, The PHP .exe Format

Get all the code used in this talk and more!

https://github.com/helgi/phar-talk

Friday, 27 January 12

Page 19: Phar, The PHP .exe Format

Going more practical

Friday, 27 January 12

Page 20: Phar, The PHP .exe Format

Normal Include

Friday, 27 January 12

Page 21: Phar, The PHP .exe Format

Phar Include

Friday, 27 January 12

Page 22: Phar, The PHP .exe Format

Phar works via Streams

Friday, 27 January 12

Page 23: Phar, The PHP .exe Format

Streams Usage

fopen / fwrite / fclose

file_get_contents

opendir / rmdir / mkdir

anything that works with streams

Friday, 27 January 12

Page 24: Phar, The PHP .exe Format

Friday, 27 January 12

Page 25: Phar, The PHP .exe Format

Does not extract to disk

Friday, 27 January 12

Page 26: Phar, The PHP .exe Format

Works on web and CLI

Friday, 27 January 12

Page 27: Phar, The PHP .exe Format

Can be ran without the extension

Friday, 27 January 12

Page 28: Phar, The PHP .exe Format

Why use Phar?

Friday, 27 January 12

Page 29: Phar, The PHP .exe Format

Full Applications

Friday, 27 January 12

Page 30: Phar, The PHP .exe Format

CLI Applications

Pyrus

PHPUnit

Friday, 27 January 12

Page 31: Phar, The PHP .exe Format

Web Applications

Friday, 27 January 12

Page 32: Phar, The PHP .exe Format

Setup Tools

go-pear.phar

PEAR installation in PHP builds

Friday, 27 January 12

Page 33: Phar, The PHP .exe Format

Supporting Code

Friday, 27 January 12

Page 34: Phar, The PHP .exe Format

Plugins

Lithium

Friday, 27 January 12

Page 35: Phar, The PHP .exe Format

Themes

Friday, 27 January 12

Page 36: Phar, The PHP .exe Format

Libraries

Silex

Friday, 27 January 12

Page 37: Phar, The PHP .exe Format

Handy Packaging!

Friday, 27 January 12

Page 38: Phar, The PHP .exe Format

Executable file for applications!

Friday, 27 January 12

Page 39: Phar, The PHP .exe Format

Pros and Cons

Friday, 27 January 12

Page 40: Phar, The PHP .exe Format

Pros

Single download with all dependencies

Run multiple versions in parallel

Upgrades are easy

No unpacking

Security against modifications

Friday, 27 January 12

Page 41: Phar, The PHP .exe Format

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

Friday, 27 January 12

Page 42: Phar, The PHP .exe Format

Stubs

Friday, 27 January 12

Page 43: Phar, The PHP .exe Format

Bootstrap for phar

Friday, 27 January 12

Page 44: Phar, The PHP .exe Format

Smallest possible Stub

Friday, 27 January 12

Page 45: Phar, The PHP .exe Format

__HALT_COMPILER() is a minimum requirement

Friday, 27 January 12

Page 46: Phar, The PHP .exe Format

Not used when phar file is used via streams

Friday, 27 January 12

Page 47: Phar, The PHP .exe Format

Web Phar

Friday, 27 January 12

Page 48: Phar, The PHP .exe Format

Phar::interceptFileFuncs()

Phar::mungServer()

Phar::WebPhar

Friday, 27 January 12

Page 49: Phar, The PHP .exe Format

CLI Phar

Friday, 27 January 12

Page 50: Phar, The PHP .exe Format

No Phar::WebPhar()

Simple file include

Shebang for easy execution

Friday, 27 January 12

Page 51: Phar, The PHP .exe Format

Web and CLI Phar

Friday, 27 January 12

Page 52: Phar, The PHP .exe Format

CLI + Autoload

Friday, 27 January 12

Page 53: Phar, The PHP .exe Format

CLI + Autoloadcli.php

a.php

b.php

Friday, 27 January 12

Page 54: Phar, The PHP .exe Format

Phar offers a default stub

Friday, 27 January 12

Page 55: Phar, The PHP .exe Format

Will work for most

Friday, 27 January 12

Page 56: Phar, The PHP .exe Format

Extracts to temp dir if Phar is not available

Friday, 27 January 12

Page 57: Phar, The PHP .exe Format

Performance

Friday, 27 January 12

Page 58: Phar, The PHP .exe Format

Works with APC

Friday, 27 January 12

Page 59: Phar, The PHP .exe Format

Works on files inside the archive

Friday, 27 January 12

Page 60: Phar, The PHP .exe Format

Remember, everything goes via PHP

Extract static files to a directory and serve from there

Friday, 27 January 12

Page 61: Phar, The PHP .exe Format

Security

Friday, 27 January 12

Page 62: Phar, The PHP .exe Format

Phar disallows writing by default

Friday, 27 January 12

Page 63: Phar, The PHP .exe Format

Suhosin

Enabled by default in Debian and Ubuntu

Friday, 27 January 12

Page 64: Phar, The PHP .exe Format

Signatures are used to check the integrity of the archive

Friday, 27 January 12

Page 65: Phar, The PHP .exe Format

Signature

Hashes:

MD5

SHA1

SHA256

SHA512

OpenSSL

Friday, 27 January 12

Page 66: Phar, The PHP .exe Format

Hashes

By default executable Phar is signed SHA1

Tar / Zip are not unless specified

Friday, 27 January 12

Page 67: Phar, The PHP .exe Format

Extra Goodies!

Friday, 27 January 12

Page 68: Phar, The PHP .exe Format

Phar::mount()

Mount external files, such as config, into the phar archive

Friday, 27 January 12

Page 69: Phar, The PHP .exe Format

PharData

Phar archives can be extracted to disk

Can operate on non-phar gzip and tar

extract and compress archives

Like PDO for archives! :-)

Friday, 27 January 12

Page 70: Phar, The PHP .exe Format

Useful Tools

phar-util

https://github.com/koto/phar-util

empir

http://empir.sourceforge.net/

Friday, 27 January 12

Page 71: Phar, The PHP .exe Format

Get all the code used in this talk and more!

https://github.com/helgi/phar-talk

Friday, 27 January 12

Page 72: Phar, The PHP .exe Format

Come to the winter party tomorrow!Beer and food is on me :-)

And some awesome swag!

Friday, 27 January 12