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