📜 ⬆️ ⬇️

Self-executable phar as a way to distribute web applications

As we all probably already know, PHP 5.3 introduced support for a special type of archives with the .phar extension. Those who do not know - can read a great article. Phar - executable PHP archives
The area of ​​application that immediately comes to mind is libraries / frameworks in the form of connected * .phar archives and installers of web applications, for example, CMS. I am going to talk about the latter in more detail and with examples.

Formulation of the problem


The goal is to get one file at the output, which will be itself executable, and will contain all the necessary files. If we draw an analogy with ordinary applications, this is an application that is packaged in SFX (self-extracting archive).

Requirements


The examples in the article are written for PHP 5.4 (but they can be easily corrected under 5.3), and the PEAR library Archive_Tar is used to create native * .tar archives.

Take for example the simplest structure:
     build.php
     index.php
     install.php
     readme.txt

Naturally, the install.php file is needed only at the installation stage, and build.php only at the installation stage. The readme.txt file will be created at the build stage.
')

Create tar


First, we will collect the necessary files in tar, this makes it possible to circumvent some limitations of the * .phar format, for example, the inadmissibility of Cyrillic in the file name. If you use Phar or PharData for this, you will have to rename the files to a valid view when packing, and back to the original view when unpacking.

Create a tar archive:
// PEAR  require_once 'Archive/Tar.php'; //  $tar = new Archive_Tar(__DIR__.'/system.phar.tar'); 

The file extension is specified .phar.tar in order for php to work with it.
Add files to the archive:
 //   $tar->createModify( [ 'index.php', 'install.php' ], null, __DIR__ ); //   ,        ,     . // readme.txt,         $tar->addString( 'readme.txt', "This is demo project\nBuilt ".date('dm-Y').' at '.date('H:i') ); 


Turn tar into phar


Create a so-called tar-based phar from the resulting archive.
 //   $phar = new Phar(__DIR__.'/system.phar.tar'); //  bz2 ,   .phar $phar->convertToExecutable(Phar::TAR, Phar::BZ2, '.phar'); 


Making the archive itself executable


To do this, you must specify the file that will be opened when the web server attempts to directly access the file by the web server. The default is index.php, but we have an installation, so you need to open install.php
 //   $phar = new Phar(__DIR__.'/system.phar'); //     $phar->setStub("<?php Phar::webPhar(null, 'install.php'); __HALT_COMPILER();"); // rename(__DIR__.'/system.phar', __DIR__.'/system.phar.php'); 

The strange format .phar.php plays a very important role. The .php extension forces you to transfer a file to the PHP interpreter, and .phar makes it clear that this is an archive, and not a file with source code. In fact, the archive is converted into a directory, for example, you can enter /system.phar.php/index.php in the address bar of the browser - and it will work.

Installation


Listing install.php that installs
 //   ,   ,    $root = substr(pathinfo(__DIR__, PATHINFO_DIRNAME), 7); //   2      (new Phar($root.'/'.pathinfo(__DIR__, PATHINFO_BASENAME)))->extractTo( $root, [ 'index.php', 'readme.txt' ] ); //  ( ) unlink($root.'/'.pathinfo(__DIR__, PATHINFO_BASENAME)); 

And the hero, for whom the whole kitchen was started - index.php
 echo 'I was inside the phar archive'; 

I hope that this information will be useful to someone, and more and more products will be supplied not in the form of zip / rar / tar / gz / bz2 archives, but of native phar, for which it was created.

Source files system.phar.php

Source: https://habr.com/ru/post/148773/


All Articles