Running a phar without .phar extension

I'm shipping a new side project as .phar file, php-sqllint.phar.

The phar stub has a shebang line so that I can call it without explicitly running PHP:

When making the file executable with chmod +x, you can simply execute it:

$ ./php-sqllint.phar --version
./php-sqllint.phar version 0.0.2.

No extension

Then I wanted to get rid of the .phar extension, because

$ ./php-sqllint file.sql

reads nicer than

$ ./php-sqllint.phar file.sql

Simply renaming the phar file resulted in an error:

PHP Warning: require(phar:///usr/bin/php-sqllint/bin/phar-php-sqllint.php): failed to open stream: phar error: invalid url or non-existent phar "phar:///usr/bin/php-sqllint/bin/phar-php-sqllint.php" in /usr/bin/php-sqllint on line 15

I found the solution to this problem in phpunit bug #734 : Use Phar::mapPhar() in the stub and include the following code with phar://$alias:

#!/usr/bin/env php
<?php
Phar::mapPhar('php-sqllint.phar');
require 'phar://php-sqllint.phar/bin/phar-php-sqllint.php';
?>

You have to use the same alias name that was used to create the phar file.

Written by Christian Weiske.

Comments? Please send an e-mail.