Creating Phar PHP Files

Creating Phar PHP Files

There are lots of tools for that from plain phar pack -f to lots of others. Za jednostavne skripte, dovoljno je:

phar pack -f example.phar -i example.php
chmod +x example.phar

Ali problem je malo u slučaju da imaš Autoloader odnosno da koristiš Composer.

Box

Bez dileme ovo je najkompletniji tool. Ima čak mogućnost i kreiranja Docker fajla za Phar

Box is really polished application bundler with PHARs with repo at box-project/box. Definitivno je najbolji, ali mu je potreban PHP8 za rad.

Instaliraj sa:

wget https://github.com/box-project/box/releases/latest/download/box.phar
chmod +x box.phar

I pokreni sa:

php box.phar compile

phar-composer

clue/phar-composer is Phar creation tool for every PHP project managed via Composer, and it’s introductionary article

Add line

“bin”: [“main.php”]

inside composer.json and recreate composer.lock with composer update

Then execute:

php phar-composer.phar build .

Autoload aka phpab

phpab.net aka theseer/Autoload is lightweight phar archive builder.

wget https://github.com/theseer/Autoload/releases/download/1.27.1/phpab-1.27.1.phar -O phpab.phar
chmod +x phpab.phar

Run with:

php phpab.phar --phar --output example.phar --basedir . composer.json

Ali nikakve rezultate nisam dobio, odnosno jesam fajl “example.phar” koji ništ ane radi.

Others

  • Didn’t try Empir at all

Running Phar as normal PHP file on server

Create a “runner” script on your web server with the following content:

<?php include "phar://example.phar/anyfile.php";

or

<?php include "example.phar";

Perfect solution to avoid haveing to reconfigure server to handle .phar files same as .php files, and if you’re on a shared hosting you probably can’t change server at all

Sources are Using Phar Archives and Packaging Your Apps with Phar

Extract files from PHAR

Simple as:

phar extract -f example.phar

Box can do that also easily.

date 15. Feb 2023 | modified 29. Dec 2023
filename: PHP » Single-file Phar Executables