You are here

public function ParameterBag::resolveString in Service Container 7

Same name and namespace in other branches
  1. 7.2 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php \Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolveString()

Resolves parameters inside a string.

Parameters

string $value The string to resolve:

array $resolving An array of keys that are being resolved (used internally to detect circular references):

Return value

string The resolved string

Throws

ParameterNotFoundException if a placeholder references a parameter that does not exist

ParameterCircularReferenceException if a circular reference if detected

RuntimeException when a given parameter has a type problem.

1 call to ParameterBag::resolveString()
ParameterBag::resolveValue in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php
Replaces parameter placeholders (%name%) by their values.

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php, line 218

Class

ParameterBag
Holds parameters.

Namespace

Symfony\Component\DependencyInjection\ParameterBag

Code

public function resolveString($value, array $resolving = array()) {

  // we do this to deal with non string values (Boolean, integer, ...)
  // as the preg_replace_callback throw an exception when trying
  // a non-string in a parameter value
  if (preg_match('/^%([^%\\s]+)%$/', $value, $match)) {
    $key = strtolower($match[1]);
    if (isset($resolving[$key])) {
      throw new ParameterCircularReferenceException(array_keys($resolving));
    }
    $resolving[$key] = true;
    return $this->resolved ? $this
      ->get($key) : $this
      ->resolveValue($this
      ->get($key), $resolving);
  }
  $self = $this;
  return preg_replace_callback('/%%|%([^%\\s]+)%/', function ($match) use ($self, $resolving, $value) {

    // skip %%
    if (!isset($match[1])) {
      return '%%';
    }
    $key = strtolower($match[1]);
    if (isset($resolving[$key])) {
      throw new ParameterCircularReferenceException(array_keys($resolving));
    }
    $resolved = $self
      ->get($key);
    if (!is_string($resolved) && !is_numeric($resolved)) {
      throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "%s" of type %s inside string value "%s".', $key, gettype($resolved), $value));
    }
    $resolved = (string) $resolved;
    $resolving[$key] = true;
    return $self
      ->isResolved() ? $resolved : $self
      ->resolveString($resolved, $resolving);
  }, $value);
}