class ClassResolver

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

3 files declare their use of ClassResolver
CallableResolverTest.php in core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php
ControllerResolverTest.php in core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php
ThemeNegotiatorTest.php in core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php

File

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

Namespace

Drupal\Core\DependencyInjection
View source
class ClassResolver implements ClassResolverInterface {
    use DependencySerializationTrait;
    
    /**
     * Constructs a new ClassResolver object.
     *
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
     *   The service container.
     */
    public function __construct(ContainerInterface $container) {
    }
    
    /**
     * {@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();
            }
        }
        return $instance;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ClassResolver::getInstanceFromDefinition public function Returns a class instance with a given class definition. Overrides ClassResolverInterface::getInstanceFromDefinition
ClassResolver::__construct public function Constructs a new ClassResolver object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2

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