简体   繁体   中英

how to convert a php script into a phar file?

The goal of the project is to convert a php script into .phar

My project is made of : 1 single index.php that calls classes into /lib and /database folders

the goal is to have as less files as possible to distribute (ideally 2 : index.php and assets.phar which will include all files from /lib and /database) or even 1 (index.phar)

I tried with empir with any success: has someone has done that before ?

Any tutorial available ?

Use the Phar class . For example, put this in compress.php :

$phar = new Phar('index.phar');
$phar->addFile('index.php');
$phar->buildFromDirectory('lib');
$phar->buildFromDirectory('database');

Run php compress.php from the command line. Voilà, you have yourself a Phar.

I use the following script:

<?php
$phar = new \Phar(__DIR__ . '/index.phar');    
$phar->startBuffering(); // For performance reasons. Ordinarily, every time a file within a Phar archive is created or modified in any way, the entire Phar archive will be recreated with the changes.  
$phar->addFile('index.php');
$phar->addFile('composer.json');
$phar->addFile('composer.lock');
$phar->buildFromDirectory(__DIR__ . '/vendor');
$phar->stopBuffering();

Run the following command to run the previous script:

$ php -dphar.readonly=0 index.php

(Creating of phar archives is disabled by default in PHP.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM