首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在没有.phar扩展的情况下包括PHAR

在没有.phar扩展的情况下包括PHAR
EN

Stack Overflow用户
提问于 2019-12-12 10:40:15
回答 2查看 577关注 0票数 2

在我的应用程序引导代码中包含一个PHAR是有问题的。我试图在我的应用程序中包含phpunit/phpunit PHAR,以使PHPUnit类可用。

考虑到我有以下目录结构:

代码语言:javascript
复制
/path/to/code/
    app.php
    phpunit

其中,phpunit是作为PHPUnit的PHPUnit,app.php包含:

代码语言:javascript
复制
<?php

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

include $phar;

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

我得到以下错误:

代码语言:javascript
复制
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并包含它时,请使用

代码语言:javascript
复制
<?php

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

include $phar;

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

代码工作正常,并且存在TestCase类。

为什么第一个版本不能工作,关于“新phar的完整路径”的错误意味着什么?

我的PHP版本是7.2,PHPUnit PHAR是在版本7.*上安装的。

为什么不直接包括使用.phar 扩展?

我希望能够在没有PHPUnit扩展的情况下将.phar PHAR作为二进制文件使用,以保持事物的清洁。

EN

回答 2

Stack Overflow用户

发布于 2020-01-11 09:28:51

为什么第一个版本不能工作

基于源代码,您应该能够使用标记phar作为可执行文件,并在没有扩展的情况下使用它,但是如果这样做,我不确定您是否能够运行它。

代码语言:javascript
复制
 * 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.

关于“通向新phar的完全路径”的错误意味着什么?

我认为他们是在试图传达这样的信息:你不能通过一条通往和平进程的部分道路。

我希望能够在没有PHPUnit扩展的情况下将.phar PHAR作为二进制文件使用,以保持事物的清洁。

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

票数 0
EN

Stack Overflow用户

发布于 2021-01-02 11:47:55

不要使用phar://流,因为您正在访问(外部) Phar文件。使用phar://实际上试图访问Phar存档中的资源。

代码语言:javascript
复制
<?php
$phar = __DIR__ . '/phpunit';
include $phar;
assert(class_exists('\\PHPUnit\\Framework\\TestCase'));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59302726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档