简体   繁体   English

最小的PHP Phar存根

[英]Minimal PHP Phar stub

I'm currently experimenting with an extension mechanism for my framework. 我正在尝试为我的框架扩展机制。 Each module consists of at least one PHP file (defining a single class) and an XSL stylesheet but potentially several other files could be involved so I immediately thought of using Phars. 每个模块至少包含一个PHP文件(定义一个类)和一个XSL样式表,但可能涉及其他几个文件,因此我立即考虑使用Phars。

Everything's playing nicely together but I noticed if I didn't use createDefaultStub() and instead constructed the Phar as in the following snippet then the result is a quarter of the size — and is smaller than the compressed version. 一切都很好地在一起,但我注意到如果我没有使用createDefaultStub() ,而是在下面的代码片段中构建Phar,那么结果是大小的四分之一 - 并且小于压缩版本。

$phar = new Phar('Example.phar', 0, 'Example.phar');
$phar->buildFromDirectory(__DIR__ . '/src');
$phar->setStub('<?php __HALT_COMPILER();');
$phar->setSignatureAlgorithm(Phar::SHA256);
$phar->compress(Phar::GZ);

Example file sizes: 示例文件大小:

8799 14 Dec 09:37 ExampleCog.phar (using createDefaultStub())
2143 14 Dec 10:08 ExampleCog.phar (using __HALT_COMPILER())
3373 14 Dec 10:08 ExampleCog.phar.gz (consistent with either method)

The Phar will simply be used to keep module-specific files bundled together and will be included in a framework — running standalone wouldn't make any sense in this context. Phar将仅用于将特定于模块的文件捆绑在一起并将包含在框架中 - 在此上下文中独立运行将没有任何意义。 I guess my question is, what am I missing out on — if anything — with using the minimal stub code? 我想我的问题是,我错过了什么 - 如果有的话 - 使用最小的存根代码? And why is the compressed version always the same size? 为什么压缩版本总是相同的大小?

I guess my question is, what am I missing out on — if anything — with using the minimal stub code? 我想我的问题是,我错过了什么 - 如果有的话 - 使用最小的存根代码?

In the file format documentation , the default stub is described as: 文件格式文档中 ,默认存根描述为:

The default stub for phar-based Phar archives contains approximately 7k of code to extract the contents of the phar and execute them. 基于phar的Phar存档的默认存根包含大约7k的代码,用于提取phar的内容并执行它们。

It then points to Phar::createDefaultStub , which says: 然后它指向Phar :: createDefaultStub ,它说:

This method provides a simple and easy method to create a stub that will run a startup file from the phar archive. 此方法提供了一种简单易用的方法来创建将从phar存档运行启动文件的存根。 In addition, different files can be specified for running the phar archive from the command line versus through a web server. 此外,可以指定不同的文件,以便从命令行运行phar存档,而不是通过Web服务器运行。 The loader stub also calls Phar::interceptFileFuncs() to allow easy bundling of a PHP application that accesses the file system. 加载器存根还调用Phar :: interceptFileFuncs()以允许轻松捆绑访问文件系统的PHP应用程序。 If the phar extension is not present, the loader stub will extract the phar archive to a temporary directory and then operate on the files. 如果没有pha​​r扩展,则加载程序存根将phar存档提取到临时目录,然后对文件进行操作。 A shutdown function erases the temporary files on exit. 关闭功能会在退出时删除临时文件。

Emphasis added, as that's the reason the default stub is so large. 强调补充,因为这是默认存根如此之大的原因。 If you can assume that you'll always operate under PHP 5.3 or later, you probably don't need the default stub and can stick with the minimal __HALT_COMPILER 如果你可以假设你总是在PHP 5.3或更高版本下运行,你可能不需要默认的存根,并且可以坚持使用最小的__HALT_COMPILER

And why is the compressed version always the same size? 为什么压缩版本总是相同的大小?

Diving once again into the file format documentation, there's a comparison between archive formats , which explains that Phar performs both per-file and whole-archive compression. 再次潜入文件格式文档, 存档格式之间进行比较 ,这解释了Phar执行每个文件和整个存档压缩。 It's likely that you're seeing similar compression sizes because gzip can't compress the data any further. 您可能会看到类似的压缩大小,因为gzip无法进一步压缩数据。 This is speculation. 这是猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM