How to determine if PEAR package is installed in php scripts?

I am trying to write dependency tracking code. Is there a way to programmatically determine if the PEAR package is installed? I think something like:

if ($some_pear_api->isPackageInstalled('FooPack')) { echo 'FooPack is installed!'; } else { echo 'FooPack is not installed. :('; } 

I know that you can simply determine if a class file exists for this package, but I basically want to know if PEAR is installed, because sometimes some libraries provide other ways to include their code (for example, PHPUnit has a pear channel, as well as a git repo.).

Thanks for the help!

+4
source share
3 answers

For this you need to use the PEAR_Registry class (as the PEAR script itself). Read the Adam Harvey blog post โ€œ pear โ†’ list โ€ 3 years ago - all the details / examples you need.

 include 'PEAR/Registry.php'; $reg = new PEAR_Registry; foreach ($reg->listPackages() as $package) { print "$package\n"; } 

If you need this to check the specific versions of each package, you can use something in the following example, which I presented in a comment on this blog post:

 <?php require 'PEAR/Registry.php'; $reg = new PEAR_Registry; define("NAME", 0); define("VERSION", 1); $packages = array( array("PEAR", "1.6.2"), array("Date", "1.4.7"), array("Date_Holidays", "0.17.1"), array("Validate_IE", "0.3.1") ); foreach ($packages as $package) { $pkg = $reg->getPackage($package[NAME]); $version = $pkg->getVersion(); echo "{$package[NAME]} โ€“ {$package[VERSION]} โ€“ "; echo version_compare($version, $package[VERSION], '>=') ? 'OK': 'BAD', "\n"; } ?> 

If you need to copy and paste this, then you might be better off using the version https://gist.github.com/kenguest/1671361 .

+7
source

You can use Pear/Info packageInstalled to answer this:

 <?php require_once 'PEAR/Info.php'; $res = PEAR_Info::packageInstalled('FooPack'); if ($res) { print "Package FooPack is installed \n"; } else { print "Package FooPack is not yet installed \n"; } ?> 
0
source

Why not just include the package and see if the class exists?

 // Supress Errors. Checking is done below. @require_once 'PHP/UML.php'; if(!class_exists('PHP_UML')) { throw new Exception('PHP_UML is not installed. Please call `pear install PHP_UML` from the command line',1); } // Code to use PHP_UML below... $uml = new PHP_UML(); 
0
source

Source: https://habr.com/ru/post/1332996/


All Articles