Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ $drupalFinder = new \DrupalFinder\DrupalFinder(getcwd());
$drupalRoot = $drupalFinder->getDrupalRoot();
$composerRoot = $drupalFinder->getComposerRoot();
$vendorDir = $drupalFinder->getVendorDir();
$vendorBin = $drupalFinder->getVendorBin();
```

### Environment variables
Expand All @@ -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
Expand Down
43 changes: 41 additions & 2 deletions src/DrupalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
}

/**
Expand All @@ -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());
}

/**
Expand Down