Please explain how to create PHP's Phar stubs

[亡魂溺海] 提交于 2019-12-09 05:08:46

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/6336144/please-explain-how-to-create-phps-phar-stubs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!