Home>Article>Backend Development> How PHP operates phar files
This time I will show you how to operate phar files with PHP. What are theprecautions for operating phar files with PHP? The following is a practical case, let's take a look.
This article describes the concept and usage of the archive format phar file in PHP development. Share it with everyone for your reference, the details are as follows: A PHP application is often composed of multiple files. It is very convenient if they can be concentrated into one file to distribute and run. There are many such examples, such as the installation program on the window operating system, a jquery library, etc. Etc., in order to achieve this, PHP uses the phar document file format. This concept is derived from Java's jar, but it is mainly designed for PHP's web environment, unlike JAR archives, Phar archives can be Handled natively, so no additional tools are needed to create or use it, just use a php script to create or extract it. phar is a compound word composed of PHP and Archive composition, you can see that it means php archive file. For the official website documentation of phar, please see http://php.net/manual/zh/book.phar.php. This document can be seen as complementary to the official website documentationThere are three formats of phar archive files: tar archive, zip archive, phar archive. The first two types of execution require Phar to install Phar extension support, and are rarely used. Here we mainly talk about the phar archive format.
The phar format archive file can be executed directly. Its generation relies on the Phar extension and is generated by a php script written by yourself. The Phar extension is not a new concept to PHP. It has been built into PHP in php5.3. It was originally written in PHP and named PHP_Archive was then added to the PEAR library in 2005. Since pure PHP solutions to this problem are very slow in practice, 2007 Rewritten as a pure C language extension, and adding ArrayAccess using SPLObject traversalPhar Archiving support. Since then, a lot of work has been done to improve the performance of Phar archives.
The Phar extension depends on the php stream wrapper. For this, please refer to the previous article PHP Streams and wrapper wrapper concepts and usage examples for detailed explanation Many PHP applications are distributed and run in phar format. The famous ones include dependency management: composer,Unit test:phpunit. Let's take a look at how to create, run, extract and restore.
Creation of phar files:
First modify the phar.readonly option in php.ini, remove the preceding semicolon, and change the value to off. Due to security reasons, this option defaults to on. If it is disabled in php.ini (the value is 0 or off) , then it can be turned on or off in the user script. If it is turned on in php.ini, the user script cannot be turned off, so it is set to off here to show the example. Let's create a project. Create a project folder in the server root directory as project. The structure in the directory is as follows:file -yunek.js -yunke.css lib -lib_a.php template -msg.html index.php Lib.phpThe file folder has two js and css files with empty contents. It only demonstrates that phar can contain multiple file formats The content of lib_a.php is as follows:
The content of msg.html is as follows:The content of index.php is as follows:phar =$str; ?>The content of Lib.php is as follows:The project file is ready and started to be created. Now create a yunkeBuild.php in the same directory as the project folder to generate a phar format file. The content is as follows:buildFromDirectory(dirname(FILE) . '/project'); //设置执行时的入口文件,第一个用于命令行,第二个用于浏览器访问,这里都设置为index.php $phar->setDefaultStub('index.php', 'index.php');Then access this yunkeBuild.php file in the browser, a yunke.phar file will be generated. At this time, the server rootdirectory structureis as follows:
project yunkeBuild.php yunke.pharThis is the simplest process to generate a phar archive file. For more information, please see the official website. It should be noted here that if the project does not have a single execution entry, it is not appropriate to use the phar archive fileUsage of phar archive files:
We create an index.php file in the server root directory to demonstrate how to use the phar file created above. The content is as follows:If there is only the first line in the index.php file, then add the following code exactly the same as when not using an archive file:require "project/index.php";如果没有第二行,那么第三行的yunke()将提示未定义,所以可见require一个phar文件时并不是导入了里面所有的文件,而只是导入了入口执行文件而已,但在实际项目中往往在这个入口文件里导入其他需要使用的文件,在本例中入口执行文件为project/index.php
phar文件的提取还原:
我们有时候会好奇phar里面包含的文件源码,这个时候就需要将phar文件还原,如果只是看一看的话可以使用一些ide工具,比如phpstorm 10就能直接打开它,如果需要修改那么就需要提取操作了,为了演示,我们下载一个composer.phar放在服务器目录,在根目录建立一个get.php文件,内容如下:
extractTo('composer'); //提取一份原项目文件 $phar->convertToData(Phar::ZIP); //另外再提取一份,和上行二选一即可用浏览器访问这个文件,即可提取出来,以上列子展示了两种提取方式:第二行将建立一个composer目录,并将提取出来的内容放入,第三行将产生一个composer.zip文件,解压即可得到提取还原的项目文件。
补充:
1、在部署phar文件到生产服务器时需要调整服务器的配置,避免当访问时浏览器直接下载phar文件
2、可以为归档设置别名,别名保存在归档文件中永久保存,它可以用一个简短的名字引用归档,而不管归档文件在文件系统中存储在那里,设置别名:
$phar = new Phar('lib/yunke.phar', 0); $phar->setAlias ( "yun.phar");设置别名后可以如下使用:
如果在制作phar文件时没有指定别名,也可以在存根文件里面使用
Phar::mapPhar('yunke.phar');
指定3、归档文件中有一个存根文件,其实就是一段php执行代码,在制作归档时可以设置,直接执行归档文件时,其实就是执行它,所以它是启动文件;在脚本中包含归档文件时就像包含普通php文件一样包含它并运行,但直接以phar://的方式包含归档中某一个文件时不会执行存根代码, 往往在存根文件里面require包含要运行的其他文件,对存根文件的限制仅为以
HALT_COMPILER();
结束,默认的存根设计是为在没有phar扩展时能够运行,它提取phar文件内容到一个临时目录再执行,不过从php5.3开始该扩展默认内置启用了4、制作的phar文件不能被改动,因此配置文件之类的文件需要另外放置在归档文件外面
5、mapPhar函数:这个函数只应该在stub存根代码中调用,在没有设置归档别名的时候可以用来设置别名,打开一个引用映射到phar流
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of How PHP operates phar files. For more information, please follow other related articles on the PHP Chinese website!