PHAR

The PHP Archiving Utility

Nguyen Tien Si

GO1

Agenda

  • Introducing Phar & What can it do?
  • How does it work?
  • Installing/Configuring
  • Examples
  • Useful case use Phar

What's Phar & What can it do?

  • Phar is to PHP as Jar is to Java.
  • Phar is an archiving utility for PHP.
  • Phar can turn many files into one.
  • Phar can run of PHP application as a single file.

How does it work?

  • Phar groups files together to create a signle .phar file.
  • Phar is implemented through a PHP Stream Wrapper (phar://).
  • Phars are not extract before used. Files accessed and used with any PHP file function that support PHP Stream Wrappers.
<?php

// Example #1 Using an external file
include '/path/to/external/file.php';

// Example #2 Using a file within a phar archive
include 'phar:///path/to/myphar.phar/file.php';

?>

Installing/Configuring

Requirements

  • PHP 5.2.0 or newer.
  • Additional features require the SPL extension.
  • You may optionally wish to enable the zlib and bzip2 extensions to take advantage of compressed phar support.
  • Read more about php.net/manual/en/phar.requirements.php

Installing/Configuring

  • The Phar extension is built into PHP as of PHP version 5.3.0.
  • PHP ini:
    • phar.readonly (set to 0 to security reasons)
    • phar.require_hash
    • phar.extract_list (removed as of phar 2.0.0)
    • phar.cache_list

Examples

Example Phar app

Folder structures

  • myphar
    • build
    • src
      • common.php
      • config.ini
      • index.php

 

Contents

<?php
// index.php

require_once "phar://myphar.phar/common.php";
$config = parse_ini_file("config.ini");
AppManager::run($config);
<?php
// common.php

class AppManager
{
    public static function run($config) {
         echo "Print the config variable";
         var_dump($config);
     }
}
[phar]
app = myphar

Creating the Phar

<?php
// create-phar.php

$srcRoot = "src/";
$buildRoot = "build/";

// Create a new Phar object with three args
$phar = new Phar($buildRoot . "/myphar.phar",
    FilesystemIterator::CURRENT_AS_FILEINFO | 
    FilesystemIterator::KEY_AS_FILENAME, "myapp.phar");

$phar["index.php"] = file_get_contents($srcRoot . "/index.php");
$phar["common.php"] = file_get_contents($srcRoot . "/common.php");
$phar->setStub($phar->createDefaultStub("index.php"));
 
copy($srcRoot . "/config.ini", $buildRoot . "/config.ini");
[12:55][mrsinguyen@go1:~/Workspaces/myphar]$ php create-phar.php

Results

[13:02][mrsinguyen@go1:~/Workspaces/myphar]$ cd build/
[13:03][mrsinguyen@go1:~/Workspaces/myphar/build]$ ls
config.ini  myphar.phar
[13:03][mrsinguyen@go1:~/Workspaces/myphar/build]$ chmod +x myphar.phar 
[13:03][mrsinguyen@go1:~/Workspaces/myphar/build]$ php myphar.phar 
Print the config variablearray(1) {
  ["app"]=>
  string(6) "myphar"
}
[13:03][mrsinguyen@go1:~/Workspaces/myphar/build]$ 
<?php
// test.php
// http://localhost/test.php

require '/home/mrsinguyen/Workspaces/myphar/build/myphar.phar';

Useful case use Phar

PHPUnit

$ wget https://phar.phpunit.de/phpunit.phar
$ chmod +x phpunit.phar
$ sudo mv phpunit.phar /usr/local/bin/phpunit
$ phpunit --version
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.

To globally install the PHAR:

You may also use the downloaded PHAR file directly:

$ wget https://phar.phpunit.de/phpunit.phar
$ php phpunit.phar --version
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.

Composer

$ curl -sS https://getcomposer.org/installer | php
$ chmod +x composer.phar
$ mv composer.phar /usr/local/bin/composer
$ composer --version
Composer version 1.0-dev (9f9cff558e5f447165f4265f320b2b1178f18301) 2015-03-08 18:02:57

To globally install the Composer:

You may also use the downloaded Composer file directly:

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar --version
Composer version 1.0-dev (9f9cff558e5f447165f4265f320b2b1178f18301) 2015-03-08 18:02:57

Limitations of Phar

  • You can package a whole application in a PHAR, but it is not an auto-deploy solution.
  • You should avoid back-writing to Phars in a real-world deployment. Instead, put all writeable files outside the archive.
  • ...

References

  • http://php.net/manual/en/book.phar.php
  • http://www.sitepoint.com/packaging-your-apps-with-phar/
  • ...

Question?

Made with Slides.com