How to use the archive format phar for PHP development

php中世界最好的语言
Release: 2023-03-18 07:40:01
Original
1857 people have browsed it

Let me introduce to you the concept and usage of phar files developed by PHP. We know that a PHP application is often composed of multiple files. It is very convenient if they can be concentrated into one file for distribution and operation. Examples like this There are many, but they are mainly designed for PHP's Web environment. Unlike JAR archives, Phar archives can be processed by PHP itself, so there is no need to use additional tools to create or use it. It can be created or extracted using a PHP script. phar is a compound word composed of PHP and Archive. It can be seen that it means PHP archive file.

Phar archive files come in three formats: tar archive, zip archive, and 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.

Phar format archive files can be executed directly. Its generation relies on the Phar extension and is generated by a php script written by yourself.

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, and then was added to the PEAR library in 2005. . Since pure PHP solutions to this problem were very slow in practice, it was rewritten in 2007 as a pure C language extension, while adding support for ArrayAccess Object traversal Phar archives using SPL. Since then, a lot of work has been done to improve the performance of Phar archives.

Phar extension depends on the php stream wrapper. For this, please refer to the previous article PHP Streams, wrapper wrapper concepts and usage examples

Many php applications are distributed in phar format And running, the famous dependency management: composer, unit testing: phpunit, let's take a look at how to create, run, extract and restore.

Creation of phar file:

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, then the user script cannot be turned off, so it is set here to off to show examples.

Let's create a project. Create the 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.php
Copy after login


The file folder There are two js and css files with empty content. It only demonstrates that phar can contain multiple file formats

lib_a.php content is as follows:

<?php
/**
 * Created by yunke.
 * User: yunke
 * Date: 2017/2/10
 * Time: 9:23
 */
function show(){
  echo "l am show()";
}
Copy after login


msg The content of .html is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>phar</title>
</head>
<body>
<?=$str; ?>
</body>
</html>
Copy after login


The content of index.php is as follows:

<?php
/**
 * Created by yunke.
 * User: yunke
 * Date: 2017/2/10
 * Time: 9:17
 */
require "lib/lib_a.php";
show();
$str = isset($_GET["str"]) ? $_GET["str"] : "hello world";
include "template/msg.html";
Copy after login


##The content of Lib.php is as follows:

<?php
/**
 * Created by yunke.
 * User: yunke
 * Date: 2017/2/10
 * Time: 9:20
 */
function yunke()
{
  echo "l am yunke()";
}
Copy after login


The project file is ready. Start creating it. Now create a yunkeBuild.php in the same directory as the project folder to generate phar format files. The content is as follows:

<?php
/**
 * Created by yunke.
 * User: yunke
 * Date: 2017/2/10
 * Time: 9:36
 */
//产生一个yunke.phar文件
$phar = new Phar(&#39;yunke.phar&#39;, 0, &#39;yunke.phar&#39;);
// 添加project里面的所有文件到yunke.phar归档文件
$phar->buildFromDirectory(dirname(FILE) . &#39;/project&#39;);
//设置执行时的入口文件,第一个用于命令行,第二个用于浏览器访问,这里都设置为index.php
$phar->setDefaultStub(&#39;index.php&#39;, &#39;index.php&#39;);
Copy after login


Then access the yunkeBuild.php file in the browser, a yunke.phar file will be generated. At this time, the server root directory structure is as follows:

project
yunkeBuild.php
yunke.phar
Copy after login


This 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 file

Use of phar archive file:

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:

<?php
/**
 * Created by yunke.
 * User: yunke
 * Date: 2017/2/8
 * Time: 9:33
 */
require "yunke.phar";
require "phar://yunke.phar/Lib.php";
yunke();
Copy after login


If there is only the first line in the index.php file, then add the following code exactly the same as when not using the archive file:

require "project/index.php";
Copy after login


If there is no second line , then the yunke() in the third line will prompt that it is undefined, so it can be seen that when requiring a phar file, not all the files in it are imported, but only the entry execution file is imported, but in actual projects, it is often in this entry file Import other files that need to be used. In this case, the entry execution file is project/index.php

Extract and restore the phar file:

We sometimes wonder about the source code of the files contained in phar. At this time, you need to restore the phar file. If you just take a look, you can use some IDE tools, such as phpstorm 10 to open it directly. If you need to modify it, you need to extract it. For demonstration, we download a composer.phar and put it in In the server directory, create a get.php file in the root directory with the following content:

<?php
/**
 * Created by yunke.
 * User: yunke
 * Date: 2017/2/9
 * Time: 19:02
 */
$phar = new Phar(&#39;composer.phar&#39;);
$phar->extractTo(&#39;composer&#39;); //提取一份原项目文件
$phar->convertToData(Phar::ZIP); //另外再提取一份,和上行二选一即可
Copy after login


Use a browser to access this file to extract it. The above example shows the two Extraction method: The second line will create a composer directory and put the extracted content into it. The third line will generate a composer.zip file. Unzip it to get the extracted and restored project file.

Supplement:

1. When deploying the phar file to the production server, you need to adjust the server configuration to avoid the browser directly downloading the phar file when accessing

2. You can Set an alias for the archive. The alias is saved permanently in the archive file. It can refer to the archive with a short name regardless of where the archive file is stored in the file system. Set the alias:

$phar = new Phar(&#39;lib/yunke.phar&#39;, 0);
$phar->setAlias ( "yun.phar");
Copy after login


After setting the alias, you can use it as follows:

<?php
require "lib/yunke.phar";
require "phar://yun.phar/Lib.php"; //使用别名访问归档文件
require "phar://lib/yunke.phar/Lib.php"; //当然仍然可以使用这样的方式去引用
Copy after login


If you do not specify an alias when making a phar file, you can also use Phar::mapPhar('yunke.phar'); to specify it in the stub file.

3. There is a stub file in the archive file. In fact, It is a piece of PHP execution code that can be set when making an archive. When the archive file is executed directly, it is actually executed, so it is a startup file; when the archive file is included in the script, it is included and run just like a normal PHP file, but When a file in the archive is directly included in phar://, the stub code will not be executed. The stub file often requires other files to be run. The restriction on the stub file is only to end with HALT_COMPILER();, which is the default The stub is designed to run without the phar extension. It extracts the contents of the phar file to a temporary directory and then executes it. However, starting from php5.3, the extension is built-in enabled by default

4. The created phar file cannot be Change, so files such as configuration files need to be placed outside the archive file.

5. mapPhar function: This function should only be called in the stub code. It can be used to set the alias when the archive alias is not set. , open a reference mapped to the phar stream


I believe you have mastered the method after reading these cases. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

How PHP solves the problem of large website traffic and high concurrency

AJAX principles and CORS cross-domain methods

Detailed explanation of javascript data types and git usage code

The above is the detailed content of How to use the archive format phar for PHP development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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