You are here

private function PhpDumper::dumpValue in Service Container 7

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

Dumps values.

Parameters

mixed $value:

bool $interpolate:

Return value

string

Throws

RuntimeException

11 calls to PhpDumper::dumpValue()
PhpDumper::addConstructor in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Adds the constructor.
PhpDumper::addFrozenConstructor in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Adds the constructor for a frozen container.
PhpDumper::addNewInstance in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
PhpDumper::addServiceConfigurator in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Adds configurator definition.
PhpDumper::addServiceInclude in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Generates the require_once statement for service includes.

... See full list

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php, line 1280

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
      ->getFactory()) {
      $factory = $value
        ->getFactory();
      if (is_string($factory)) {
        return sprintf('\\%s(%s)', $factory, implode(', ', $arguments));
      }
      if (is_array($factory)) {
        if (is_string($factory[0])) {
          return sprintf('\\%s::%s(%s)', $factory[0], $factory[1], implode(', ', $arguments));
        }
        if ($factory[0] instanceof Definition) {
          return sprintf("call_user_func(array(%s, '%s')%s)", $this
            ->dumpValue($factory[0]), $factory[1], count($arguments) > 0 ? ', ' . implode(', ', $arguments) : '');
        }
        if ($factory[0] instanceof Reference) {
          return sprintf('%s->%s(%s)', $this
            ->dumpValue($factory[0]), $factory[1], implode(', ', $arguments));
        }
      }
      throw new RuntimeException('Cannot dump definition because of invalid factory');
    }
    if (null !== $value
      ->getFactoryMethod(false)) {
      if (null !== $value
        ->getFactoryClass(false)) {
        return sprintf("call_user_func(array(%s, '%s')%s)", $this
          ->dumpValue($value
          ->getFactoryClass(false)), $value
          ->getFactoryMethod(false), count($arguments) > 0 ? ', ' . implode(', ', $arguments) : '');
      }
      elseif (null !== $value
        ->getFactoryService(false)) {
        $service = $this
          ->dumpValue($value
          ->getFactoryService(false));
        return sprintf('%s->%s(%s)', 0 === strpos($service, '$') ? sprintf('$this->get(%s)', $service) : $this
          ->getServiceCall($value
          ->getFactoryService(false)), $value
          ->getFactoryMethod(false), 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 Expression) {
    return $this
      ->getExpressionLanguage()
      ->compile((string) $value, array(
      'this' => 'container',
    ));
  }
  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, $this
        ->export($value)));
      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 $this
      ->export($value);
  }
}