trait AutowiredInstanceTrait

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/DependencyInjection/AutowiredInstanceTrait.php \Drupal\Core\DependencyInjection\AutowiredInstanceTrait

Defines a base trait for automatically wiring dependency arguments.

Hierarchy

3 files declare their use of AutowiredInstanceTrait
AutowiredInstanceTraitTest.php in core/tests/Drupal/Tests/Core/DependencyInjection/AutowiredInstanceTraitTest.php
OverrideMenuLinks.php in core/lib/Drupal/Core/Menu/Plugin/ConfigAction/OverrideMenuLinks.php
PluginBase.php in core/lib/Drupal/Core/Plugin/PluginBase.php

File

core/lib/Drupal/Core/DependencyInjection/AutowiredInstanceTrait.php, line 15

Namespace

Drupal\Core\DependencyInjection
View source
trait AutowiredInstanceTrait {
  
  /**
   * Instantiates a new instance of the implementing class using autowiring.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container this instance should use.
   * @param mixed ...$args
   *   Any predefined arguments to pass to the constructor.
   *
   * @return static
   */
  public static function createInstanceAutowired(ContainerInterface $container, mixed ...$args) : static {
    $reflection = new \ReflectionClass(static::class);
    if (method_exists(static::class, '__construct')) {
      $parameters = array_slice($reflection->getMethod('__construct')
        ->getParameters(), count($args));
      $args = array_merge($args, self::getAutowireArguments($container, $parameters, '__construct'));
    }
    $instance = new static(...$args);
    foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
      if (!empty($method->getAttributes(Required::class))) {
        $method->invoke($instance, ...self::getAutowireArguments($container, $method->getParameters(), $method->getName()));
      }
    }
    return $instance;
  }
  
  /**
   * Resolves arguments for a method using autowiring.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container.
   * @param \ReflectionParameter[] $parameters
   *   The parameters to resolve.
   * @param string $method_name
   *   The name of the method being called.
   *
   * @return array
   *   The resolved arguments.
   *
   * @throws \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
   *   When a service cannot be resolved.
   */
  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;
  }

}

Members

Title Sort descending Modifiers Object type Summary
AutowiredInstanceTrait::createInstanceAutowired public static function Instantiates a new instance of the implementing class using autowiring.
AutowiredInstanceTrait::getAutowireArguments private static function Resolves arguments for a method using autowiring.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.