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条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?