Skip to content
Snippets Groups Projects

Added automatically wiring dependencies if ContainerInjectionInterface is not implemented.

1 unresolved thread
Files
16
@@ -2,9 +2,7 @@
namespace Drupal\Core\DependencyInjection;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
/**
* Defines a trait for automatically wiring dependencies from the container.
@@ -23,25 +21,7 @@ trait AutowireTrait {
* @return static
*/
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);
return $container->get(DependencyAutowire::class)->autowireClass(new \ReflectionClass(static::class));
}
}
Loading