function ComposerIntegrationTrait::binDir

Same name and namespace in other branches
  1. main core/tests/Drupal/Tests/Composer/ComposerIntegrationTrait.php \Drupal\Tests\Composer\ComposerIntegrationTrait::binDir()

Gets the absolute path to the Composer bin directory.

Resolution order follows Composer itself: the COMPOSER_BIN_DIR environment variable, the "bin-dir" setting in composer.json, the "vendor-dir" setting plus "/bin", and finally "vendor/bin".

Parameters

string|null $project_root: The directory containing the root composer.json. If NULL, it is detected from the Composer runtime API.

Return value

string The absolute path to the bin directory. The directory is not guaranteed to exist.

8 calls to ComposerIntegrationTrait::binDir()
ComposerIntegrationTest::testAbsoluteBinDir in core/tests/Drupal/Tests/Composer/ComposerIntegrationTest.php
Tests that an absolute bin-dir is returned unchanged.
ComposerIntegrationTest::testBinDir in core/tests/Drupal/Tests/Composer/ComposerIntegrationTest.php
Tests bin dir resolution against composer.json contents.
ComposerIntegrationTest::testEmptyEnvironmentVariableIgnored in core/tests/Drupal/Tests/Composer/ComposerIntegrationTest.php
Tests that an empty COMPOSER_BIN_DIR environment variable is ignored.
ComposerIntegrationTest::testEnvironmentVariableOverride in core/tests/Drupal/Tests/Composer/ComposerIntegrationTest.php
Tests that the COMPOSER_BIN_DIR environment variable takes precedence.
ComposerIntegrationTest::testMalformedComposerJson in core/tests/Drupal/Tests/Composer/ComposerIntegrationTest.php
Tests that malformed composer.json falls back to the default.

... See full list

File

core/tests/Drupal/Tests/Composer/ComposerIntegrationTrait.php, line 54

Class

ComposerIntegrationTrait
Some utility functions for testing the Composer integration.

Namespace

Drupal\Tests\Composer

Code

public static function binDir(?string $project_root = NULL) : string {
  if ($project_root === NULL) {
    $project_root = realpath(InstalledVersions::getRootPackage()['install_path']);
  }
  $bin_dir = getenv('COMPOSER_BIN_DIR');
  if ($bin_dir === FALSE || $bin_dir === '') {
    $config = [];
    $file = $project_root . '/composer.json';
    if (is_readable($file)) {
      $json = json_decode((string) file_get_contents($file), TRUE);
      $config = $json['config'] ?? [];
    }
    if (!empty($config['bin-dir'])) {
      $bin_dir = $config['bin-dir'];
    }
    else {
      $vendor_dir = !empty($config['vendor-dir']) ? $config['vendor-dir'] : 'vendor';
      $bin_dir = $vendor_dir . '/bin';
    }
  }
  // Relative paths are resolved against the composer.json location.
  if (!preg_match('{^(?:/|[a-zA-Z]:[\\\\/])}', $bin_dir)) {
    $bin_dir = $project_root . '/' . $bin_dir;
  }
  return $bin_dir;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.