How to set timezone at application level and user login level using PHP
May 27, 2013How to use 9-Patch images in Android
May 29, 2013The phar extension is used to put entire PHP applications into a single file called a "phar" (PHP Archive) for easy distribution and installation. It provides the means for distributing code as a single archive, that does not have to be extracted to a folder before usage. In addition to providing this service, the phar extension also provides a file-format abstraction method for creating and manipulating tar and zip files through the PharData class, much as PDO provides a unified interface for accessing different databases.
PHAR files are treated as read-only by default, and you can use any PHAR file without any special configuration. This is great for deployment. But as you’ll be creating your own PHARs you’ll need to allow write-access which is done through the php.ini file.
Phar archives are best characterized as a convenient way to group several files into a single file. As such, a phar archive provides a way to distribute a complete PHP application in a single file and run it from that file without the need to extract it to disk. Additionally, phar archives can be executed by PHP as easily as any other file, both on the commandline and from a web server. Phar is kind of like a thumb drive for PHP applications.
To package your application using Phar, you have to build your application’s directory structure. The structure should be similar to something like:
My App
Build
Src
common.php
config.ini
index.php
The Phar package would be stored inside the “Build” folder. The source folder would contain the files that make up the application while the config.ini would contain the configuration settings for the application.
index.php will serve as the application’s entry point, common.php will be a pseudo-library of common classes used by the application, and config.ini will be the configuration file for the application.
Now add below code to your index.php file:
<?php require_once "phar://myapp.phar/common.php"; $config = parse_ini_file("config.ini"); AppManager::run($config);
And add this to your common.php file:
<?php
class AppManager
{
public static function run($config) {
echo "Application is now running with the following configuration... ";
var_dump($config);
}
}
In the configuration file, that is config.ini add this code:
[database]
host=localhost
db=dbname
user=myuser
pass=dbpass
Now all you need is the script that would generate the Phar package:
<?php
$srcRoot = "~/myapp/src";
$buildRoot = "~/myapp/build";
$phar = new Phar($buildRoot . "/myapp.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");
Phar requires PHP 5.2.0 or newer. Additional features require the SPL extension in order to take advantage of iteration and array access to a Phar's file contents. The pharstream does not require any additional extensions to function.
For further explaination and help please go to http://www.php.net/manual/en/book.phar.php
Courtesy: http://phpmaster.com/packaging-your-apps-with-phar