You are here

protected function Interpolator::replacements in Drupal 9

Same name and namespace in other branches
  1. 8 composer/Plugin/Scaffold/Interpolator.php \Drupal\Composer\Plugin\Scaffold\Interpolator::replacements()

Finds the tokens that exist in a message and builds a replacement array.

All of the replacements in the data array are looked up given the token keys from the provided message. Keys that do not exist in the configuration are replaced with the default value.

Parameters

string $message: String with tokens.

array $data: Data to use for interpolation.

string $default: (optional) The value to substitute for tokens that are not found in the data. If FALSE, then missing tokens are not replaced. Defaults to an empty string.

Return value

string[] An array of replacements to make. Keyed by tokens and the replacements are the values.

1 call to Interpolator::replacements()
Interpolator::interpolate in composer/Plugin/Scaffold/Interpolator.php
Replaces tokens in a string with values from an associative array.

File

composer/Plugin/Scaffold/Interpolator.php, line 123

Class

Interpolator
Injects config values from an associative array into a string.

Namespace

Drupal\Composer\Plugin\Scaffold

Code

protected function replacements($message, array $data, $default = '') {
  $tokens = $this
    ->findTokens($message);
  $replacements = [];
  foreach ($tokens as $sourceText => $key) {
    $replacement_text = array_key_exists($key, $data) ? $data[$key] : $default;
    if ($replacement_text !== FALSE) {
      $replacements[$sourceText] = $replacement_text;
    }
  }
  return $replacements;
}