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 is in a directory called MyProject
and it has these two files in it:
|-- createPhar.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
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
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.
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With