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
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'));
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
<?php
$phar = 'phar://' . __DIR__ . '/phpunit.phar';
include $phar;
assert(class_exists('\PHPUnit\Framework\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?
My PHP version is 7.2, and the PHPUnit PHAR is on version 7.* which has been installed with Phive.
“Why don’t you just include using the .phar
extension?”
I want to be able to use the PHPUnit PHAR as a binary without the .phar
extension to keep things cleaner.
2
Answers
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.
I think they are trying to convey that you cannot pass a partial path to a phar.
You could always just symlink or alias the phar file to an extensionless name.
Omit using the
phar://
stream since you’re accessing the (outer) Phar file. Usingphar://
actually tries to access resources inside a Phar archive.