private function PhpDumper::dumpValue

Dumps values.

Parameters

array $value:

Boolean $interpolate:

Return value

string

Throws

RuntimeException

11 calls to PhpDumper::dumpValue()
PhpDumper::addConstructor in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Adds the constructor.
PhpDumper::addFrozenConstructor in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Adds the constructor for a frozen container.
PhpDumper::addNewInstance in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
PhpDumper::addServiceConfigurator in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Adds configurator definition
PhpDumper::addServiceInclude in drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Generates the require_once statement for service includes.

... See full list

File

drupal/core/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php, line 1145

Class

PhpDumper
PhpDumper dumps a service container as a PHP class.

Namespace

Symfony\Component\DependencyInjection\Dumper

Code

private function dumpValue($value, $interpolate = true) {
  if (is_array($value)) {
    $code = array();
    foreach ($value as $k => $v) {
      $code[] = sprintf('%s => %s', $this
        ->dumpValue($k, $interpolate), $this
        ->dumpValue($v, $interpolate));
    }
    return sprintf('array(%s)', implode(', ', $code));
  }
  elseif ($value instanceof Definition) {
    if (null !== $this->definitionVariables && $this->definitionVariables
      ->contains($value)) {
      return $this
        ->dumpValue($this->definitionVariables
        ->offsetGet($value), $interpolate);
    }
    if (count($value
      ->getMethodCalls()) > 0) {
      throw new RuntimeException('Cannot dump definitions which have method calls.');
    }
    if (null !== $value
      ->getConfigurator()) {
      throw new RuntimeException('Cannot dump definitions which have a configurator.');
    }
    $arguments = array();
    foreach ($value
      ->getArguments() as $argument) {
      $arguments[] = $this
        ->dumpValue($argument);
    }
    $class = $this
      ->dumpValue($value
      ->getClass());
    if (false !== strpos($class, '$')) {
      throw new RuntimeException('Cannot dump definitions which have a variable class name.');
    }
    if (null !== $value
      ->getFactoryMethod()) {
      if (null !== $value
        ->getFactoryClass()) {
        return sprintf("call_user_func(array(%s, '%s')%s)", $this
          ->dumpValue($value
          ->getFactoryClass()), $value
          ->getFactoryMethod(), count($arguments) > 0 ? ', ' . implode(', ', $arguments) : '');
      }
      elseif (null !== $value
        ->getFactoryService()) {
        return sprintf("%s->%s(%s)", $this
          ->getServiceCall($value
          ->getFactoryService()), $value
          ->getFactoryMethod(), implode(', ', $arguments));
      }
      else {
        throw new RuntimeException('Cannot dump definitions which have factory method without factory service or factory class.');
      }
    }
    return sprintf("new \\%s(%s)", substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
  }
  elseif ($value instanceof Variable) {
    return '$' . $value;
  }
  elseif ($value instanceof Reference) {
    if (null !== $this->referenceVariables && isset($this->referenceVariables[$id = (string) $value])) {
      return $this
        ->dumpValue($this->referenceVariables[$id], $interpolate);
    }
    return $this
      ->getServiceCall((string) $value, $value);
  }
  elseif ($value instanceof Parameter) {
    return $this
      ->dumpParameter($value);
  }
  elseif (true === $interpolate && is_string($value)) {
    if (preg_match('/^%([^%]+)%$/', $value, $match)) {

      // we do this to deal with non string values (Boolean, integer, ...)
      // the preg_replace_callback converts them to strings
      return $this
        ->dumpParameter(strtolower($match[1]));
    }
    else {
      $that = $this;
      $replaceParameters = function ($match) use ($that) {
        return "'." . $that
          ->dumpParameter(strtolower($match[2])) . ".'";
      };
      $code = str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\\1/', $replaceParameters, var_export($value, true)));
      return $code;
    }
  }
  elseif (is_object($value) || is_resource($value)) {
    throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
  }
  else {
    return var_export($value, true);
  }
}