You are here

class ClassResolver in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/DependencyInjection/ClassResolver.php \Drupal\Core\DependencyInjection\ClassResolver

Implements the class resolver interface supporting class names and services.

Hierarchy

Expanded class hierarchy of ClassResolver

1 file declares its use of ClassResolver
ControllerResolverTest.php in core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php
Contains \Drupal\Tests\Core\Controller\ControllerResolverTest.
1 string reference to 'ClassResolver'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ClassResolver
class_resolver in core/core.services.yml
Drupal\Core\DependencyInjection\ClassResolver

File

core/lib/Drupal/Core/DependencyInjection/ClassResolver.php, line 16
Contains \Drupal\Core\DependencyInjection\ClassResolver.

Namespace

Drupal\Core\DependencyInjection
View source
class ClassResolver implements ClassResolverInterface, ContainerAwareInterface {
  use DependencySerializationTrait;
  use ContainerAwareTrait;

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClassResolver::getInstanceFromDefinition public function Returns a class instance with a given class definition. Overrides ClassResolverInterface::getInstanceFromDefinition
ContainerAwareTrait::$container protected property
ContainerAwareTrait::setContainer public function Sets the Container associated with this Controller.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2