简体   繁体   English

PHP PHAR:再次以正确的方式创建 .phar

[英]PHP PHAR: Once again about creation of .phar in a proper way

I'm trying to create a PHAR Archive with executable stub.我正在尝试使用可执行存根创建一个 PHAR 存档。 The code:编码:

<?php
$phar = new Phar('test.phar');
$phar->buildFromDirectory('files/');
$phar->setStub($phar->createDefaultStub(file_get_contents('st.php'), 'install.php'));
?>

The contents of "st.php" (the default stub): “st.php”的内容(默认存根):

#!/usr/bin/php
<?php Phar::mapPhar(); include("phar://test.phar/install.php"); __HALT_COMPILER();

Inside the "files" is only one file - the "install.php" “文件”里面只有一个文件——“install.php”

<?php
echo "The Installer is running right now.";
?>

PHAR compilation process runs with no any errors. PHAR 编译过程运行没有任何错误。 But when I try to include the created PHAR archive但是当我尝试包含创建的 PHAR 存档时

include_once('phar://test.phar');

the error occurs:发生错误:

PHP Parse error:  syntax error, unexpected ''#!/usr/bin/php\r' (T_ENCAPSED_AND_WHITESPACE) in D:/WebServer/domains/modinst/www/test.phar on line 110

The question is: how to create a PHAR archive for use as a PHP resource that I can simply 'include'?问题是:如何创建一个 PHAR 存档以用作我可以简单地“包含”的 PHP 资源?

You cannot use the shebang line within the stub file.您不能在存根文件中使用 shebang 行。 This is a limitation of current PHP versions.这是当前 PHP 版本的限制。

Remove it and the error will disappear.删除它,错误就会消失。

To set the shebang, use:要设置shebang,请使用:

$phar->setStub("#!/usr/bin/env php" . PHP_EOL 
    . $phar->createDefaultStub(file_get_contents('st.php')));

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

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