dongshan2680 2010-12-22 04:41
浏览 106
已采纳

如何检测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!

  • 写回答

3条回答 默认 最新

  • duandi4238 2010-12-22 11:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看