function ClassResolver::getInstanceFromDefinition

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/DependencyInjection/ClassResolver.php \Drupal\Core\DependencyInjection\ClassResolver::getInstanceFromDefinition()
  2. 10 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()

Overrides ClassResolverInterface::getInstanceFromDefinition

File

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

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) {
        $instance->setContainer($this->container);
    }
    return $instance;
}

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