function PhpDumper::addNewInstance
4 calls to PhpDumper::addNewInstance()
- PhpDumper::addInlineService in vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php - PhpDumper::addServiceInstance in vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php - PhpDumper::dumpValue in vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php - PhpDumper::getServiceCall in vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php
File
-
vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php, line 1138
Class
- PhpDumper
- PhpDumper dumps a service container as a PHP class.
Namespace
Symfony\Component\DependencyInjection\DumperCode
private function addNewInstance(Definition $definition, string $return = '', ?string $id = null, bool $asGhostObject = false) : string {
$tail = $return ? str_repeat(')', substr_count($return, '(') - substr_count($return, ')')) . ";\n" : '';
$arguments = [];
if (BaseServiceLocator::class === $definition->getClass() && $definition->hasTag($this->serviceLocatorTag)) {
foreach ($definition->getArgument(0) as $k => $argument) {
$arguments[$k] = $argument->getValues()[0];
}
return $return . $this->dumpValue(new ServiceLocatorArgument($arguments)) . $tail;
}
foreach ($definition->getArguments() as $i => $value) {
$arguments[] = (\is_string($i) ? $i . ': ' : '') . $this->dumpValue($value);
}
if (null !== $definition->getFactory()) {
$callable = $definition->getFactory();
if ('current' === $callable && [
0,
] === array_keys($definition->getArguments()) && \is_array($value) && [
0,
] === array_keys($value)) {
return $return . $this->dumpValue($value[0]) . $tail;
}
if ([
'Closure',
'fromCallable',
] === $callable) {
$callable = $definition->getArgument(0);
if ($callable instanceof ServiceClosureArgument) {
return $return . $this->dumpValue($callable) . $tail;
}
$arguments = [
'...',
];
if ($callable instanceof Reference || $callable instanceof Definition) {
$callable = [
$callable,
'__invoke',
];
}
}
if (\is_string($callable) && str_starts_with($callable, '@=')) {
return $return . \sprintf('(($args = %s) ? (%s) : null)', $this->dumpValue(new ServiceLocatorArgument($definition->getArguments())), $this->getExpressionLanguage()
->compile(substr($callable, 2), [
'container' => 'container',
'args' => 'args',
])) . $tail;
}
if (!\is_array($callable)) {
return $return . \sprintf('%s(%s)', $this->dumpLiteralClass($this->dumpValue($callable)), $arguments ? implode(', ', $arguments) : '') . $tail;
}
if (!preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $callable[1])) {
throw new RuntimeException(\sprintf('Cannot dump definition because of invalid factory method (%s).', $callable[1] ?: 'n/a'));
}
if ([
'...',
] === $arguments && ($definition->isLazy() || 'Closure' !== ($definition->getClass() ?? 'Closure')) && ($callable[0] instanceof Reference || $callable[0] instanceof Definition && !$this->definitionVariables
->contains($callable[0]))) {
$initializer = 'fn () => ' . $this->dumpValue($callable[0]);
return $return . LazyClosure::getCode($initializer, $callable, $definition, $this->container, $id) . $tail;
}
if ($callable[0] instanceof Reference || $callable[0] instanceof Definition && $this->definitionVariables
->contains($callable[0])) {
return $return . \sprintf('%s->%s(%s)', $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '') . $tail;
}
$class = $this->dumpValue($callable[0]);
// If the class is a string we can optimize away
if (str_starts_with($class, "'") && !str_contains($class, '$')) {
if ("''" === $class) {
throw new RuntimeException(\sprintf('Cannot dump definition: "%s" service is defined to be created by a factory but is missing the service reference, did you forget to define the factory service id or class?', $id ? 'The "' . $id . '"' : 'inline'));
}
return $return . \sprintf('%s::%s(%s)', $this->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '') . $tail;
}
if (str_starts_with($class, 'new ')) {
return $return . \sprintf('(%s)->%s(%s)', $class, $callable[1], $arguments ? implode(', ', $arguments) : '') . $tail;
}
return $return . \sprintf("[%s, '%s'](%s)", $class, $callable[1], $arguments ? implode(', ', $arguments) : '') . $tail;
}
if (null === ($class = $definition->getClass())) {
throw new RuntimeException('Cannot dump definitions which have no class nor factory.');
}
if (!$asGhostObject) {
return $return . \sprintf('new %s(%s)', $this->dumpLiteralClass($this->dumpValue($class)), implode(', ', $arguments)) . $tail;
}
if (!method_exists($this->container
->getParameterBag()
->resolveValue($class), '__construct')) {
return $return . '$lazyLoad' . $tail;
}
return $return . \sprintf('($lazyLoad->__construct(%s) && false ?: $lazyLoad)', implode(', ', $arguments)) . $tail;
}