Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Web Application (ZF2) to PHAR

I know how to convert php file into phar file.

$phar = new PHAR('app.phar');
$phar->addFile('file1.php');
$phar->addFile('file2.php');

I thought it can be useful only for converting POPO to PHAR.

My question

Is it possible to convert entire Zend framework 2 web application to PHAR file? if yes, then How?

(or)

How to package Zend framework2 web application into a package? (except .zpk(zend studio package file) file format)


Updated: creating Phar from entire application

create-phar.php

$basePath = /path/to/;
$path = /path/to/application;  
$phar = new Phar ('project.phar');    
$phar->buildFromIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)), $basePath);
$phar->setStub("<?php 
    Phar::webPhar('project', 'public/index.php');
       include 'phar://project/public/index.php';
       __HALT_COMPILER();
    ?>");

index.php

<?php
include 'project.phar';
?>

Question:

When i run index.php it do not redirect to actual public/index.php file instead it shows blank page on the browser. How can I resolve this problem?

Thanks in advance

like image 603
codeninja.sj Avatar asked Jul 22 '14 08:07

codeninja.sj


1 Answers

I deploy phorkie as a .phar file.

There are some things to consider:

  1. Pack up all files in the .phar - php and static asset files (js, css, png, jpg), too. I use phing to do that. Do not forget the dependencies.
  2. You need a stub file that runs Phar::webPhar() which takes care of sending out HTTP headers
  3. Write your own .htaccess rewrite rule interpreter, because Phar::webPhar() does not do nice-path resolving on its own. I did that already.
  4. Manually take care of sending out caching headers for static files
  5. Hope that you (or your users) don't run into one of the Phar::webPhar bugs:
    1. Symlinked files
    2. No HEAD, PUT or DELETE support
    3. Redirection loop with FPM (already fixed in git)
  6. Hope that your user's web server handles .phar files (it will not; neither Debian nor Fedora ship that configuration)
  7. Hope that the user's web server is correctly configured to handle .phar files with PATH_INFO appended (/path/to/file.phar/foo/bar.png), like in http://p.cweiske.de/122

Getting phorkie running from the .phar took me the evenings of two weeks. Now that it basically works it still is not a drop-phar-and-play solution, because the default configuration of web servers just doesn't play nice with .phar.

like image 80
cweiske Avatar answered Oct 10 '22 15:10

cweiske