|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\DependencyInjection\Compiler;
|
13 | 13 |
|
| 14 | +use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; |
| 15 | +use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; |
| 16 | +use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; |
| 17 | +use Symfony\Component\DependencyInjection\Attribute\TaggedLocator; |
14 | 18 | use Symfony\Component\DependencyInjection\ChildDefinition;
|
15 | 19 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
16 | 20 | use Symfony\Component\DependencyInjection\Definition;
|
|
20 | 24 | */
|
21 | 25 | final class AttributeAutoconfigurationPass extends AbstractRecursivePass
|
22 | 26 | {
|
| 27 | + private $classAttributeConfigurators = []; |
| 28 | + private $parameterAttributeConfigurators = []; |
| 29 | + |
23 | 30 | public function process(ContainerBuilder $container): void
|
24 | 31 | {
|
25 |
| - if (80000 > \PHP_VERSION_ID || !$container->getAutoconfiguredAttributes()) { |
| 32 | + if (80000 > \PHP_VERSION_ID) { |
26 | 33 | return;
|
27 | 34 | }
|
28 | 35 |
|
| 36 | + $this->classAttributeConfigurators = $container->getAutoconfiguredAttributes(); |
| 37 | + |
29 | 38 | parent::process($container);
|
30 | 39 | }
|
31 | 40 |
|
32 | 41 | protected function processValue($value, bool $isRoot = false)
|
33 | 42 | {
|
34 | 43 | if (!$value instanceof Definition
|
35 |
| - || !$value->isAutoconfigured() |
36 | 44 | || $value->isAbstract()
|
37 | 45 | || $value->hasTag('container.ignore_attributes')
|
38 |
| - || !($reflector = $this->container->getReflectionClass($value->getClass(), false)) |
| 46 | + || !($classReflector = $this->container->getReflectionClass($value->getClass(), false)) |
39 | 47 | ) {
|
40 | 48 | return parent::processValue($value, $isRoot);
|
41 | 49 | }
|
42 | 50 |
|
43 |
| - $autoconfiguredAttributes = $this->container->getAutoconfiguredAttributes(); |
| 51 | + $classAttributeConfigurators = $value->isAutoconfigured() ? $this->classAttributeConfigurators : []; |
| 52 | + $parameterAttributeConfigurators = array_merge( |
| 53 | + $value->isAutoconfigured() ? $this->parameterAttributeConfigurators : [], |
| 54 | + [ |
| 55 | + TaggedIterator::class => static function (ChildDefinition $definition, TaggedIterator $attribute, \ReflectionParameter $reflector) { |
| 56 | + $definition->setArgument($reflector->getPosition(), new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, null, false)); |
| 57 | + }, |
| 58 | + TaggedLocator::class => static function (ChildDefinition $definition, TaggedLocator $attribute, \ReflectionParameter $reflector) { |
| 59 | + $definition->setArgument($reflector->getPosition(), new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, null, true))); |
| 60 | + }, |
| 61 | + ] |
| 62 | + ); |
| 63 | + |
44 | 64 | $instanceof = $value->getInstanceofConditionals();
|
45 |
| - $conditionals = $instanceof[$reflector->getName()] ?? new ChildDefinition(''); |
46 |
| - foreach ($reflector->getAttributes() as $attribute) { |
47 |
| - if ($configurator = $autoconfiguredAttributes[$attribute->getName()] ?? null) { |
48 |
| - $configurator($conditionals, $attribute->newInstance(), $reflector); |
| 65 | + $conditionals = $instanceof[$classReflector->getName()] ?? new ChildDefinition(''); |
| 66 | + |
| 67 | + if ($classAttributeConfigurators) { |
| 68 | + foreach ($classReflector->getAttributes() as $attribute) { |
| 69 | + if ($configurator = $classAttributeConfigurators[$attribute->getName()] ?? null) { |
| 70 | + $configurator($conditionals, $attribute->newInstance(), $classReflector); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if ($parameterAttributeConfigurators && $constructorReflector = $this->getConstructor($value, false)) { |
| 76 | + foreach ($constructorReflector->getParameters() as $parameterReflector) { |
| 77 | + foreach ($parameterReflector->getAttributes() as $attribute) { |
| 78 | + if ($configurator = $parameterAttributeConfigurators[$attribute->getName()] ?? null) { |
| 79 | + $configurator($conditionals, $attribute->newInstance(), $parameterReflector); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if ($parameterAttributeConfigurators) { |
| 86 | + foreach ($classReflector->getMethods(\ReflectionMethod::IS_PUBLIC) as $methodReflector) { |
| 87 | + if ($methodReflector->isStatic() || $methodReflector->isConstructor() || $methodReflector->isDestructor()) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + if ($parameterAttributeConfigurators) { |
| 92 | + foreach ($methodReflector->getParameters() as $parameterReflector) { |
| 93 | + foreach ($parameterReflector->getAttributes() as $attribute) { |
| 94 | + if ($configurator = $parameterAttributeConfigurators[$attribute->getName()] ?? null) { |
| 95 | + $configurator($conditionals, $attribute->newInstance(), $parameterReflector); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
49 | 100 | }
|
50 | 101 | }
|
51 |
| - if (!isset($instanceof[$reflector->getName()]) && new ChildDefinition('') != $conditionals) { |
52 |
| - $instanceof[$reflector->getName()] = $conditionals; |
| 102 | + |
| 103 | + if (!isset($instanceof[$classReflector->getName()]) && new ChildDefinition('') != $conditionals) { |
| 104 | + $instanceof[$classReflector->getName()] = $conditionals; |
53 | 105 | $value->setInstanceofConditionals($instanceof);
|
54 | 106 | }
|
55 | 107 |
|
|
0 commit comments