Phar, The PHP .exe
                          format


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

Friday, 27 January 12
Who am I?



Friday, 27 January 12
Helgi
                        Co-founded Orchestra.io
                        Work at EngineYard
                        PEAR Developer
                        From Iceland
                        @h on Twitter


Friday, 27 January 12
What is Phar?



Friday, 27 January 12
Phar
                          =
                        PHP Archive

Friday, 27 January 12
Similar to JAR

                           ... But for PHP


Friday, 27 January 12
Phar

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




Friday, 27 January 12
PEAR: PHP_Archive

                        Reference implementation
                        User Land Code
                        Less Powerful
                        Works on older PHP
                        Not maintained anymore



Friday, 27 January 12
Phar unravelled



Friday, 27 January 12
Simple yet flexible
                           File Format


Friday, 27 January 12
Pack multiple files
                             into one


Friday, 27 January 12
Phar




Friday, 27 January 12
Similar to Tar



Friday, 27 January 12
The special sauce
                          1. Stub
                          2. Manifest
                          3. File Contents
                          4. Signature (optional)




Friday, 27 January 12
Manifest
                        List of Files
                        File Permission
                        File Compression
                        Meta Data




Friday, 27 January 12
Compression
                               Whole Archive
                                  Per file


                        Available Compressions

                              gzip, bz2 and zip


Friday, 27 January 12
Stub

                        Piece of PHP code
                           Bootstraping
                           Autoloader




Friday, 27 January 12
Get all the code used in
                          this talk and more!


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




Friday, 27 January 12
Going more practical



Friday, 27 January 12
Normal Include




Friday, 27 January 12
Phar Include




Friday, 27 January 12
Phar works via Streams




Friday, 27 January 12
Streams Usage

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




Friday, 27 January 12
Friday, 27 January 12
Does not extract to disk




Friday, 27 January 12
Works on web and CLI




Friday, 27 January 12
Can be ran without the
                              extension




Friday, 27 January 12
Why use Phar?



Friday, 27 January 12
Full Applications



Friday, 27 January 12
CLI Applications
                               Pyrus
                              PHPUnit




Friday, 27 January 12
Web Applications



Friday, 27 January 12
Setup Tools
                                 go-pear.phar
                        PEAR installation in PHP builds




Friday, 27 January 12
Supporting Code



Friday, 27 January 12
Plugins

                         Lithium




Friday, 27 January 12
Themes



Friday, 27 January 12
Libraries

                           Silex




Friday, 27 January 12
Handy
                        Packaging!



Friday, 27 January 12
Executable file for
                          applications!




Friday, 27 January 12
Pros and Cons



Friday, 27 January 12
Pros

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



Friday, 27 January 12
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
Stubs



Friday, 27 January 12
Bootstrap for phar



Friday, 27 January 12
Smallest possible Stub




Friday, 27 January 12
__HALT_COMPILER() is a minimum requirement




Friday, 27 January 12
Not used when phar file
                          is used via streams




Friday, 27 January 12
Web Phar




Friday, 27 January 12
Phar::interceptFileFuncs()
                        Phar::mungServer()
                        Phar::WebPhar




Friday, 27 January 12
CLI Phar




Friday, 27 January 12
No Phar::WebPhar()
                        Simple file include
                        Shebang for easy execution




Friday, 27 January 12
Web and CLI Phar




Friday, 27 January 12
CLI + Autoload




Friday, 27 January 12
CLI + Autoload
      cli.php



         a.php




        b.php


Friday, 27 January 12
Phar offers a default stub




Friday, 27 January 12
Will work for most




Friday, 27 January 12
Extracts to temp dir if
                        Phar is not available




Friday, 27 January 12
Performance



Friday, 27 January 12
Works with APC



Friday, 27 January 12
Works on files inside
                 the archive


Friday, 27 January 12
Remember, everything
                           goes via PHP


                         Extract static files to a directory
                              and serve from there



Friday, 27 January 12
Security



Friday, 27 January 12
Phar disallows
                        writing by default




Friday, 27 January 12
Suhosin

                    Enabled by default in Debian and Ubuntu




Friday, 27 January 12
Signatures are used to check the
                            integrity of the archive




Friday, 27 January 12
Signature
                        Hashes:   OpenSSL
                         MD5
                         SHA1
                         SHA256
                         SHA512




Friday, 27 January 12
Hashes

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




Friday, 27 January 12
Extra Goodies!



Friday, 27 January 12
Phar::mount()

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




Friday, 27 January 12
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
Useful Tools

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




Friday, 27 January 12
Get all the code used in
                          this talk and more!


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




Friday, 27 January 12
Come to the winter party tomorrow!
                    Beer and food is on me :-)

                        And some awesome swag!




Friday, 27 January 12
Questions?

                                      @h
                            helgi@engineyard.com

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


                        Joind.in: http://joind.in/4755
Friday, 27 January 12

Phar, The PHP .exe Format