Dumps values.
array $value:
Boolean $interpolate:
string
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);
}
}