获取已安装的composer软件包的文件系统路径 [英] Get filesystem path of installed composer package

查看:641
本文介绍了获取已安装的composer软件包的文件系统路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取composer包的文件系统路径?

How can get filesystem path of composer package?

composer.json示例:

composer.json example:

{

    "require" : {
        "codeception/codeception" : "@stable",
        "willdurand/geocoder": "*"
    }
}

示例:
$ composer-> getPath(\Geocoder\HttpAdapter\HttpAdapterInterface);

并返回:
/ home / me / public_html / vendor / willdurand / geocoder / src / Geocoder / HttpAdapter

推荐答案

所有这些都是基于这样的假设,你实际上是在谈论而不是类(在示例中提到,但不是在

All of this is based on the assumption that you are actually talking about packages and not classes (which are mentioned in the example but are not asked for in the question).

如果您有 Composer 对象,目录从 Config 对象:

If you have the Composer object, you can get the path of the vendor directory from the Config object:

    $vendorPath = $composer->getConfig()->get('vendor-dir');

$ vendorPath 现在应包含 / home / me / public_html / vendor /

从这里开始构建路径的其余部分不应太难,因为您已经拥有包名。

It shouldn't be too hard to construct the rest of the path from there, as you already have the package name.

如果这种感觉太脆弱或者你不想写逻辑,还有另一个解决方案。你可以获取所有的包,迭代,直到找到正确的包并抓取它的路径:

If this feels too flaky or you don't want to write the logic, there is another solution. You could fetch all packages, iterate until you find the right package and grab the path from it:

    $repositoryManager = $composer->getRepositoryManager();
    $installationManager = $composer->getInstallationManager();
    $localRepository = $repositoryManager->getLocalRepository();

    $packages = $localRepository->getPackages();
    foreach ($packages as $package) {
        if ($package->getName() === 'willdurand/geocoder') {
            $installPath = $installationManager->getInstallPath($package);
            break;
        }
    }

$ installPath 现在应包含 / home / me / public_html / vendor / willdurand / geocoder

这篇关于获取已安装的composer软件包的文件系统路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆