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

Breadcrumb

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

class ResolveAutowireInlineAttributesPass

Inspects existing autowired services for { @author Ismail Özgün Turan <oezguen.turan@dadadev.com>

Hierarchy

  • class \Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass implements \Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface
    • class \Symfony\Component\DependencyInjection\Compiler\ResolveAutowireInlineAttributesPass extends \Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass

Expanded class hierarchy of ResolveAutowireInlineAttributesPass

See also

AutowireInline} attributes and registers the definitions for reuse.

File

vendor/symfony/dependency-injection/Compiler/ResolveAutowireInlineAttributesPass.php, line 27

Namespace

Symfony\Component\DependencyInjection\Compiler
View source
class ResolveAutowireInlineAttributesPass extends AbstractRecursivePass {
    protected bool $skipScalars = true;
    private int $counter;
    protected function processValue(mixed $value, bool $isRoot = false) : mixed {
        $value = parent::processValue($value, $isRoot);
        if (!$value instanceof Definition || !$value->isAutowired() || !$value->getClass() || $value->hasTag('container.ignore_attributes')) {
            return $value;
        }
        if ($isRoot) {
            $this->counter = 0;
        }
        $isChildDefinition = $value instanceof ChildDefinition;
        try {
            $constructor = $this->getConstructor($value, false);
        } catch (RuntimeException) {
            return $value;
        }
        if ($constructor) {
            $arguments = $this->registerAutowireInlineAttributes($constructor, $value->getArguments(), $isChildDefinition);
            if ($arguments !== $value->getArguments()) {
                $value->setArguments($arguments);
            }
        }
        $methodCalls = $value->getMethodCalls();
        foreach ($methodCalls as $i => $call) {
            [
                $method,
                $arguments,
            ] = $call;
            try {
                $method = $this->getReflectionMethod($value, $method);
            } catch (RuntimeException) {
                continue;
            }
            $arguments = $this->registerAutowireInlineAttributes($method, $arguments, $isChildDefinition);
            if ($arguments !== $call[1]) {
                $methodCalls[$i][1] = $arguments;
            }
        }
        if ($methodCalls !== $value->getMethodCalls()) {
            $value->setMethodCalls($methodCalls);
        }
        return $value;
    }
    private function registerAutowireInlineAttributes(\ReflectionFunctionAbstract $method, array $arguments, bool $isChildDefinition) : array {
        $parameters = $method->getParameters();
        if ($method->isVariadic()) {
            array_pop($parameters);
        }
        $paramResolverContainer = new ContainerBuilder($this->container
            ->getParameterBag());
        foreach ($parameters as $index => $parameter) {
            if ($isChildDefinition) {
                $index = 'index_' . $index;
            }
            if (\array_key_exists('$' . $parameter->name, $arguments) || \array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
                $attribute = \array_key_exists('$' . $parameter->name, $arguments) ? $arguments['$' . $parameter->name] : $arguments[$index];
                if (!$attribute instanceof AutowireInline) {
                    continue;
                }
            }
            elseif (!($attribute = $parameter->getAttributes(AutowireInline::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null)) {
                continue;
            }
            else {
                $attribute = $attribute->newInstance();
            }
            $type = ProxyHelper::exportType($parameter, true);
            if (!$type && isset($arguments[$index])) {
                continue;
            }
            $definition = $attribute->buildDefinition($attribute->value, $type, $parameter);
            $paramResolverContainer->setDefinition('.autowire_inline', $definition);
            (new ResolveParameterPlaceHoldersPass(false, false))->process($paramResolverContainer);
            $id = '.autowire_inline.' . $this->currentId . '.' . ++$this->counter;
            $this->container
                ->setDefinition($id, $definition);
            $arguments[$isChildDefinition ? '$' . $parameter->name : $index] = new Reference($id);
            if ($definition->isAutowired()) {
                $this->processValue($definition);
            }
        }
        return $arguments;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AbstractRecursivePass::$container protected property
AbstractRecursivePass::$currentId protected property
AbstractRecursivePass::$expressionLanguage private property 1
AbstractRecursivePass::$inExpression private property
AbstractRecursivePass::$processExpressions private property
AbstractRecursivePass::enableExpressionProcessing protected function
AbstractRecursivePass::getConstructor protected function
AbstractRecursivePass::getExpressionLanguage private function 1
AbstractRecursivePass::getReflectionMethod protected function
AbstractRecursivePass::inExpression protected function
AbstractRecursivePass::process public function Overrides CompilerPassInterface::process 15
ResolveAutowireInlineAttributesPass::$counter private property
ResolveAutowireInlineAttributesPass::$skipScalars protected property Overrides AbstractRecursivePass::$skipScalars
ResolveAutowireInlineAttributesPass::processValue protected function Processes a value found in a definition tree. Overrides AbstractRecursivePass::processValue
ResolveAutowireInlineAttributesPass::registerAutowireInlineAttributes private function
RSS feed
Powered by Drupal