Download - Phar, The PHP .exe Format
![Page 1: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/2.jpg)
Who am I?
Friday, 27 January 12
![Page 3: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/4.jpg)
What is Phar?
Friday, 27 January 12
![Page 5: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/5.jpg)
Phar=
PHP Archive
Friday, 27 January 12
![Page 6: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/6.jpg)
Similar to JAR
... But for PHP
Friday, 27 January 12
![Page 7: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/9.jpg)
Phar unravelled
Friday, 27 January 12
![Page 10: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/10.jpg)
Simple yet flexibleFile Format
Friday, 27 January 12
![Page 11: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/11.jpg)
Pack multiple files into one
Friday, 27 January 12
![Page 12: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/12.jpg)
Phar
Friday, 27 January 12
![Page 13: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/13.jpg)
Similar to Tar
Friday, 27 January 12
![Page 14: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/14.jpg)
The special sauce
1. Stub
2. Manifest
3. File Contents
4. Signature (optional)
Friday, 27 January 12
![Page 15: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/15.jpg)
Manifest
List of Files
File Permission
File Compression
Meta Data
Friday, 27 January 12
![Page 16: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/16.jpg)
Compression
Whole Archive
Per file
gzip, bz2 and zip
Available Compressions
Friday, 27 January 12
![Page 17: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/17.jpg)
Stub
Piece of PHP code
Bootstraping
Autoloader
Friday, 27 January 12
![Page 18: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/19.jpg)
Going more practical
Friday, 27 January 12
![Page 20: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/20.jpg)
Normal Include
Friday, 27 January 12
![Page 21: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/21.jpg)
Phar Include
Friday, 27 January 12
![Page 22: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/22.jpg)
Phar works via Streams
Friday, 27 January 12
![Page 23: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/24.jpg)
Friday, 27 January 12
![Page 25: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/25.jpg)
Does not extract to disk
Friday, 27 January 12
![Page 26: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/26.jpg)
Works on web and CLI
Friday, 27 January 12
![Page 27: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/27.jpg)
Can be ran without the extension
Friday, 27 January 12
![Page 28: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/28.jpg)
Why use Phar?
Friday, 27 January 12
![Page 29: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/29.jpg)
Full Applications
Friday, 27 January 12
![Page 30: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/30.jpg)
CLI Applications
Pyrus
PHPUnit
Friday, 27 January 12
![Page 31: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/31.jpg)
Web Applications
Friday, 27 January 12
![Page 32: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/32.jpg)
Setup Tools
go-pear.phar
PEAR installation in PHP builds
Friday, 27 January 12
![Page 33: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/33.jpg)
Supporting Code
Friday, 27 January 12
![Page 34: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/34.jpg)
Plugins
Lithium
Friday, 27 January 12
![Page 35: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/35.jpg)
Themes
Friday, 27 January 12
![Page 36: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/36.jpg)
Libraries
Silex
Friday, 27 January 12
![Page 37: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/37.jpg)
Handy Packaging!
Friday, 27 January 12
![Page 38: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/38.jpg)
Executable file for applications!
Friday, 27 January 12
![Page 39: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/39.jpg)
Pros and Cons
Friday, 27 January 12
![Page 40: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/42.jpg)
Stubs
Friday, 27 January 12
![Page 43: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/43.jpg)
Bootstrap for phar
Friday, 27 January 12
![Page 44: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/44.jpg)
Smallest possible Stub
Friday, 27 January 12
![Page 45: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/45.jpg)
__HALT_COMPILER() is a minimum requirement
Friday, 27 January 12
![Page 46: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/46.jpg)
Not used when phar file is used via streams
Friday, 27 January 12
![Page 47: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/47.jpg)
Web Phar
Friday, 27 January 12
![Page 48: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/48.jpg)
Phar::interceptFileFuncs()
Phar::mungServer()
Phar::WebPhar
Friday, 27 January 12
![Page 49: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/49.jpg)
CLI Phar
Friday, 27 January 12
![Page 50: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/50.jpg)
No Phar::WebPhar()
Simple file include
Shebang for easy execution
Friday, 27 January 12
![Page 51: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/51.jpg)
Web and CLI Phar
Friday, 27 January 12
![Page 52: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/52.jpg)
CLI + Autoload
Friday, 27 January 12
![Page 53: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/53.jpg)
CLI + Autoloadcli.php
a.php
b.php
Friday, 27 January 12
![Page 54: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/54.jpg)
Phar offers a default stub
Friday, 27 January 12
![Page 55: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/55.jpg)
Will work for most
Friday, 27 January 12
![Page 56: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/56.jpg)
Extracts to temp dir if Phar is not available
Friday, 27 January 12
![Page 57: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/57.jpg)
Performance
Friday, 27 January 12
![Page 58: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/58.jpg)
Works with APC
Friday, 27 January 12
![Page 59: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/59.jpg)
Works on files inside the archive
Friday, 27 January 12
![Page 60: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/60.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/61.jpg)
Security
Friday, 27 January 12
![Page 62: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/62.jpg)
Phar disallows writing by default
Friday, 27 January 12
![Page 63: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/63.jpg)
Suhosin
Enabled by default in Debian and Ubuntu
Friday, 27 January 12
![Page 64: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/64.jpg)
Signatures are used to check the integrity of the archive
Friday, 27 January 12
![Page 65: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/65.jpg)
Signature
Hashes:
MD5
SHA1
SHA256
SHA512
OpenSSL
Friday, 27 January 12
![Page 66: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/66.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/67.jpg)
Extra Goodies!
Friday, 27 January 12
![Page 68: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/68.jpg)
Phar::mount()
Mount external files, such as config, into the phar archive
Friday, 27 January 12
![Page 69: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/69.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/70.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/71.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/72.jpg)
Come to the winter party tomorrow!Beer and food is on me :-)
And some awesome swag!
Friday, 27 January 12
![Page 73: Phar, The PHP .exe Format](https://reader034.vdocuments.us/reader034/viewer/2022051314/54c718464a7959381b8b45ae/html5/thumbnails/73.jpg)
Questions?
Joind.in: http://joind.in/4755
https://github.com/helgi/phar-talk
Friday, 27 January 12