phpsymfonyphar

PHP Application in a single file


I want to create php app (with composer and some libs's) in a single file. First what i try is a phar.

It's great , but when i enter .phar (file name can be changed to .php) file in browser it redirects me to deafult stub (something.phar/www/index.php), and ok , but with some .htaccess redirect rules ( for example default .htacces for symfony 3) i got 404 error ( becouse this file don't exist's and htaccess redirects to framework )

my question's are :

  1. anyone seen solution for using single file app (phar) with symfony3 .htaccess or similar ?

  2. Is there any other solution to pack whole ip in a single php file ?

PS . creating new .php file that includes .phar don't work

PS . I'm searching for solution that dosen't touch .htaccess (without modification there )


Solution

  • While it doesn't always have sense, it sometimes does, thus I'm not gonna write you shall do it or not, totally up to you ;)

    Few hints.

    First of all, you could make it fully one-file webapp, or have bootstrap file (like index.php) + optional config excluded, and only libraries put into dedicated phar file. While one file is nice to have and super easy distribute, having dedicated index file that loads config is more flexible.

    What you need is a bit knowledge about Phar itself (http://php.net/manual/en/class.phar.php), especially a stub. Stub is entry point that is executed when file is included/required (when you create phar file as library of classes/resources), or during execution (via CLI/web, doesn't matter). Take a look at http://php.net/manual/en/phar.setdefaultstub.php ! Be aware that accessing non-php resources via phar:// protocole is slower than just getting it from drive directly, as it requires php to jump into phar:// stream (basically phar copied into memory and unpacked) and properly extract what you need. And getting php involved into retrieving static resources like CSS files is so slow. IMO do it only for small non-production tools, that requires super easy sharing. Like queue/DB manager.

    For sure you need to register autoloader of classes that phar contains, then maybe you want to also bootstrap+start application as well.

    Super small, old and crappy example of mine of approach that phar file contains only libraries, not bootstrap/config: https://github.com/keradus/PharBuilder nowadays, you probably want to use https://github.com/box-project/box2 instead.

    Note that if you gonna expose phar on your server (in any option, app or just lib), ensure to configure your server to not serve file as downloadable content, eg apache was doing that by default 5 years ago (no clue what is current behaviour)