包括不带.phar扩展名的PHP PHAR

问题描述 投票:1回答:1

我在将PHAR包含到我的应用程序引导代码中时遇到麻烦。我试图包括phpunit/phpunit PHAR以使PHPUnit类在我的应用程序中可用。

鉴于我具有以下目录结构:

/path/to/code/
    app.php
    phpunit

phpunit是PHPUnit作为PHAR,app.php包含:

<?php

$phar = 'phar://' . __DIR__ . '/phpunit';

include $phar;

assert(class_exists('\\PHPUnit\\Framework\\TestCase'));

我收到以下错误:

PHP Warning:  include(phar:///path/to/code/phpunit): failed to open stream:
    phar error: no directory in "phar:///path/to/code/phpunit",
    must have at least phar:///path/to/code/phpunit/ for root directory
    (always use full path to a new phar) in /path/to/code/app.php on line 4

但是当我将PHAR重命名为phpunit.phar,并使用[]时将其包括在内

<?php

$phar = 'phar://' . __DIR__ . '/phpunit.phar';

include $phar;

assert(class_exists('\\PHPUnit\\Framework\\TestCase'));

代码运行正常,并且存在TestCase类。

为什么第一个版本不起作用,关于“新药的完整路径”的错误是什么意思?


我的PHP版本是7.2,PHPUnit PHAR在版本7. *上,该版本已与Pive一起安装。


“为什么不包括使用.phar扩展名?”

我希望能够不使用.phar扩展名而将PHPUnit PHAR用作二进制文件,以保持环境整洁。

php include require phar
1个回答
0
投票

为什么第一个版本不起作用

基于source code,您应该能够将phar标记为可执行文件,并且无需扩展即可使用,但是如果您这样做,我不确定是否可以运行它。

 * if executable is 1, only returns SUCCESS if the extension is one of the tar/zip .phar extensions
 * if executable is 0, it returns SUCCESS only if the filename does *not* contain ".phar" anywhere, and treats
 * the first extension as the filename extension
 *
 * if an extension is found, it sets ext_str to the location of the file extension in filename,
 * and ext_len to the length of the extension.
 * for urls like "phar://alias/oops" it instead sets ext_len to -1 and returns FAILURE, which tells
 * the calling function to use "alias" as the phar alias
 *
 * the last parameter should be set to tell the thing to assume that filename is the full path, and only to check the
 * extension rules, not to iterate.

关于“通往新法尔的完整道路”的错误是什么意思?

[我认为他们正在努力传达您不能通过部分途径到达法尔。

我希望能够将PHPUnit PHAR用作不带.phar扩展名的二进制文件,以保持环境的整洁。

您总是可以将phar文件符号链接或别名为无扩展名。

© www.soinside.com 2019 - 2024. All rights reserved.