简体   繁体   English

包含没有 .phar 扩展名的 PHP PHAR

[英]Include PHP PHAR without .phar extension

I'm having trouble including a PHAR into my application bootstrapping code.我在将 PHAR 包含到我的应用程序引导代码中时遇到了麻烦。 I'm attempting to include the phpunit/phpunit PHAR to have the PHPUnit classes available in my application.我正在尝试包含phpunit/phpunit PHAR 以使 PHPUnit 类在我的应用程序中可用。

Given I have the following directory structure:鉴于我有以下目录结构:

/path/to/code/
    app.php
    phpunit

Where phpunit is the PHPUnit as a PHAR, and app.php contains:其中phpunit是作为 PHAR 的 PHPUnit,而app.php包含:

<?php

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

include $phar;

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

I get the following error:我收到以下错误:

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

But when I rename the PHAR to phpunit.phar , and include that instead using但是当我将 PHAR 重命名为phpunit.phar ,并使用

<?php

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

include $phar;

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

The code works fine, and the TestCase class exists.代码工作正常,并且存在TestCase类。

Why doesn't the first version work, and what does the error about "full path to a new phar" mean?为什么第一个版本不起作用,关于“新 phar 的完整路径”的错误是什么意思?


My PHP version is 7.2, and the PHPUnit PHAR is on version 7.* which has been installed with Phive.我的 PHP 版本是 7.2,PHPUnit PHAR 的版本是 7.*,它已与 Phive 一起安装。


"Why don't you just include using the .phar extension?" “为什么不直接使用.phar扩展名?”

I want to be able to use the PHPUnit PHAR as a binary without the .phar extension to keep things cleaner.我希望能够将 PHPUnit PHAR 用作没有.phar扩展名的二进制文件,以保持整洁。

Why doesn't the first version work为什么第一个版本不起作用

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.根据源代码,您应该能够将 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.

what does the error about "full path to a new phar" mean?关于“新 phar 的完整路径”的错误是什么意思?

I think they are trying to convey that you cannot pass a partial path to a phar.我认为他们试图传达你不能通过部分路径到 phar。

I want to be able to use the PHPUnit PHAR as a binary without the .phar extension to keep things cleaner.我希望能够将 PHPUnit PHAR 用作没有 .phar 扩展名的二进制文件,以保持整洁。

You could always just symlink or alias the phar file to an extensionless name.您始终可以将 phar 文件符号链接或别名为无扩展名。

Omit using the phar:// stream since you're accessing the (outer) Phar file.省略使用phar://流,因为您正在访问(外部)Phar 文件。 Using phar:// actually tries to access resources inside a Phar archive.使用phar://实际上是尝试访问 Phar 档案中的资源。

<?php
$phar = __DIR__ . '/phpunit';
include $phar;
assert(class_exists('\\PHPUnit\\Framework\\TestCase'));

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

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