Phar is a kind of packaged code tool that was provided after PHP5. This is essentially a package of code in the form of a Java Jar file, but since PHP is not compiled, this Phar is essentially a package of code as is, and will not be compiled. But we can compress the packaged Phar package.
Also, very few people actually use Phar packages, especially now that Composer has become the de facto code base standard. However, the Composer installation package is itself a.phar package. One of the main reasons is that the installation of code packages in the form of Phar is not as easy as Composer. On the other hand, early programmers, especially LAMP/LAMP programmers, prefer to copy open source code rather than use a toolkit directly. After all, having source code in our hands makes us feel more secure. In fact, even though Composer is directly downloaded from the source code, we never really read it. One of the biggest advantages of Composer over Phar is the automatic loading of code and the standard PSR command space and directory specifications. Both of these are not available in the Phar, so we have to require them to use the Phar package.
It’s out of date, but let’s look at it briefly. The way a Phar packages code may come in handy at some point, especially when encapsulating some internal common library functions.
The code package
Let’s start by creating a directory tree in a standard format.
In this directory tree, the SRC directory houses the source code and the build directory houses the generated.phar code package.
// index.php
require_once "phar://myphar.phar/common.php";
Copy the code
In the index.php file, we simply refer to common.php. Note that the common.php file is loaded using the Phar pseudo-protocol. We have explained the content of pseudo protocol in an article before.
// common.php
class Manager{
public static function run($config){
echo "AAA", PHP_EOL;
var_dump($config);
}
public static function ChineseMobile($mobile){
if(preg_match("/^1[34578]\d{9}$/".$mobile)) {return true;
}
return false; }}Copy the code
The common.php file provides only one class and two simple methods to test. The run() method simply prints out the print and the arguments passed in. ChineseMobile() method is a function we provide to judge our domestic mobile phone number.
[database]
host=localhost
db=dbname
user=myuser
pass=dbpass
Copy the code
Ini is a configuration file. In fact, we can read the configuration file directly in the Phar code, or we can build the configuration file along with the code to the specified directory.
With the source files ready, it’s time to prepare the packaged build files.
// create-phar.php
$srcRoot = "./myphar/src";
$buildRoot = "./myphar/build";
$phar = new Phar($buildRoot . "/myphar.phar".FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, "myphar.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");
Copy the code
The code isn’t complicated. It’s basically a Phar class that specifies the directory to generate the file, the name of the file, and then calls our package entry file index.php using the createDefaultStub() method, which creates a stub for the specified.phar file. You specify an entry file, just like the main() method entry in Java.
We then copied the config.ini file to the build release directory.
Then run the create-phar.php file from the command line to generate the code package.
# php ./create-phar.php
Copy the code
If you open the myphar.phar file with a text editor, you will find the familiar PHP code inside. If you pull down to the bottom, you will find that the contents of index.php and common.php are compiled in this file. The auto-generated code above is just some bootstrap or pre-prep statements that the Phar extension prepared for us, and all user-written source code will be at the bottom of this file. In other words, you can download the installation package for Composer, which is the.phar file and see what’s in it.
Now it’s time to use it, and this is pretty simple.
$config = parse_ini_file("./myphar/build/config.ini");
require './myphar/build/myphar.phar';
Manager::run($config);
// AAA
// array(4) {
// ["host"]=>
// string(9) "localhost"
// ["db"]=>
// string(6) "dbname"
// ["user"]=>
// string(6) "myuser"
// ["pass"]=>
// string(6) "dbpass"
// }
var_dump(Manager::ChineseMobile('13811111111'));
var_dump(Manager::ChineseMobile('138111111112'));
// bool(true)
// bool(false)
Copy the code
Compression capability
As mentioned earlier, Phar has long lost out to Composer as a code base, but in addition to being used as an installation package, Phar is also a compression tool in its own right. Can be used to archive some files, text, directories and so on. Here’s a quick look at how Phar is used for archiving text.
unlink('./my.phar');
unlink('./my.phar.bz2');
unlink('./my.phar.gz');
$p = new Phar('./my.phar'.0 ,'my.phar');
$p['myfile1.txt'] = 'hi1';
$p['myfile2.txt'] = 'hi2';
$p1 = $p->compress(Phar::GZ);
$p2 = $p->compress(Phar::BZ2);
unset($p);
$decompressPhar = new Phar('./my.phar'.0 ,'my.phar');
foreach($decompressPhar as $file) {// $file is the PharFileInfo object returned
var_dump($file->getFileName());
var_dump($file->isCompressed());
var_dump($file->isCompressed(Phar::BZ2));
var_dump($file->isCompressed(Phar::GZ));
var_dump($file->getContent());
}
echo '= = = = = = = = = = = = = = = = = =', PHP_EOL;
// string(11) "myfile1.txt"
// bool(false)
// bool(false)
// bool(false)
// string(3) "hi1"
// string(11) "myfile2.txt"
// bool(false)
// bool(false)
// bool(false)
// string(3) "hi2"
Copy the code
First, we instantiate a Phar class again, and then we add properties to it like an array, so that the property contents are packaged into the.phar file. By looking directly at the my.phar file, we can see that the myFile1.txt property is saved as a file directly, that is, it helps us to convert the text into a file and package it in the my.phar compressed file.
The compress() method compresses the current Phar object into a file of some format. Here we have directly compressed the Bzip2 and GZ files. Calling this method directly generates the corresponding compressed file.
The object generated by the Phar object in traversal is the PharFileInfo object, which has many file-like manipulation functions. You may find this in the official documentation.
If we iterate through my.phar. Gz, isCompressed() will still be false, is the file not compressed? In fact, we need another function to compress all files in the same format.
$p = new Phar('./my.phar'.0 ,'my.phar');
$p->compressFiles(Phar::GZ);
unset($p);
$decompressPhar = new Phar('./my.phar.gz'.0 ,'my.phar');
foreach($decompressPhar as $file) {// $file is the PharFileInfo object returned
var_dump($file->getFileName());
var_dump($file->isCompressed());
var_dump($file->isCompressed(Phar::BZ2));
var_dump($file->isCompressed(Phar::GZ));
var_dump($file->getContent());
}
echo '= = = = = = = = = = = = = = = = = =', PHP_EOL;
// string(11) "myfile1.txt"
// bool(true)
// bool(false)
// bool(true)
// string(3) "hi1"
// string(11) "myfile2.txt"
// bool(true)
// bool(false)
// bool(true)
// string(3) "hi2"
Copy the code
After compressFiles() is used to compress the entire.phar file in the same format, isCompressed() will return true when printing.
Data format Phar
Finally, there is no need to use the Phar class if it is just for packing compression. The Phar class’s primary purpose is to package executable PHP source code, and its createDefaultStub() method is very important. We don’t need this method if we’re just packing regular files, so we can use another PharData class to pack and compress the data. The method of use is exactly the same as the Phar class. Also, the PharData class can be packaged directly into files like tar.
$p = new PharData('./myData.tar');
$p['myfile1.txt'] = 'hi1';
$p['myfile2.txt'] = 'hi2';
foreach($p as $file){
var_dump($file->getFileName());
var_dump($file->isCompressed());
var_dump($file->isCompressed(Phar::BZ2));
var_dump($file->isCompressed(Phar::GZ));
var_dump($file->getContent());
}
echo '= = = = = = = = = = = = = = = = = =', PHP_EOL;
// string(11) "myfile1.txt"
// bool(false)
// bool(false)
// bool(false)
// string(3) "hi1"
// string(11) "myfile2.txt"
// bool(false)
// bool(false)
// bool(false)
// string(3) "hi2"
Copy the code
conclusion
To be honest, Phar is a really unpopular project, but it can be very useful in some cases, such as when it is beaten by Composer in the code package area, but it can also be used as an installation package for Composer, that is, you can’t install Composer without Phar. As a compression tool, although there is a strong strength but the use is also very little. Therefore, we are still for the purpose of understanding, and if we feel that some scenarios are very suitable, we can also thoroughly study them and use them in our actual projects. After all, it’s part of PHP, doesn’t require any compilation, installation, or other support, and is pretty native.
Test code:
Github.com/zhangyue050…
Reference Documents:
www.php.net/manual/zh/b…
www.webhek.com/post/packag…
www.mamicode.com/info-detail…