简体   繁体   English

如何将phar文件放在phar文件中?

[英]How to place a phar file inside a phar file?

I'd like to place a phar file inside a phar file. 我想将一个phar文件放在一个phar文件中。 I tried it most straight forward: 我最直接地尝试过:

$p = new Phar('test.phar', null, 'self.phar');
$p->setStub('<?php Phar::mapPhar();
include \'phar://self.phar/index.php\'; __HALT_COMPILER(); ?>');
$p['index.php'] = '<?php
echo "hello world\n";';

$p = new Phar('test2.phar', null, 'self.phar');
$p->setStub('<?php Phar::mapPhar();
include \'phar://self.phar/index.php\'; __HALT_COMPILER(); ?>');
$p['index.php'] = '<?php
echo "hello phar world\n";';
$p['test.phar'] = file_get_contents('test.phar');

However PHP just does not want to open it. 但是PHP只是不想打开它。 It does not accept any of the following includes: 它不接受以下任何一项包括:

// Warning: Phar::mapPhar(phar://path/to/test2.phar/test.phar): failed to open
// stream: Invalid argument in phar://path/to/test2.phar/test.phar
include('phar://test2.phar/test.phar');

// Warning: include(phar://test2.phar/test.phar/index.php): failed to open
// stream: phar error: "test.phar/index.php" is not a file in phar "test2.phar"
include('phar://test2.phar/test.phar/index.php');

// Warning: include(phar://test2.phar/phar://test.phar/index.php): failed to
// open stream: phar error: "phar:/test.phar/index.php" is not a file in phar
// "test2.phar"
include('phar://test2.phar/phar://test.phar/index.php');

I know the constructiveness of this question is limited, because it might just not work with phar-in-phar however probably I've just been missing a way how to do that and I'm just not seeing the wood for the trees. 我知道这个问题的建设性是有限的,因为它可能不适用于phar-in-phar但是我可能只是错过了一种方法如何做到这一点而我只是没有看到树木的木材。

The function that you should use to load a phar file in PHP is Phar::loadPhar eg 用于在PHP中加载phar文件的函数是Phar :: loadPhar eg

Phar::loadPhar("test2.phar", "test2");

Would make the phar file test2.phar be loaded and accessible via the alias test2, so you could include files from it by doing: 是否可以通过别名test2加载和访问phar文件test2.phar,因此您可以通过执行以下操作来包含文件:

include ('phar://test2/index.php');

However it appears that this does not work if that file is inside a phar itself. 但是,如果该文件位于phar本身内部,则似乎不起作用。 The PHP code for loadPhar is: loadPhar的PHP代码是:

fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, &actual);

and apparently the IGNORE_URL flag makes it impossible for the file to be opened. 显然IGNORE_URL标志使文件无法打开。

There is a workaround - extract the phar file that is contained inside the other phar to a temporary file, and then it apparently can be loaded without any issue. 有一个解决方法 - 将另一个phar中包含的phar文件解压缩到一个临时文件,然后显然可以毫无问题地加载它。 The following code will extract a phar file (phar1.phar) that is contained in a 2nd phar file, and then call loadPhar. 以下代码将提取第二个phar文件中包含的phar文件(phar1.phar),然后调用loadPhar。

function extractAndLoadPhar(){

    $tempFilename =  tempnam( sys_get_temp_dir() , "phar");

    $readHandle = fopen("phar://phar2/phar1.phar", "r");
    $writeHandle =  fopen($tempFilename, "w");

    while (!feof($readHandle)) {
        $result = fread($readHandle, 512);
        fwrite($writeHandle, $result);
    }

    fclose($readHandle);
    fclose($writeHandle);

    $result = Phar::loadPhar($tempFilename, "phar1");
}

extractAndLoadPhar(); //Extract and load the phar
include ('phar://phar1/src1.php'); //It can now be referenced by 'phar1'

I have placed a working copy of this code here https://github.com/Danack/pharphar which creates a phar, embeds it in a 2nd phar, and then loads and calls a function from the 1st phar inside the 2nd phar. 我已经在这里放置了这段代码的工作副本https://github.com/Danack/pharphar ,它创建了一个phar,将它嵌入到第二个phar中,然后加载并调用第二个phar中第一个phar的函数。

To note - I'm not convinced that this technique is a good idea. 要注意 - 我不相信这种技术是个好主意。 There seems to be some ambiguity (aka I didn't understand) what happens to the stub files for each of the phar files. 似乎有一些歧义(我不明白)每个phar文件的存根文件会发生什么。 ie do they both get loaded, or is it just the outermost phar file that has it's stub loaded and run. 即它们是否都被加载,或者它只是最外层的phar文件,它的存根被加载并运行。

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

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