Detailed explanation of "PHP" phar file_ Phar file format_ Call phar class method to generate phar file

Introduction to phar

Phar, the full name is PHP Archive. The phar extension provides a way to put the entire PHP application into a. Phar file for easy movement and installation The biggest feature of phar file is a convenient way to combine several files into one file. Phar file provides a method to distribute and run complete PHP programs in one file.

You can compare a phar file to a compressed file

phar demo

Note: the default phar extension is read-only mode. You need to manually configure phar.readonly= Off in php.ini

Unable to use ini_set modification
"Pit record" PHP - use ini_set() cannot modify phar.readonly

<?php
    $phar = new Phar('demo.phar');
    $phar['demo.php'] = '<?php echo 1;?>';
    include('phar://./demo.phar/demo.php'); //1
?>

The phar file can be read using the phar: / / pseudo protocol

The source code is as follows:

phar file format

PHP: Ingredients of all Phar archives, independent of file format - Manual

stub

stub is the header of the phar file in the format... <? php ...;__ HALT_ COMPILER();?>,… It can be any character, including leaving blank, and there can be no more than one space character between the PHP closing character and the last semicolon. In addition, the PHP closure can also be omitted.

The statements contained in the php code block are executed when the phar file is included

<?php
	$phar = new Phar('demo.phar');
	$phar->setStub('<?php echo \'in stub!\';__HALT_COMPILER();?>');
	include('phar://demo.phar');	// in stub!
?>

The shortest closing character stub not to be omitted is <? php __ HALT_ COMPILER();?>

manifest describing the contents

This area stores the attribute information of phar package, allowing each file to specify file compression, file permissions, and even user-defined metadata, such as file users or groups.

PHP: Phar File Format - Manual

file contents

Compressed user added file content

[optional] signature

Optional. Signature of phar file. MD5, SHA1, SHA256, SHA512 and OPENSSL are allowed

How to generate a phar file?

Instantiate phar class

The phar class provides operations on phar files

Usually you just need to pass in the file name

<?php
	$phar = new Phar('demo.phar');
?>

Create stub

There are two main ways to create a stub:

Custom creation

Call the class method phar:: setsub ($string) to create a custom stub for the instance

<?php
	$phar = new Phar('demo.phar');
	$phar->setStub('<?php echo \'in stub!\';__HALT_COMPILER();?>');
	include('phar://demo.phar');	// in stub!
?>

Use default stub

Call the class method Phar::setDefaultStub() to set the default stub for the instance, and use the method Phar::getStub() to get the stub of the instance

<?php
    $phar = new Phar('demo.phar');
    $phar->setDefaultStub();
    print_r($phar->getStub()); // 2, 'c' => 'text/plain', 'cc' => 'text/plain', ...
?>
If you create a stub by default, PHP uses the default stub
<?php
	$phar = new Phar('demo.phar');
	$phar['demo.txt'] = 'demo';
	print_r($phar->getStub()); // 2, 'c' => 'text/plain', 'cc' => 'text/plain', ...
?>

[optional] add custom metadata

The manifest data will be generated automatically according to the configuration information. This step is to add custom metadata (meta data), so it is optional

Too much metadata can cause performance problems

Call the class method Phar::setMetadata() to set the default stub for the instance, and use the method Phar::getMetadata() to get the stub of the instance

<?php
    $phar = new Phar('demo.phar');
    $metadata = array('demo'=>1);
    $phar->setMetadata($metadata);
    print_r($phar->getMetadata()); // Array ( [demo] => 1 )
?>

[optional] add file

There are several ways to add files:

Manually select Add existing files

Call class method Phar::addFile($filepath,$localpath =?) Add a file. The parameters are the absolute path of the file and (optional) the relative path stored in phar

<?php
	$phar = new Phar('demo.phar');
	$phar->addFile('test.php');
	include('phar://demo.phar/test.php') // in test.php
?>

Add file content as string

Call the class method Phar::addFromString($localpath,$contents) to add the file as a string

<?php
	$phar = new Phar('demo.phar');
	$phar->addFromString('test.php','<?php echo \'in test.php\'?>');
	include('phar://demo.phar/test.php'); // in test.php
?>

Add empty directory

Call the class method Phar::addEmptyDir($dirname) to add an empty directory, and use the method Phar::getContent() to obtain the file structure

<?php
	$phar = new Phar('demo.phar');
	$phar->addEmptyDir('test'); // demo.phar/test/
?>

Manually select Add existing directory

Call the class method Phar::buildFromDirectory($dir,$pattern = "") to add the entire directory

<?php
	$phar = new Phar('demo.phar');
	$phar->buildFromDirectory('test'); // test.php in test/
	include('phar://demo.phar/test/test.php'); // in test/test.php
?>

Add from iterator

Call the class method phar:: buildfromiterator ($iterator$ Basedirectory = null) added from iterator

PHP: Phar::buildFromIterator - Manual

[optional] manually add supported signatures

The default is automatic signature, based on SHA-1 algorithm

<?php
	$phar = new Phar('demo.phar');
	$phar->addFromString('test.php',1);
print_r($phar->getSignature()); // Array ( [hash] => F... [hash_type] => SHA-1 )
?>

Call class method phar:: setsignaturealgorithm (int $algo? String $privatekey = null) add signature

PHP: Phar::setSignatureAlgorithm - Manual

[optional] improve performance

Because each operation on the phar file will cause the re creation of the whole phar file on the physical storage, it will bring performance problems to deal with the phar file containing a large number of files. Therefore, using the buffer to modify and write to the physical storage after completion can reduce the re creation of files on the physical storage and write at one time, so as to improve the performance

PHP: Phar::startBuffering - Manual

After instantiating the phar class, call the method Phar::startBuffering() and Phar::stopBuffering to create the buffer, and create and add operations in the buffer area.

<?php
	$phar = new Phar('phar.phar');
	$phar->startBuffering();
	$phar->setStub('<? __HALT__COMPILER();?>');
	$phar->addFromString('test.php','<?php echo \'in test.php\'?>');
	$phar->stopBuffering();
?>

reference resources

PHP: Phar - Manual

finish

Tags: PHP

Posted on Sun, 05 Sep 2021 02:00:09 -0400 by mingmangat