yarek
yarek

Reputation: 12044

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 ?

Upvotes: 5

Views: 4244

Answers (2)

MartyIX
MartyIX

Reputation: 28648

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.)

Upvotes: 4

icktoofay
icktoofay

Reputation: 129011

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.

Upvotes: 6

Related Questions