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.


PHP Everywhere

From PHP to PHAR

Library when writing PHP CLI tools: CLImate - PHP’s best friend for the terminal.

A za Phar koristi box-project/box: 📦🚀 Fast, zero config application bundler with PHARs.. Inače, Phar je podržan standardno svugde. A pogledaj i clue/phar-composer: Simple phar creation for every PHP project managed via Composer

PHP to EXE Compiler

List of all of them: Free PHP Compilers: compile PHP scripts to native code, .NET or Java bytecode | thefreecountry.com. Wapache je samo bundlovani web server i PHP.

Ovaj baš obećava ali za male CLI ircmaxell/php-compiler: A compiler. For PHP i radi gde god postoji PHP a to je svugde. Izgleda super jednostavno ali je i dead.

but only one is very much alive: PeachPie | PHP compiler to .NET i savršen je! Pritom radi i sa Phar falovima: Phar archives - PeachPie Documentation

WpDotNet | WordPress on .NET Core on iolevel/peachpie-wordpress: WordPress running on .NET Core.

date 15. Feb 2023 | modified 10. Jun 2024
filename: PHP » Single-file Phar Executables