ClassResolver.php

Same filename in other branches
  1. 9 core/lib/Drupal/Core/DependencyInjection/ClassResolver.php
  2. 10 core/lib/Drupal/Core/DependencyInjection/ClassResolver.php
  3. 11.x core/lib/Drupal/Core/DependencyInjection/ClassResolver.php

Namespace

Drupal\Core\DependencyInjection

File

core/lib/Drupal/Core/DependencyInjection/ClassResolver.php

View source
<?php

namespace Drupal\Core\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;

/**
 * Implements the class resolver interface supporting class names and services.
 */
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;
    }

}

Classes

Title Deprecated Summary
ClassResolver Implements the class resolver interface supporting class names and services.

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