本文介绍了请说明如何创建PHP的Phar存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个非常简单的PHP CLI应用程序,该应用程序可以作为 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:

我的项目位于名为MyProject的目录中,并且其中包含以下两个文件:

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

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

bootstrap.php

bootstrap.php文件包含以下内容:

bootstrap.php

The bootstrap.php file contains this:

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

当我从Ubuntu命令行运行此脚本时:

When I run this script from my Ubuntu command line:

# cd MyProject
# php bootstrap.php

我得到以下输出:

5.3.2-1ubuntu4.9
i am some script

createPhar.php

createPhar.php文件旨在将项目转换为Phar存档.看起来像这样:

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

...在我的项目目录中创建了一个名为MyProject.phar的新文件.

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

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

现在是问题所在

当我运行phar文件时...

Now here's the problem

When I run the phar file...

# php MyProject.phar

...我希望看到与运行bootstrap.php脚本时得到的输出相同的输出.

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

相反,我什么也没看到.完全没有输出.这意味着$phar->createDefaultStub('bootstrap.php')

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')

我认为我误会了Phars及其存根的创建方式.请您解释一下我出了什么问题.

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

推荐答案

回答我自己的问题.

上面我的问题中概述的方法是创建phar/phar存根的一种正确方法.

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

它对我不起作用,对马里奥 did 不起作用的原因(请参阅问题下方的他的评论),是因为我安装了Suhosin,并且需要调整设置.

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.

使用此处概述的技术:

suhosin.executor.include.whitelist ="phar"

suhosin.executor.include.whitelist="phar"

在/etc/php5/cli/php.ini中

in /etc/php5/cli/php.ini

这篇关于请说明如何创建PHP的Phar存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 09:26