function AutowireTrait::create
Same name in other branches
- 10 core/lib/Drupal/Core/DependencyInjection/AutowireTrait.php \Drupal\Core\DependencyInjection\AutowireTrait::create()
Instantiates a new instance of the implementing class using autowiring.
Parameters
\Symfony\Component\DependencyInjection\ContainerInterface $container: The service container this instance should use.
Return value
static
32 methods override AutowireTrait::create()
- BlockContentController::create in core/
modules/ block_content/ src/ Controller/ BlockContentController.php - Instantiates a new instance of the implementing class using autowiring.
- BlockLibraryController::create in core/
modules/ block/ src/ Controller/ BlockLibraryController.php - Instantiates a new instance of the implementing class using autowiring.
- CallsDestructableServiceController::create in core/
modules/ system/ tests/ modules/ destructable_test/ src/ Controller/ CallsDestructableServiceController.php - Instantiates a new instance of the implementing class using autowiring.
- CKEditor5ImageController::create in core/
modules/ ckeditor5/ src/ Controller/ CKEditor5ImageController.php - Instantiates a new instance of the implementing class using autowiring.
- ConfigTranslationController::create in core/
modules/ config_translation/ src/ Controller/ ConfigTranslationController.php - Instantiates a new instance of the implementing class using autowiring.
File
-
core/
lib/ Drupal/ Core/ DependencyInjection/ AutowireTrait.php, line 25
Class
- AutowireTrait
- Defines a trait for automatically wiring dependencies from the container.
Namespace
Drupal\Core\DependencyInjectionCode
public static function create(ContainerInterface $container) {
$args = [];
if (method_exists(static::class, '__construct')) {
$constructor = new \ReflectionMethod(static::class, '__construct');
foreach ($constructor->getParameters() as $parameter) {
$service = ltrim((string) $parameter->getType(), '?');
foreach ($parameter->getAttributes(Autowire::class) as $attribute) {
$service = (string) $attribute->newInstance()->value;
}
if (!$container->has($service)) {
throw new AutowiringFailedException($service, sprintf('Cannot autowire service "%s": argument "$%s" of method "%s::_construct()", you should configure its value explicitly.', $service, $parameter->getName(), static::class));
}
$args[] = $container->get($service);
}
}
return new static(...$args);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.