function AutowiredInstanceTrait::getAutowireArguments
Resolves arguments for a method using autowiring.
Parameters
\Symfony\Component\DependencyInjection\ContainerInterface $container: The service container.
\ReflectionParameter[] $parameters: The parameters to resolve.
string $method_name: The name of the method being called.
Return value
array The resolved arguments.
Throws
\Symfony\Component\DependencyInjection\Exception\AutowiringFailedException When a service cannot be resolved.
File
-
core/
lib/ Drupal/ Core/ DependencyInjection/ AutowiredInstanceTrait.php, line 62
Class
- AutowiredInstanceTrait
- Defines a base trait for automatically wiring dependency arguments.
Namespace
Drupal\Core\DependencyInjectionCode
private static function getAutowireArguments(ContainerInterface $container, array $parameters, string $method_name) : array {
$args = [];
foreach ($parameters as $parameter) {
$service = ltrim((string) $parameter->getType(), '?');
foreach ($parameter->getAttributes(Autowire::class) as $attribute) {
$service = (string) $attribute->newInstance()->value;
}
if ($container->has($service)) {
$args[] = $container->get($service);
continue;
}
if ($parameter->allowsNull()) {
$args[] = NULL;
continue;
}
throw new AutowiringFailedException($service, sprintf('Cannot autowire service "%s": argument "$%s" of method "%s::%s()". Check that either the argument type is correct or the Autowire attribute is passed a valid identifier. Otherwise configure its value explicitly if possible.', $service, $parameter->getName(), static::class, $method_name));
}
return $args;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.