function ResolveBindingsPass::processValue
Overrides AbstractRecursivePass::processValue
File
-
vendor/
symfony/ dependency-injection/ Compiler/ ResolveBindingsPass.php, line 94
Class
- ResolveBindingsPass
- @author Guilhem Niot <guilhem.niot@gmail.com>
Namespace
Symfony\Component\DependencyInjection\CompilerCode
protected function processValue(mixed $value, bool $isRoot = false) : mixed {
if ($value instanceof TypedReference && $value->getType() === (string) $value) {
// Already checked
$bindings = $this->container
->getDefinition($this->currentId)
->getBindings();
$name = $value->getName();
if (isset($name, $bindings[$name = $value . ' $' . $name])) {
return $this->getBindingValue($bindings[$name]);
}
if (isset($bindings[$value->getType()])) {
return $this->getBindingValue($bindings[$value->getType()]);
}
return parent::processValue($value, $isRoot);
}
if (!$value instanceof Definition || !($bindings = $value->getBindings())) {
return parent::processValue($value, $isRoot);
}
$bindingNames = [];
foreach ($bindings as $key => $binding) {
[
$bindingValue,
$bindingId,
$used,
$bindingType,
$file,
] = $binding->getValues();
if ($used) {
$this->usedBindings[$bindingId] = true;
unset($this->unusedBindings[$bindingId]);
}
elseif (!isset($this->usedBindings[$bindingId])) {
$this->unusedBindings[$bindingId] = [
$key,
$this->currentId,
$bindingType,
$file,
];
}
if (preg_match('/^(?:(?:array|bool|float|int|string|iterable|([^ $]++)) )\\$/', $key, $m)) {
$bindingNames[substr($key, \strlen($m[0]))] = $binding;
}
if (!isset($m[1])) {
continue;
}
if (is_subclass_of($m[1], \UnitEnum::class)) {
$bindingNames[substr($key, \strlen($m[0]))] = $binding;
continue;
}
if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument && !$bindingValue instanceof ServiceLocatorArgument) {
throw new InvalidArgumentException(\sprintf('Invalid value for binding key "%s" for service "%s": expected "%s", "%s", "%s", "%s" or null, "%s" given.', $key, $this->currentId, Reference::class, Definition::class, TaggedIteratorArgument::class, ServiceLocatorArgument::class, get_debug_type($bindingValue)));
}
}
if ($value->isAbstract()) {
return parent::processValue($value, $isRoot);
}
$calls = $value->getMethodCalls();
try {
if ($constructor = $this->getConstructor($value, false)) {
$calls[] = [
$constructor,
$value->getArguments(),
];
}
} catch (RuntimeException $e) {
$this->errorMessages[] = $e->getMessage();
$this->container
->getDefinition($this->currentId)
->addError($e->getMessage());
return parent::processValue($value, $isRoot);
}
foreach ($calls as $i => $call) {
[
$method,
$arguments,
] = $call;
if ($method instanceof \ReflectionFunctionAbstract) {
$reflectionMethod = $method;
}
else {
try {
$reflectionMethod = $this->getReflectionMethod($value, $method);
} catch (RuntimeException $e) {
if ($value->getFactory()) {
continue;
}
throw $e;
}
}
$names = [];
foreach ($reflectionMethod->getParameters() as $key => $parameter) {
$names[$key] = $parameter->name;
if (\array_key_exists($key, $arguments) && '' !== $arguments[$key] && !$arguments[$key] instanceof AbstractArgument) {
continue;
}
if (\array_key_exists($parameter->name, $arguments) && '' !== $arguments[$parameter->name] && !$arguments[$parameter->name] instanceof AbstractArgument) {
continue;
}
if ($value->isAutowired() && !$value->hasTag('container.ignore_attributes') && $parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF)) {
continue;
}
$typeHint = ltrim(ProxyHelper::exportType($parameter) ?? '', '?');
$name = Target::parseName($parameter, parsedName: $parsedName);
if ($typeHint && (\array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\\1', $typeHint) . ' $' . $name, $bindings) || \array_key_exists($k = preg_replace('/(^|[(|&])\\\\/', '\\1', $typeHint) . ' $' . $parsedName, $bindings))) {
$arguments[$key] = $this->getBindingValue($bindings[$k]);
continue;
}
if (\array_key_exists($k = '$' . $name, $bindings) || \array_key_exists($k = '$' . $parsedName, $bindings)) {
$arguments[$key] = $this->getBindingValue($bindings[$k]);
continue;
}
if ($typeHint && '\\' === $typeHint[0] && isset($bindings[$typeHint = substr($typeHint, 1)])) {
$arguments[$key] = $this->getBindingValue($bindings[$typeHint]);
continue;
}
if (isset($bindingNames[$name]) || isset($bindingNames[$parsedName]) || isset($bindingNames[$parameter->name])) {
$bindingKey = array_search($binding, $bindings, true);
$argumentType = substr($bindingKey, 0, strpos($bindingKey, ' '));
$this->errorMessages[] = \sprintf('Did you forget to add the type "%s" to argument "$%s" of method "%s::%s()"?', $argumentType, $parameter->name, $reflectionMethod->class, $reflectionMethod->name);
}
}
foreach ($names as $key => $name) {
if (\array_key_exists($name, $arguments) && (0 === $key || \array_key_exists($key - 1, $arguments))) {
if (!array_key_exists($key, $arguments)) {
$arguments[$key] = $arguments[$name];
}
unset($arguments[$name]);
}
}
if ($arguments !== $call[1]) {
ksort($arguments, \SORT_NATURAL);
$calls[$i][1] = $arguments;
}
}
if ($constructor) {
[
,
$arguments,
] = array_pop($calls);
if ($arguments !== $value->getArguments()) {
$value->setArguments($arguments);
}
}
if ($calls !== $value->getMethodCalls()) {
$value->setMethodCalls($calls);
}
return parent::processValue($value, $isRoot);
}