Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an executable phar?

Tags:

php

shebang

phar

I want to start a phar script as an executable, directly by doing foo.phar <params> instead of php foo.phar <params>.

like image 618
magnetik Avatar asked Jun 18 '12 11:06

magnetik


1 Answers

It's easy by modifying the default stub (adding the shebang corresponding to php).

// start buffering. Mandatory to modify stub.
$phar->startBuffering();

// Get the default stub. You can create your own if you have specific needs
$defaultStub = $phar->createDefaultStub('index.php');

// Adding files
$phar->buildFromDirectory(__DIR__, '/\.php$/');

// Create a custom stub to add the shebang
$stub = "#!/usr/bin/php \n".$defaultStub;

// Add the stub
$phar->setStub($stub);

$phar->stopBuffering();
like image 136
magnetik Avatar answered Oct 06 '22 20:10

magnetik