$pear->list

I’ve seen three people ask a variation of how can I get a list of installed PEAR packages from within my PHP script in the last few weeks on IRC, and two just in the last day. Because I’m a helpful sort, here’s a little morsel of code that will do just that:


include 'PEAR/Registry.php';

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

This should work with pretty much any version of PEAR.

(Update: Ken has a nifty bit of code in the comments to check for a specific installed version of a package.)

4 Responses to “$pear->list”

  1. Daniel O'Connor Says:

    Oh, that beats PEAR_Info

  2. PHPDeveloper.org Says:

    Adam Harvey’s Blog: $pear->list…

  3. developercast.com » Adam Harvey’s Blog: $pear->list Says:

    [...] Harvey has a (very) quick post with a hint for PEAR users out there - how to get a list of installaed packages on the system [...]

  4. Ken Guest Says:

    This inspired me to find a way to check if certain packages of a specific version are installed:

    include ‘PEAR/Registry.php’;
    $reg = new PEAR_Registry;

    $packages = array(array(”PEAR”, “1.6.2″),
    array(”Date”, “1.4.7″),
    array(”Date_Holidays”, “0.17.1″),
    array(”Validate_IE”, “0.2.1″));

    foreach($packages as $package) {
    $pkg = $reg->getPackage($package[0]);
    $version = $pkg->getVersion();
    echo “{$package[0]} - {$package[1]} - “, version_compare($version, $package[1], “>=”) ? “OK”: “BAD”, “\n”;
    }

Leave a Reply