In order to deploy application easier, we package java application in to a jar.
Now, in PHP we have phar.
In order to enable packaging in PHP, change the config from php.ini to phar.readonly = 0
Let’s write some code. create a new project with following directory:
sample1
|
|---build.php
|---src
|----index.php
|----lib.php
|---build/
|---test/
It’s a very simple application, access the index.php, then index.php require lib.php, execute some function in lib.php, echo something in console.
in commons, it’s like
$php src/index.php
//echo it works.
we just write the index.php and lib.php as usual: This is very simple, but we want to package index.php and lib.php with phar, if they are packaged into one phar file, PHP can not load file via absolute path, just change one line: After some refactor, our index.php looks like:
//index.php
<?php
include dirname(__FILE__)."/lib.php";
Foo::run();
</code><pre>
<code><pre>
//lib.php
<?php
class Foo
{
public static function run()
{
echo PHP_EOL;
echo 'it works!';
echo PHP_EOL;
}
}
</code>
include
<?php
include "phar://sample1.phar/lib.php";
Foo::run();
OK, out application should work with phar, we need a build script to create phar file.
let's add something into build.php
<?php
$srcRoot = dirname(__FILE__)."/src";
$buildRoot = dirname(__FILE__)."/build";
$phar = new Phar($buildRoot . "/sample1.phar",
FilesystemIterator::CURRENT_AS_FILEINFO |
FilesystemIterator::KEY_AS_FILENAME, "sample1.phar");
$phar["index.php"] = file_get_contents($srcRoot . "/index.php");
$phar["lib.php"] = file_get_contents($srcRoot . "/lib.php");
$phar->setStub($phar->createDefaultStub("index.php"));
Then, try to run:
$php build.php $php build/sample1.phar
Now you have your first phar package. Do not worry about new features in PHP 5.4+, phar support namespace, let's see the second sample here:
sample2 | |--build/ |--build.xml/ |--src/ | |--autoload.php | |--saharabear/ | | --phar/ | | --index.php | | --Foo.php
//index.php
<?php
require_once "phar://sample2.phar/autoload.php";
use saharabear\phar\Foo;
Foo::run();
//Foo.php
<?php
namespace saharabear\phar;
class Foo
{
public static function run()
{
echo PHP_EOL;
echo 'it works!';
echo PHP_EOL;
}
}
//autoload.php
<?php
class PharSampleAutoload {
static public function loader($className) {
$filename = "phar://sample2.phar/".str_replace('\\', '/', $className) . ".php";
if (file_exists($filename)) {
include($filename);
if (class_exists($className)) {
return TRUE;
}
}
return FALSE;
}
}
spl_autoload_register('PharSampleAutoload::loader');
//build.php
<?php
$srcRoot = dirname(__FILE__)."/src";
$buildRoot = dirname(__FILE__)."/build";
$phar = new Phar($buildRoot . "/sample2.phar",
FilesystemIterator::CURRENT_AS_FILEINFO |
FilesystemIterator::KEY_AS_FILENAME, "sample2.phar");
$phar->buildFromDirectory($srcRoot,'/\.php$/');
$phar["index.php"] = file_get_contents($srcRoot . "/saharabear/phar/index.php");
$phar->setStub($phar->createDefaultStub("index.php"));
Now, you get all sample. Source code is on github, you are free to clone.