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

Breadcrumb

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

function AttributeAutoconfigurationPass::process

Overrides AbstractRecursivePass::process

File

vendor/symfony/dependency-injection/Compiler/AttributeAutoconfigurationPass.php, line 32

Class

AttributeAutoconfigurationPass
@author Alexander M. Turek <me@derrabus.de>

Namespace

Symfony\Component\DependencyInjection\Compiler

Code

public function process(ContainerBuilder $container) : void {
    if (!$container->getAutoconfiguredAttributes()) {
        return;
    }
    foreach ($container->getAutoconfiguredAttributes() as $attributeName => $callable) {
        $callableReflector = new \ReflectionFunction($callable(...));
        if ($callableReflector->getNumberOfParameters() <= 2) {
            $this->classAttributeConfigurators[$attributeName] = $callable;
            continue;
        }
        $reflectorParameter = $callableReflector->getParameters()[2];
        $parameterType = $reflectorParameter->getType();
        $types = [];
        if ($parameterType instanceof \ReflectionUnionType) {
            foreach ($parameterType->getTypes() as $type) {
                $types[] = $type->getName();
            }
        }
        elseif ($parameterType instanceof \ReflectionNamedType) {
            $types[] = $parameterType->getName();
        }
        else {
            throw new LogicException(\sprintf('Argument "$%s" of attribute autoconfigurator should have a type, use one or more of "\\ReflectionClass|\\ReflectionMethod|\\ReflectionProperty|\\ReflectionParameter|\\Reflector" in "%s" on line "%d".', $reflectorParameter->getName(), $callableReflector->getFileName(), $callableReflector->getStartLine()));
        }
        try {
            $attributeReflector = new \ReflectionClass($attributeName);
        } catch (\ReflectionException) {
            continue;
        }
        $targets = $attributeReflector->getAttributes(\Attribute::class)[0] ?? 0;
        $targets = $targets ? $targets->getArguments()[0] ?? -1 : 0;
        foreach ([
            'class',
            'method',
            'property',
            'parameter',
        ] as $symbol) {
            if ([
                'Reflector',
            ] !== $types) {
                if (!\in_array('Reflection' . ucfirst($symbol), $types, true)) {
                    continue;
                }
                if (!($targets & \constant('Attribute::TARGET_' . strtoupper($symbol)))) {
                    throw new LogicException(\sprintf('Invalid type "Reflection%s" on argument "$%s": attribute "%s" cannot target a ' . $symbol . ' in "%s" on line "%d".', ucfirst($symbol), $reflectorParameter->getName(), $attributeName, $callableReflector->getFileName(), $callableReflector->getStartLine()));
                }
            }
            $this->{$symbol . 'AttributeConfigurators'}[$attributeName] = $callable;
        }
    }
    parent::process($container);
}
RSS feed
Powered by Drupal