function ClassResolver::getInstanceFromDefinition

Same name in other branches
  1. 9 core/lib/Drupal/Core/DependencyInjection/ClassResolver.php \Drupal\Core\DependencyInjection\ClassResolver::getInstanceFromDefinition()
  2. 8.9.x core/lib/Drupal/Core/DependencyInjection/ClassResolver.php \Drupal\Core\DependencyInjection\ClassResolver::getInstanceFromDefinition()
  3. 11.x core/lib/Drupal/Core/DependencyInjection/ClassResolver.php \Drupal\Core\DependencyInjection\ClassResolver::getInstanceFromDefinition()

File

core/lib/Drupal/Core/DependencyInjection/ClassResolver.php, line 31

Class

ClassResolver
Implements the class resolver interface supporting class names and services.

Namespace

Drupal\Core\DependencyInjection

Code

public function getInstanceFromDefinition($definition) {
    if ($this->container
        ->has($definition)) {
        $instance = $this->container
            ->get($definition);
    }
    else {
        if (!class_exists($definition)) {
            throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $definition));
        }
        if (is_subclass_of($definition, 'Drupal\\Core\\DependencyInjection\\ContainerInjectionInterface')) {
            $instance = $definition::create($this->container);
        }
        else {
            $instance = new $definition();
        }
    }
    if ($instance instanceof ContainerAwareInterface) {
        @trigger_error('Implementing \\Symfony\\Component\\DependencyInjection\\ContainerAwareInterface is deprecated in drupal:10.3.0 and it will be removed in drupal:11.0.0. Implement \\Drupal\\Core\\DependencyInjection\\ContainerInjectionInterface and use dependency injection instead. See https://www.drupal.org/node/3428661', E_USER_DEPRECATED);
        $instance->setContainer($this->container);
    }
    return $instance;
}

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