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. 11.x 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
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 11

Namespace

Drupal\Core\DependencyInjection
View source
class ClassResolver implements ClassResolverInterface {
    use DependencySerializationTrait;
    
    /**
     * Constructs a new ClassResolver object.
     *
     * @param \Symfony\Component\DependencyInjection\ContainerInterface|null $container
     *   The service container.
     */
    public function __construct(?ContainerInterface $container = NULL) {
        if ($this->container === NULL) {
            @trigger_error('Calling ' . __METHOD__ . ' without the $container argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3419963', E_USER_DEPRECATED);
            $this->container = \Drupal::getContainer();
        }
    }
    
    /**
     * {@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) {
            @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;
    }
    
    /**
     * Sets the service container.
     *
     * @deprecated in drupal:10.3.0 and is removed from drupal:11.0.0.
     *    Instead, you should pass the container as an argument in the
     *    __construct() method.
     *
     * @see https://www.drupal.org/node/3419963
     */
    public function setContainer(?ContainerInterface $container) : void {
        @trigger_error(__METHOD__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Instead, you should pass the container as an argument in the __construct() method. See https://www.drupal.org/node/3419963', E_USER_DEPRECATED);
        $this->container = $container;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
ClassResolver::getInstanceFromDefinition public function
ClassResolver::setContainer Deprecated public function Sets the service container.
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.