Get filesystem path of installed composer package

后端 未结 4 1041
渐次进展
渐次进展 2021-02-08 07:43

How can get filesystem path of composer package?

composer.json example:

{

    \"require\" : {
        \"codeception/codeception\" : \"@stable\",
                


        
4条回答
  •  执念已碎
    2021-02-08 08:21

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

    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 should now contain /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 should now contain /home/me/public_html/vendor/willdurand/geocoder

提交回复
热议问题