简体   繁体   中英

How to properly require a phar file

phpunit has stopped supporting PEAR installation. This has caused us to need to use the phar installation of phpunit moving forward. We have a wrapping script that iterates through many directories and calls the phpunit executable on those directories. The wrapping script then consolidates all of the generated reports into a single report.

When phpunit was installed with PEAR, the only require we needed at the top of the wrapping script was

require 'PHP/CodeCoverage/Autoload.php';

Then, when the wrapping script did things like

$writer = new PHP_CodeCoverage_Report_Clover();
$writer->process($codeCoverage, "${reportsDir}/clover-coverage.xml");

The autoloader would (presumably) load whatever was needed to execute this code and we'd go on our happy way.

Since the move to a phar installation, however, things have gotten messy. Initially I thought I could just require the phar file itself and everything would be good.

require 'phpunit-3.7.31.phar';

However this caused the phar file to be executed as soon as the require line was hit, aborting the wrapping script with phpunit usage output. Since you can't use wildcards in a php require statement, I then slowly built a list of files needed to complete our wrapping script and required them like this:

require 'phar://phpunit-3.7.31.phar/php-code-coverage/CodeCoverage/Report/Clover.php';

This worked when I executed the wrapping script from it's directory and the phpunit.phar file was in the same directory. However, trying to execute it from a different directory would fail saying the phar file was not found on the include path. I attempted to change the include path, put the .phar file in locations already on the include path, and give an absolute path to the phar file in the include like:

require 'phar://actual/path/to/file/phpunit-3.7.31.phar/php-code-coverage/CodeCoverage/Report/Clover.php';

None of these attempts at solving my issue worked.

How do I include files from a .phar file in a way that let's me run the script from any location on my system?

Another acceptable answer would be one that lets me use the classes in the phar file without including everything I need individually.

Ubuntu 12.04

PHPUnit 3.7.31 (we have to stay on this version, but that doesn't really affect the question)

The PHAR of PHPUnit can only be properly used as a library as of PHPUnit 4.2 .

Also have a look at PHPCOV for merging code coverage data from multiple runs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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