Concepts and usage of phar files in archive format for PHP development [Create, use, unpack and restore and extract ],

Source: Internet
Author: User
Tags unpack jquery library

Concepts and usage of phar files in archive format for PHP development [Create, use, unpack and restore and extract ],

This document describes the concept and usage of archive phar files in PHP development. We will share this with you for your reference. The details are as follows:

A php application is often composed of multiple files. It is very convenient to centralize them into one file for distribution and operation. There are many such columns, for example, the installation program and a jquery library on the Windows operating system are used. To achieve this, php adopts the phar document file format. This concept is derived from the java jar, however, the PHP Web environment is designed. Unlike JAR archiving, Phar archiving can be processed by PHP itself. Therefore, no additional tools are required for creation or use, you can use a php script to create or extract it. Phar is a synthesis word consisting of PHP and Archive. It can be seen that it is a php Archive file.

For more information about phar, see http://php.net/manual/zh/book.phar.php.

There are three types of phar archive files: tar archive, zip archive, and phar archive. The first two types of files require php to install Phar extension support and are rarely used. Here we will focus on the phar archive format.

You can directly execute an archive file in phar format. It depends on the Phar extension and is generated by a php script compiled by you.

Phar extension is not a new concept for PHP. It was built in php. It was originally written in PHP and named PHP_Archive. It was then added to the PEAR library in. In practice, the pure PHP solution to this problem is very slow. Therefore, in 2007, it was rewritten as a pure C Language extension, added support for Traversing Phar archives using ArrayAccess objects of SPL. Since then, people have done a lot of work to improve the performance of Phar archiving.

Phar extensions depend on php stream wrapper. For more information, see the previous article on PHP stream Streams and wrapper concepts and usage examples.

Many php applications are distributed and run in phar format. The famous ones are dependency management: composer and unit test: phpunit. Let's take a look at how to create, run, and extract and restore.

Create a phar file:

First in php. modify phar in ini. readonly option, remove the semicolon and change the value to off. For security reasons, this option is on by default. ini is disabled (the value is 0 or off), so it can be enabled or disabled in the user script. if ini is enabled, the user script cannot be closed. Therefore, set it to off to show the example.

Let's create a project. In the root directory of the server, set the project folder to project. The structure in the directory is as follows:

file  -yunek.js  -yunke.csslib  -lib_a.phptemplate  -msg.htmlindex.phpLib.php

The file folder contains two Empty js and css files. It only shows that phar can contain multiple file formats.

The 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()";}

The content of msg.html is as follows:

<!DOCTYPE html>

The index. php content 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";

The Lib. php content is as follows:

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

The project file is ready to be created. Now, a yunkeBuild. php file is created in the same directory of the project folder to generate a phar file. The content is as follows:

<? Php/*** Created by yunke. * User: yunke * Date: * Time: * // generates a yunke. phar File $ phar = new Phar ('yunke. phar ', 0, 'yunke. phar '); // Add all files in the project to yunke. phar archive FILE $ phar-> buildFromDirectory (dirname (_ FILE __). '/Project'); // set the entry file for execution. The first file is used for command line and the second file is used for browser access. index is set here. php $ phar-> setDefaultStub ('index. php', 'index. php ');

Access the yunkeBuild. php file in the browser, and a yunke. phar file is generated. The root directory structure of the server is as follows:

projectyunkeBuild.phpyunke.phar

This is the simplest process of generating a phar archive file. For more information, see the official website. Note that if the project does not have a single execution portal, you should not use the phar archive file.

Use of phar archive files:

Create an index. php file in the root directory of the server 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();

If the index. php file contains only the first line, add the following code exactly the same as if the archive file is not used:

require "project/index.php";

If there is no second line, the yunke () in the third line will prompt undefined. Therefore, when a phar file in require is not imported, however, only the entry execution file is imported, but in actual projects, other files are often imported into the entry file. In this example, the entry execution file is project/index. php

Extraction and restoration of phar files:

Sometimes we will be curious about the file source code contained in phar. At this time, we need to restore the phar file. If we just have a look at it, we 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. put phar in the server directory and create a get. PHP file with the following content:

<? Php/*** Created by yunke. * User: yunke * Date: * Time: */$ phar = new Phar ('composer. phar '); $ phar-> extracloud ('composer'); // extract an original project file $ phar-> convertToData (Phar: ZIP ); // extract another copy, and select either from the upstream version.

Access the file in a browser and extract the file. The preceding columns show two extraction methods: the second row creates a composer, and stores the extracted content. The third row extracts and restores a composer.zip file.

Supplement:

1. You need to adjust the server configuration when deploying the phar file to the production server to avoid directly downloading the phar file from the browser when accessing the server.

2. You can set an alias for an archive object. The alias is permanently saved in the archive object. It can be referenced with a short name, regardless of where the archive object is stored in the file system, set alias:

$phar = new Phar('lib/yunke.phar', 0);$phar->setAlias ( "yun.phar");

The alias can be used as follows:

<? Phprequire "lib/yunke. phar "; require" phar: // yun. phar/Lib. php "; // use an alias to access the archive file require" phar: // lib/yunke. phar/Lib. php "; // Of course, you can still reference it in this way

If you do not specify an alias when creating a phar file, you can also usePhar::mapPhar('yunke.phar');Specify

3. There is a stub file in the archive file, which is actually a piece of php code to execute. You can set it when creating the archive file. When you directly execute the archive file, you actually execute it, so it is the Startup File; when a script contains an archive file, it will include it and run it like a normal PHP file, but it will not execute the stub code when it directly contains a file in phar, the require file in the stub file usually contains other files to be run.__HALT_COMPILER();The default stub is designed to run without a phar extension. It extracts the phar file content to a temporary directory and then executes it. However, the extension is enabled by default starting from php5.3.

4. The created phar file cannot be modified. Therefore, files such as configuration files must 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 and map a reference to the phar stream.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.