Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Packaging into a Phar file an entire Slim Application

Tags:

php

slim

phar

I have a Slim Application with the following directory structure:

app/
vendor/
www/

config.php

In app/ I have the relevant files of the project, in vendor/ the dependencies managed by composer and in www/ the files accesible by the web server.

So I am thinking to create a Phar file along the lines of:

<?php

$full_path = '/home/.../forms/';
$package_name = 'www/package.phar';

try {
    $phar = new Phar($full_path . $package_name, 
    FilesystemIterator::CURRENT_AS_FILEINFO |       FilesystemIterator::KEY_AS_FILENAME, $full_path . $package_name);

$phar->startBuffering();

$phar->addFile($full_path . 'www/index.php');
$phar->addFile($full_path . 'www/bootstrap.php');
$phar->addFile($full_path . 'www/session_start.php');


// Grab config
$phar->addFile($full_path . 'config.php');


$phar->buildFromIterator(new RecursiveIteratorIterator (new RecursiveDirectoryIterator('../app', FilesystemIterator::SKIP_DOTS)),'../app');
$phar->buildFromIterator(new RecursiveIteratorIterator (new RecursiveDirectoryIterator('../vendor', FilesystemIterator::SKIP_DOTS)),'../vendor');

$phar->setDefaultStub('bootstrap.php', 'bootstrap.php');

$phar->stopBuffering();

echo "Phar created.";

} catch (Exception $e) {
// handle errors here
echo $e->getMessage();
}

So I create that way the phar and then I have:

deploy.php

<?php

require_once 'phar://package.phar/bootstrap.php';
$app->run();

But when accesing /deploy.php I am getting:

[Fri May 15 20:07:02 2015] [error] [client 10.0.2.2] PHP Warning: require_once(phar://package.phar/bootstrap.php) [function.require-once]: failed to open stream: Cannot open archive "/vagrant/www/package.phar", invalid alias in /vagrant/www/deploy.php on line 3 [Fri May 15 20:07:02 2015] [error] [client 10.0.2.2] PHP Fatal error: require_once() [function.require]: Failed opening required 'phar://package.phar/bootstrap.php' (include_path='.:/usr/share/php:/usr/share/pear') in /vagrant/www/deploy.php on line 3

Do you think I should be addressing it like this?

Thanks

like image 797
Alfonso Embid-Desmet Avatar asked May 11 '15 18:05

Alfonso Embid-Desmet


1 Answers

EDIT:

"Creating the phar in a shared folder of a Vagrant box caused that problem. Once I changed "output" property to a location outside the shared folder the error went away." source


You should check your php.ini: phar.require_hash = Off

Or sign your Phar with setSignatureAlgorithm()

Or with Phing: <pharpackage ... signature="sha512"> ...

like image 110
C Würtz Avatar answered Nov 01 '22 19:11

C Würtz