问题描述
我正在尝试使用我的一个lib创建一个Phar存档. lib只是一类归类为文件夹和子文件夹的类.这里根本没有index.php,只有一个静态Config类可以调用以启动自动加载器.
I'm trying to make a Phar archive with one of my lib. The lib is just a bunch of classes organized into folders and subfolders. No index.php at all here, just a static Config class to call to initiate the autoloader.
无论如何,我建立了这样的档案:
Anyway, I built a archive like this :
$phar = new Phar(__DIR__ . '/lis.phar',0,'lib.phar');
$phar->buildFromDirectory(__DIR__ . '/class','/\.php$');
$phar->stopBuffering();
之后,我尝试使用这样的phar:
After that I'm trying to use the phar like this :
require('lib.phar');
Config::register(); // Config is in the phar
但是出现以下错误:
如何在其中没有任何index.php文件的情况下制作phar存档?实际上,我只需要将存档文件作为文件的容器,而无需自动执行任何操作.
How can I make a phar archive without any index.php file inside it ? In fact I just need the archive to be a container for my files, no need to auto execute anything.
推荐答案
首先,我认为您必须在stopBuffering()
之前先startBuffering()
.我可能认为buildFromDirectory
在内部为您完成此操作.您无需执行stopBuffering()
即可封存"存档.它已经准备就绪,可以运行中".
First of all, i think you have to startBuffering()
before stopBuffering()
. And I might think that buildFromDirectory
does this internally for you.You don't need to do stopBuffering()
for "sealing" the archive. Its ready "on the fly".
第二步:您可以像这样观看defaultStub(在代码中隐式使用):
So second: You can watch the defaultStub (which is used in your code implicity) like this:
$phar->setDefaultStub();
var_dump($phar->getStub());
它有点神秘,但是您会发现的.它会检查是否支持phar流包装器(在5.3中),如果不支持,则将内容提取到临时文件中,然后执行Phar::START
常量文件-默认情况下为"index.php".当然,它会执行Phar::interceptFileFuncs()
并设置包含路径,这会使phar发挥魔力".但是您的问题听起来像您只需要一个lib存档.因此,最好使用"PharData"类.还没有尝试过,但是文档上是这样说的.
Its a littly bit cryptic, but you will figure it out. It does check for phar stream wrapper support (in 5.3) and if not it extracts the contents to temp file and then executes the Phar::START
constant File - which is by default "index.php". And of course it does Phar::interceptFileFuncs()
and sets the include path, which makes the phar working "magic". But your question sounds like you only need an archive for your libs. So you're better off with using the "PharData" class. Haven't tried it yet, but the documentation says so.
这篇关于如何创建没有索引的Phar存档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!