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

Breadcrumb

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

function AutowireRequiredMethodsPass::processValue

Overrides AbstractRecursivePass::processValue

File

vendor/symfony/dependency-injection/Compiler/AutowireRequiredMethodsPass.php, line 26

Class

AutowireRequiredMethodsPass
Looks for definitions with autowiring enabled and registers their corresponding "#[Required]" methods as setters.

Namespace

Symfony\Component\DependencyInjection\Compiler

Code

protected function processValue(mixed $value, bool $isRoot = false) : mixed {
    $value = parent::processValue($value, $isRoot);
    if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
        return $value;
    }
    if (!($reflectionClass = $this->container
        ->getReflectionClass($value->getClass(), false))) {
        return $value;
    }
    $alreadyCalledMethods = [];
    $withers = [];
    foreach ($value->getMethodCalls() as [
        $method,
    ]) {
        $alreadyCalledMethods[strtolower($method)] = true;
    }
    foreach ($reflectionClass->getMethods() as $reflectionMethod) {
        $r = $reflectionMethod;
        if ($r->isConstructor() || isset($alreadyCalledMethods[strtolower($r->name)])) {
            continue;
        }
        while (true) {
            if ($r->getAttributes(Required::class)) {
                if ($this->isWither($r, $r->getDocComment() ?: '')) {
                    $withers[] = [
                        $r->name,
                        [],
                        true,
                    ];
                }
                else {
                    $value->addMethodCall($r->name, []);
                }
                break;
            }
            try {
                $r = $r->getPrototype();
            } catch (\ReflectionException) {
                break;
                // method has no prototype
            }
        }
    }
    if ($withers) {
        // Prepend withers to prevent creating circular loops
        $setters = $value->getMethodCalls();
        $value->setMethodCalls($withers);
        foreach ($setters as $call) {
            $value->addMethodCall($call[0], $call[1], $call[2] ?? false);
        }
    }
    return $value;
}
RSS feed
Powered by Drupal