function Finder::getComposerRecipesDirectory

Returns the directory where recipes are installed.

Placeholder tokens, like {$name} and {$vendor}, are left as-is.

Parameters

string|null $project_root: (optional) The root of the project, where the top-level `composer.json` is. Auto-detected by default.

Return value

string The path where Composer has installed recipes.

Throws

\RuntimeException Thrown if the install directory for recipes cannot be determined.

2 calls to Finder::getComposerRecipesDirectory()
RecipeFinderTest::testComposerRecipeDirectory in core/tests/Drupal/KernelTests/Core/Recipe/RecipeFinderTest.php
Tests that the Composer recipe directory is read correctly.
RecipeFinderTest::testUnknownComposerRecipeDirectory in core/tests/Drupal/KernelTests/Core/Recipe/RecipeFinderTest.php
Tests that there is an error if Composer is misconfigured for recipes.

File

core/lib/Drupal/Core/Recipe/Finder.php, line 138

Class

Finder
Discovers recipes at specific locations in the file system.

Namespace

Drupal\Core\Recipe

Code

public static function getComposerRecipesDirectory(?string $project_root = NULL) : string {
  // This is expensive to compute, and very unlikely to change in a request.
  if (isset(self::$recipesDirectory)) {
    return self::$recipesDirectory;
  }
  if (empty($project_root)) {
    ['install_path' => $project_root] = InstalledVersions::getRootPackage();
    $project_root = realpath($project_root);
  }
  assert(is_string($project_root));
  $file = $project_root . DIRECTORY_SEPARATOR . 'composer.json';
  $data = (string) file_get_contents($file);
  $data = json_decode($data, TRUE, flags: JSON_THROW_ON_ERROR);
  $directory = array_find_key($data['extra']['installer-paths'] ?? [], fn(array $criteria): bool => in_array('type:' . Recipe::COMPOSER_PROJECT_TYPE, $criteria, TRUE));
  if ($directory) {
    return self::$recipesDirectory = $project_root . DIRECTORY_SEPARATOR . $directory;
  }
  throw new \RuntimeException("No recipe install location is configured in {$file}.");
}

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