How can get filesystem path of composer package?
composer.json example:
{
\"require\" : {
\"codeception/codeception\" : \"@stable\",
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