what i need to consider when building phar file from php application use php framework like laravel

StackOverflow https://stackoverflow.com/questions/22731737

質問

I am php developer use laravel-4 as framework for building web applications , in the last few days I wanted to create phar file from my web application created on laravel framework .

I searched in the web for tools build php archive files (.phar) and I found PHP box , this tool is very good and use json configuration file for building the phar files but i could not use it for creating my phar files because there is many considerations when creating phar file from a web application use a framework like laravel . my questions are :

laravel use composer autoloader as auto loading mechanism

1- how to handle composer auto loading mechanism ?

2- how to handle the framework bootstrapping process ? 'like laravel'

3- what i need to make the browser read my index page from inside the phar file ?

4- how to use framework command line tools from the phar file ? 'like laravel-artisan'

役に立ちましたか?

解決

You might use composer.json in your application and require box there. When your application runs on laravel, you know that your bootstrap works and would also work inside a phar.

I believe Box brings a lot of the composer autloading stuff itself, so you won't run into trouble with it. I think the class_map gets included automatically.

One thing to consider is, that configuration details must be passed in!

In general, you need to "forward" to your application, which is inside the phar, like so:

<?php
require_once "phar://myapp.phar/frontcontroller.php"; // maybe index.php
$config = array('dsn' => 'database-config');
Application::run($config);

Also accessing a PHAR in a PHAR is a problem!

You can't access a PHAR packaged in a PHAR directly. Firstly you need to extract the packaged PHAR, secondly do the forwarding call and pass the CLI commands along. Problem solved here: https://stackoverflow.com/a/13537329/1163786

Full Example

box.json.dist

{
  "main": "bootstrap.php",
  "output": "application.phar",
  "compactors": ["Herrera\\Box\\Compactor\\Composer"],
  "chmod": "0755",
  "directories": ["src/"],
  "stub": true
}

bootstrap.php

<?php
require 'vendor/autoload.php'; //<-- this is autoload.php generated by Composer

use MyApp\Application;

$config = parse_ini_file(__DIR__.'/config.ini');

$app = new Application();
$app->run($config);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top