Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AutowirePass.php

function AutowirePass::getAutowiredReference

Returns a reference to the service matching the given type, if any.

2 calls to AutowirePass::getAutowiredReference()
AutowirePass::autowireMethod in vendor/symfony/dependency-injection/Compiler/AutowirePass.php
Autowires the constructor or a method.
AutowirePass::doProcessValue in vendor/symfony/dependency-injection/Compiler/AutowirePass.php

File

vendor/symfony/dependency-injection/Compiler/AutowirePass.php, line 442

Class

AutowirePass
Inspects existing service definitions and wires the autowired ones using the type hints of their classes.

Namespace

Symfony\Component\DependencyInjection\Compiler

Code

private function getAutowiredReference(TypedReference $reference, bool $filterType) : ?TypedReference {
    $this->lastFailure = null;
    $type = $reference->getType();
    if ($type !== (string) $reference) {
        return $reference;
    }
    if ($filterType && false !== ($m = strpbrk($type, '&|'))) {
        $types = array_diff(explode($m[0], $type), [
            'int',
            'string',
            'array',
            'bool',
            'float',
            'iterable',
            'object',
            'callable',
            'null',
        ]);
        sort($types);
        $type = implode($m[0], $types);
    }
    $name = $target = (array_filter($reference->getAttributes(), static fn($a) => $a instanceof Target)[0] ?? null)?->name;
    if (null !== ($name ??= $reference->getName())) {
        if ($this->container
            ->has($alias = $type . ' $' . $name) && !$this->container
            ->findDefinition($alias)
            ->isAbstract()) {
            return new TypedReference($alias, $type, $reference->getInvalidBehavior());
        }
        if (null !== ($alias = $this->getCombinedAlias($type, $name)) && !$this->container
            ->findDefinition($alias)
            ->isAbstract()) {
            return new TypedReference($alias, $type, $reference->getInvalidBehavior());
        }
        $parsedName = (new Target($name))->getParsedName();
        if ($this->container
            ->has($alias = $type . ' $' . $parsedName) && !$this->container
            ->findDefinition($alias)
            ->isAbstract()) {
            return new TypedReference($alias, $type, $reference->getInvalidBehavior());
        }
        if (null !== ($alias = $this->getCombinedAlias($type, $parsedName)) && !$this->container
            ->findDefinition($alias)
            ->isAbstract()) {
            return new TypedReference($alias, $type, $reference->getInvalidBehavior());
        }
        if ($this->container
            ->has($n = $name) && !$this->container
            ->findDefinition($n)
            ->isAbstract() || $this->container
            ->has($n = $parsedName) && !$this->container
            ->findDefinition($n)
            ->isAbstract()) {
            foreach ($this->container
                ->getAliases() as $id => $alias) {
                if ($n === (string) $alias && str_starts_with($id, $type . ' $')) {
                    return new TypedReference($n, $type, $reference->getInvalidBehavior());
                }
            }
        }
        if (null !== $target) {
            return null;
        }
    }
    if ($this->container
        ->has($type) && !$this->container
        ->findDefinition($type)
        ->isAbstract()) {
        return new TypedReference($type, $type, $reference->getInvalidBehavior());
    }
    if (null !== ($alias = $this->getCombinedAlias($type)) && !$this->container
        ->findDefinition($alias)
        ->isAbstract()) {
        return new TypedReference($alias, $type, $reference->getInvalidBehavior());
    }
    return null;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal