Drupal Finder provides a class to locate a Drupal installation in a given path.
$drupalFinder = new \DrupalFinder\DrupalFinder(getcwd());
$drupalRoot = $drupalFinder->getDrupalRoot();
$composerRoot = $drupalFinder->getComposerRoot();
$vendorDir = $drupalFinder->getVendorDir();
If a set of environment variables is specified, then Drupal Finder uses those values to determine the paths of the pertinent directories:
DRUPAL_FINDER_DRUPAL_ROOT
DRUPAL_FINDER_COMPOSER_ROOT
DRUPAL_FINDER_VENDOR_DIR
For example:
DRUPAL_FINDER_DRUPAL_ROOT=/var/www/web
DRUPAL_FINDER_COMPOSER_ROOT=/var/www
DRUPAL_FINDER_VENDOR_DIR=/var/www/vendor
This is useful for situations where you are containerizing an application, directories may be in odd places, or a composer.json might be missing since it is unneeded in a final build artifact.
You are not required to set all the environment variables to use this feature. If you set an environment variable, then its associated getter function will return the value assigned to the environment variable.
GPL-2.0+