function PhpDumper::dumpValue
Throws
8 calls to PhpDumper::dumpValue()
- PhpDumper::addNewInstance in vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php - PhpDumper::addServiceConfigurator in vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php - PhpDumper::addServiceInclude in vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php - PhpDumper::addServiceInstance in vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php - PhpDumper::addServiceMethodCalls in vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php
File
-
vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php, line 1841
Class
- PhpDumper
- PhpDumper dumps a service container as a PHP class.
Namespace
Symfony\Component\DependencyInjection\DumperCode
private function dumpValue(mixed $value, bool $interpolate = true) : string {
if (\is_array($value)) {
if ($value && $interpolate && false !== ($param = array_search($value, $this->container
->getParameterBag()
->all(), true))) {
return $this->dumpValue("%{$param}%");
}
$isList = array_is_list($value);
$code = [];
foreach ($value as $k => $v) {
$code[] = $isList ? $this->dumpValue($v, $interpolate) : \sprintf('%s => %s', $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate));
}
return \sprintf('[%s]', implode(', ', $code));
}
elseif ($value instanceof ArgumentInterface) {
$scope = [
$this->definitionVariables,
$this->referenceVariables,
];
$this->definitionVariables = $this->referenceVariables = null;
try {
if ($value instanceof ServiceClosureArgument) {
$value = $value->getValues()[0];
$code = $this->dumpValue($value, $interpolate);
$returnedType = '';
if ($value instanceof TypedReference) {
$returnedType = \sprintf(': %s\\%s', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $value->getInvalidBehavior() ? '' : '?', str_replace([
'|',
'&',
], [
'|\\',
'&\\',
], $value->getType()));
}
$attribute = '';
if ($value instanceof Reference) {
$attribute = 'name: ' . $this->dumpValue((string) $value, $interpolate);
if ($this->container
->hasDefinition($value) && ($class = $this->container
->findDefinition($value)
->getClass()) && $class !== (string) $value) {
$attribute .= ', class: ' . $this->dumpValue($class, $interpolate);
}
$attribute = \sprintf('#[\\Closure(%s)] ', $attribute);
}
return \sprintf('%sfn ()%s => %s', $attribute, $returnedType, $code);
}
if ($value instanceof IteratorArgument) {
if (!($values = $value->getValues())) {
return 'new RewindableGenerator(fn () => new \\EmptyIterator(), 0)';
}
$code = [];
$code[] = 'new RewindableGenerator(function () use ($container) {';
$operands = [
0,
];
foreach ($values as $k => $v) {
($c = $this->getServiceConditionals($v)) ? $operands[] = "(int) ({$c})" : ++$operands[0];
$v = $this->wrapServiceConditionals($v, \sprintf(" yield %s => %s;\n", $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate)));
foreach (explode("\n", $v) as $v) {
if ($v) {
$code[] = ' ' . $v;
}
}
}
$code[] = \sprintf(' }, %s)', \count($operands) > 1 ? 'fn () => ' . implode(' + ', $operands) : $operands[0]);
return implode("\n", $code);
}
if ($value instanceof ServiceLocatorArgument) {
$serviceMap = '';
$serviceTypes = '';
foreach ($value->getValues() as $k => $v) {
if (!$v instanceof Reference) {
$serviceMap .= \sprintf("\n %s => [%s],", $this->export($k), $this->dumpValue($v));
$serviceTypes .= \sprintf("\n %s => '?',", $this->export($k));
continue;
}
$id = (string) $v;
while ($this->container
->hasAlias($id)) {
$id = (string) $this->container
->getAlias($id);
}
$definition = $this->container
->getDefinition($id);
$load = !($definition->hasErrors() && ($e = $definition->getErrors())) ? $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition) : reset($e);
$serviceMap .= \sprintf("\n %s => [%s, %s, %s, %s],", $this->export($k), $this->export($definition->isShared() ? $definition->isPublic() ? 'services' : 'privates' : false), $this->doExport($id), $this->export(ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE !== $v->getInvalidBehavior() && !\is_string($load) ? $this->generateMethodName($id) : null), $this->export($load));
$serviceTypes .= \sprintf("\n %s => %s,", $this->export($k), $this->export($v instanceof TypedReference ? $v->getType() : '?'));
$this->locatedIds[$id] = true;
}
$this->addGetService = true;
return \sprintf('new \\%s($container->getService ??= $container->getService(...), [%s%s], [%s%s])', ServiceLocator::class, $serviceMap, $serviceMap ? "\n " : '', $serviceTypes, $serviceTypes ? "\n " : '');
}
} finally {
[
$this->definitionVariables,
$this->referenceVariables,
] = $scope;
}
}
elseif ($value instanceof Definition) {
if ($value->hasErrors() && ($e = $value->getErrors())) {
return \sprintf('throw new RuntimeException(%s)', $this->export(reset($e)));
}
if ($this->definitionVariables?->contains($value)) {
return $this->dumpValue($this->definitionVariables[$value], $interpolate);
}
if ($value->getMethodCalls()) {
throw new RuntimeException('Cannot dump definitions which have method calls.');
}
if ($value->getProperties()) {
throw new RuntimeException('Cannot dump definitions which have properties.');
}
if (null !== $value->getConfigurator()) {
throw new RuntimeException('Cannot dump definitions which have a configurator.');
}
return $this->addNewInstance($value);
}
elseif ($value instanceof Variable) {
return '$' . $value;
}
elseif ($value instanceof Reference) {
$id = (string) $value;
while ($this->container
->hasAlias($id)) {
$id = (string) $this->container
->getAlias($id);
}
if (null !== $this->referenceVariables && isset($this->referenceVariables[$id])) {
return $this->dumpValue($this->referenceVariables[$id], $interpolate);
}
return $this->getServiceCall($id, $value);
}
elseif ($value instanceof Expression) {
return $this->getExpressionLanguage()
->compile((string) $value, [
'container' => '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($match[1]);
}
$replaceParameters = fn($match) => "'." . $this->dumpParameter($match[2]) . ".'";
return str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\\1/', $replaceParameters, $this->export($value)));
}
elseif ($value instanceof \UnitEnum) {
return \sprintf('\\%s::%s', $value::class, $value->name);
}
elseif ($value instanceof AbstractArgument) {
throw new RuntimeException($value->getTextWithContext());
}
elseif (\is_object($value) || \is_resource($value)) {
throw new RuntimeException(\sprintf('Unable to dump a service container if a parameter is an object or a resource, got "%s".', get_debug_type($value)));
}
return $this->export($value);
}