18
votes

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.

2
Your example does work for me. But try the ->setDefaultStub shortcut (over ::createDefault and ::setStub) for testing.mario
Thanks mario. I tried using ->setDefaultStub and it still did not work either. Its weird that you got it to work and I haven't. I suppose the problem is not to do with the code then.JW.
My PHP_VERSION is 5.3.3-1ubuntu9.5, but there was only one minor security bug fix related to the phar extension in between. It must be something else. Did you look at the resulting file with a hexeditor? Is the bootstrap content anywhere in it? (I got a plain php script with some admixed binary gibberish.)mario
Yep. Had a look at MyProject.phar and there is bootstrap code plus some gibberish in it. Thanks for your suggestions. I suspect I am doing something really stupid somewhere along the line - so gonna sleep on it and re-try tomorrow.JW.

2 Answers

11
votes

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

0
votes

you could also do it like this:

bootstrap.php;

<?php
function go() {
    print phpversion() . PHP_EOL;
    print 'i am some script' . PHP_EOL;
}

then:

php -r "require 'phar://Myproject.phar'; go();"

or don't have a function and it will execute whatever commands you have in there, but typically you would have some functions or class files in the phar.