Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain how to create PHP's Phar stubs

Tags:

php

stub

phar

I'm trying to create a very simple PHP CLI application that can be run as a phar file from the command line:

# php myProject.phar

This is what I've tried so far:

My Project

My project is in a directory called MyProject and it has these two files in it:

 |-- createPhar.php
 `-- bootstrap.php

bootstrap.php

The bootstrap.php file contains this:

<?php
print phpversion() . PHP_EOL;
print 'i am some script' . PHP_EOL;

When I run this script from my Ubuntu command line:

# cd MyProject 
# php bootstrap.php

I get the following output:

5.3.2-1ubuntu4.9
i am some script

createPhar.php

The createPhar.php file is meant to turn the project into Phar archive. It looks like this:

<?php    
$phar = new Phar('MyProject.phar');
$phar->addFile('bootstrap.php');
$phar->setStub( $phar->createDefaultStub('bootstrap.php') );

When I run that script...

# php createPhar.php

... a new file called MyProject.phar is created in my project's directory.

|-- bootstrap.php
|-- createPhar.php
`-- MyProject.phar

Now here's the problem

When I run the phar file...

# php MyProject.phar 

...I expect to see the same the same output that I got when when I ran the bootstrap.php script.

Instead I see nothing. No output at all. This implies that my bootstrap.php script is not being included by the default stub that was created by $phar->createDefaultStub('bootstrap.php')

I think I am misunderstanding how Phars and their stubs are being created. Could you, please, explain where I have gone wrong.

like image 980
JW. Avatar asked Jun 13 '11 20:06

JW.


People also ask

How to create a phar file?

Steps. Simply create a folder for your project, before creating a folder within there called "app" for your application's source code. Copy all of your PHP files into the app folder and make the "entrypoint" file called main. php (or you can call it anything you like and modify the script below).

What is Phar file in PHP?

In software, a PHAR (PHP Archive) file is a package format to enable distribution of applications and libraries by bundling many PHP code files and other resources (e.g. images, stylesheets, etc.) into a single archive file. PHP Archive.


1 Answers

To answer my own question.

The method outlined in my question, above, is one correct way to create a phar / phar stub.

The reason why it did not work for me and did work for Mario (see his comment below the question), is because I had Suhosin installed and needed to tweak the settings.

Fixed using the technique outlined here:

To fix, put:

suhosin.executor.include.whitelist="phar"

in /etc/php5/cli/php.ini

like image 65
JW. Avatar answered Oct 03 '22 11:10

JW.