如何检测PHP脚本中是否安装了PEAR包?

I'm trying to write some code to track dependencies. Is there a way to programatically detect if a PEAR package has been installed? I'm thinking something like:

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

I know you can simply detect if the class file for that package exists, but I mostly want to know if PEAR has that installed because sometimes some libraries provide other means of including their code (e.g. PHPUnit has a pear channel as well as a git repo.).

Thanks for the help!

You need to use the PEAR_Registry class to do this (which is what the PEAR script itself uses). Read Adam Harvey's blog post "pear -> list" from 3 years ago - all the details/examples you need are there.

include 'PEAR/Registry.php';

$reg = new PEAR_Registry;
foreach ($reg->listPackages() as $package) {
    print "$package
";
}

If you need this to check for specific versions of each package, then you could base something on the following example, which I provided in a comment to that blog entry:

<?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', "
";
}
?>

If you need to copy and paste this, then it might be best for you to use the version at https://gist.github.com/kenguest/1671361.

You can use Pear/Infos packageInstalled to answer this:

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

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();