diff --git a/README.md b/README.md index c68c09a..ce75881 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ $drupalFinder = new \DrupalFinder\DrupalFinder(getcwd()); $drupalRoot = $drupalFinder->getDrupalRoot(); $composerRoot = $drupalFinder->getComposerRoot(); $vendorDir = $drupalFinder->getVendorDir(); +$vendorBin = $drupalFinder->getVendorBin(); ``` ### Environment variables @@ -23,12 +24,14 @@ values to determine the paths of the pertinent directories: - `DRUPAL_FINDER_DRUPAL_ROOT` - `DRUPAL_FINDER_COMPOSER_ROOT` - `DRUPAL_FINDER_VENDOR_DIR` +- `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` +- `DRUPAL_FINDER_VENDOR_BIN=/var/www/vendor/bin` 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 diff --git a/src/DrupalFinder.php b/src/DrupalFinder.php index ffbcecd..36fb50c 100644 --- a/src/DrupalFinder.php +++ b/src/DrupalFinder.php @@ -24,6 +24,11 @@ class DrupalFinder */ const ENV_VENDOR_DIR = 'DRUPAL_FINDER_VENDOR_DIR'; + /** + * Vendor directory environment variable. + */ + const ENV_VENDOR_BIN = 'DRUPAL_FINDER_VENDOR_BIN'; + /** * Drupal web public directory. * @@ -47,6 +52,15 @@ class DrupalFinder */ private $vendorDir; + /** + * Composer vendor bin directory. + * + * @var string + * + * @see https://getcomposer.org/doc/06-config.md#vendor-bin + */ + private $vendorBin; + /** * Initialize finder. * @@ -64,6 +78,7 @@ public function __construct($start_path = null) { $this->drupalRoot = false; $this->composerRoot = false; $this->vendorDir = false; + $this->vendorBin = false; // If a starting path was provided, attempt to locate and set path // variables. @@ -130,6 +145,18 @@ public function getVendorDir() return !empty($environment_path) ? $environment_path : $this->vendorDir; } + /** + * Get the vendor bin. + * + * @return string|bool + * The path to the vendor bin, if it was found. False otherwise. + */ + public function getVendorBin() + { + $environment_path = $this->getValidEnvironmentVariablePath(self::ENV_VENDOR_BIN); + return !empty($environment_path) ? $environment_path : $this->vendorBin; + } + /** * Discover all valid paths. * @@ -232,6 +259,18 @@ protected function findAndValidateRoots($path) $this->vendorDir = $this->composerRoot . '/' . $json['config']['vendor-dir']; } } + if ($this->composerRoot && $this->vendorDir && file_exists($this->composerRoot . '/' . $this->getComposerFileName())) { + $json = json_decode( + file_get_contents($path . '/' . $this->getComposerFileName()), + true + ); + if (is_array($json) && isset($json['config']['bin-dir'])) { + $this->vendorBin = $this->composerRoot . '/' . $json['config']['bin-dir']; + } + elseif ($this->vendorDir) { + $this->vendorBin = $this->vendorDir . '/bin'; + } + } return $this->allPathsDiscovered(); } @@ -251,7 +290,7 @@ protected function getComposerFileName() * True if all paths have been discovered, false if one or more haven't been found. */ protected function allPathsDiscovered() { - return !empty($this->drupalRoot) && !empty($this->composerRoot) && !empty($this->vendorDir); + return !empty($this->drupalRoot) && !empty($this->composerRoot) && !empty($this->vendorDir) && !empty($this->vendorBin); } /** @@ -261,7 +300,7 @@ protected function allPathsDiscovered() { * True if all paths are known, false if one or more paths are unknown. */ protected function allPathsKnown() { - return !empty($this->getDrupalRoot()) && !empty($this->getComposerRoot()) && !empty($this->getVendorDir()); + return !empty($this->getDrupalRoot()) && !empty($this->getComposerRoot()) && !empty($this->getVendorDir()) && !empty($this->getVendorBin()); } /**