包括不带.phar扩展名的PHP PHAR [英] Include PHP PHAR without .phar extension

查看:182
本文介绍了包括不带.phar扩展名的PHP PHAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having trouble including a PHAR into my application bootstrapping code. I'm attempting to include the phpunit/phpunit PHAR to have the PHPUnit classes available in my application.

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

Given I have the following directory structure:

/path/to/code/
    app.php
    phpunit

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

Where phpunit is the PHPUnit as a PHAR, and app.php contains:

<?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时,请使用

But when I rename the PHAR to phpunit.phar, and include that instead using

<?php

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

include $phar;

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

代码可以正常工作,并且TestCase类存在.

The code works fine, and the TestCase class exists.

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

Why doesn't the first version work, and what does the error about "full path to a new phar" mean?

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

My PHP version is 7.2, and the PHPUnit PHAR is on version 7.* which has been installed with Phive.

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

"Why don't you just include using the .phar extension?"

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

I want to be able to use the PHPUnit PHAR as a binary without the .phar extension to keep things cleaner.

推荐答案

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

Why doesn't the first version work

基于源代码,您应该可以将phar标记为可执行文件,并且不带扩展名即可使用,但是,如果这样做,我不确定您可以运行它.

Based on the source code, you should be able to use a mark the phar as executable and use it without an extension, but if you do that, I am not sure you can run it.

 * 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.

通往新法尔的完整道路"的错误是什么意思?

what does the error about "full path to a new phar" mean?

我认为他们正在努力传达您不能通过一条通往法尔的部分路径的信.

I think they are trying to convey that you cannot pass a partial path to a phar.

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

I want to be able to use the PHPUnit PHAR as a binary without the .phar extension to keep things cleaner.

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

You could always just symlink or alias the phar file to an extensionless name.

这篇关于包括不带.phar扩展名的PHP PHAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆