Home > Backend Development > PHP Problem > How to use Phar code packaging tool in php

How to use Phar code packaging tool in php

醉折花枝作酒筹
Release: 2023-03-10 18:12:01
forward
2937 people have browsed it

This article will introduce to you how to use Phar code packaging tool in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to use Phar code packaging tool in php

Briefly understand the use of Phar code packaging tool

Phar is a code packaging tool provided after PHP5 tool. Essentially, it is a code package in the form of a Java Jar file. However, since PHP itself is not compiled, this Phar actually packages the code as it is without compilation. But we can compress the packaged Phar package.

In addition, there are very few people who have actually used the Phar package. Especially today when Composer has become the de facto code library standard, Phar is even more difficult to find. However, the Composer installation package itself is also a .phar packaging file.

The main reason is that the installation of code packages in the form of Phar is not as simple and convenient as Composer. On the other hand, early programmers, especially LAMP/LAMP programmers, like Go copy the open source code instead of using a toolkit directly.

After all, having the source code in hand makes us more at ease. In fact, even if Composer directly downloads the source code, no one has ever actually looked through it. The biggest advantages of Composer compared to Phar are automatic loading of code and standard PSR command space and directory specifications. These two are not available in Phar, so we must require them if we want to use the Phar package.

Although it is outdated, we still briefly learn about it. Maybe we will be able to use it when, especially when encapsulating some internal public library functions, Phar's way of packaging code is still very useful.

Code packaging

We first create a directory tree in a standard format.

How to use Phar code packaging tool in php

In this directory tree, the src directory stores the source code, and the build directory is used to store the generated .phar code package.

// index.php
<?php

require_once "phar://myphar.phar/common.php";
Copy after login

In the index.php file, we simply reference common.php. Note that the phar pseudo-protocol is used to load the common.php file. We have previously explained the content of pseudo-protocols in an article.

<?php
// 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 after login

The common.php file only provides a class and two simple methods for testing. The run() method simply outputs the printed content and the passed parameters. The ChineseMobile() method is a function we provide to determine our domestic mobile phone number.

[database]
host=localhost
db=dbname
user=myuser
pass=dbpass
Copy after login

config.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 to the specified directory along with the code.

The source code file is ready, and the next step is to prepare the packaged compiled file.

// 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 after login

The code is not complicated. It is mainly a Phar class. This class needs to specify the directory and file name of the generated file, and then use the createDefaultStub() method to call the entry file index.php of our package. This method is A stub for creating the specified .phar file. In fact, it is to specify an entry file, just like the main() method entry in Java.

Then we copied the config.ini file to the release directory build.

Then use the command line to run the create-phar.php file to generate this code package.

# php ./create-phar.php
Copy after login

How to use Phar code packaging tool in php

Use a text editor to open the myphar.phar file. We will find that it is still the familiar PHP code. Pull it to the bottom and you will find index.php and The contents of common.php are compiled into this file. The automatically generated codes above are some bootstrapping or pre-prepared statements, which are prepared for us by the Phar extension. All source codes written by users will be at the bottom of this file. In other words, you can download the Composer installation package, which is the .phar file, and see what is written in it.

The next step is to use it, this is very simple.

$config = parse_ini_file("./myphar/build/config.ini");
require &#39;./myphar/build/myphar.phar&#39;;

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(&#39;13811111111&#39;));
var_dump(Manager::ChineseMobile(&#39;138111111112&#39;));
// bool(true)
// bool(false)
Copy after login

Compression ability

As mentioned before, as a code library, Phar has long been defeated by Composer, but in addition to being used as some installation packages In addition to being used, Phar itself is also a compression tool. It can be used to archive some files, texts, directories and the like. Next, let me briefly take a look at how Phar is used for text archiving.

unlink(&#39;./my.phar&#39;);
unlink(&#39;./my.phar.bz2&#39;);
unlink(&#39;./my.phar.gz&#39;);
$p = new Phar(&#39;./my.phar&#39;, 0 ,&#39;my.phar&#39;);
$p[&#39;myfile1.txt&#39;] = &#39;hi1&#39;;
$p[&#39;myfile2.txt&#39;] = &#39;hi2&#39;;
$p1 = $p->compress(Phar::GZ);
$p2 = $p->compress(Phar::BZ2);
unset($p);

$decompressPhar  = new Phar(&#39;./my.phar&#39;, 0 ,&#39;my.phar&#39;);
foreach($decompressPhar as $file){
    // $file 是返回的 PharFileInfo 对象
    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 &#39;==================&#39;, 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 after login

首先,依然是实例化一个 Phar 类,然后我们给它像数组一样增加属性,这样,属性内容就被打包进了 .phar 文件中。通过直接查看 my.phar 文件,我们可以看出,myfile1.txt 这两个属性直接被写成了文件进行保存了,也就是说,它帮我们将文本转化成文件并打包在了 my.phar 这个压缩包文件中了。

compress() 方法则是将当前的这个 Phar 对象压缩存储为某个格式的文件。这里我们直接压缩了 Bzip2 和 GZ 文件。调用这个方法后直接就会生成对应的压缩文件。

Phar 对象在遍历时产生的对象是 PharFileInfo 对象,它拥有很多类似于 File 的文件操作函数。大家可能在官方文档中找到相关的说明。

假设我们遍历 my.phar.gz ,内容依然可以正常输出,但循环中的 isCompressed() 判断都依然会是 false ,难道文件没有被压缩吗?其实,我们需要通过另一个函数来让所有文件都进行统一格式的压缩。

$p = new Phar(&#39;./my.phar&#39;, 0 ,&#39;my.phar&#39;);
$p->compressFiles(Phar::GZ);
unset($p);

$decompressPhar  = new Phar(&#39;./my.phar.gz&#39;, 0 ,&#39;my.phar&#39;);
foreach($decompressPhar as $file){
    // $file 是返回的 PharFileInfo 对象
    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 &#39;==================&#39;, 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 after login

使用 compressFiles() 对整个 .phar 中的所有文件进行了统一的格式压缩之后,再打印时 isCompressed() 就会返回对应格式的 true 了。

数据格式 Phar

最后,如果只是为了打包压缩功能的话,我们没必要使用 Phar 类。Phar 类最主要的还是用来打包能够运行的 PHP 源码,也就是它的 createDefaultStub() 方法非常重要。而如果只是打包普通文件的话,我们并不需要这个方法,这时,我们就可以使用另外一个 PharData 类来进行数据的打包压缩。使用方法和 Phar 类是一模一样的。同时,PharData 类可以直接打包成 tar 之类的文件。

$p = new PharData(&#39;./myData.tar&#39;);
$p[&#39;myfile1.txt&#39;] = &#39;hi1&#39;;
$p[&#39;myfile2.txt&#39;] = &#39;hi2&#39;;

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 &#39;==================&#39;, 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 after login

总结

说实话,Phar 真的是一个冷门项目,但是在某些情况中又非常有用,比如它虽然在代码包领域被 Composer 打败了,但是它又可以成为 Composer 的安装包,也就是说,没有 Phar 你就安装不了 Composer 。而做为压缩工具,虽然有强大的实力但使用的却也非常的少。因此,我们还是以了解为目的,如果感觉某些场景非常合适的话,也完全可以深入的研究拿来放到我们的实际项目中使用。毕竟它是 PHP 的一部分,不需要任何的编译安装及其它支持,非常原生。

测试代码:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202007/source/%E7%AE%80%E5%8D%95%E4%BA%86%E8%A7%A3Phar%E4%BB%A3%E7%A0%81%E6%89%93%E5%8C%85%E5%B7%A5%E5%85%B7%E7%9A%84%E4%BD%BF%E7%94%A8.php
Copy after login

推荐学习:php视频教程

The above is the detailed content of How to use Phar code packaging tool in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template