diff --git a/src/DependencyInjection/AutowiredAttributeServicesExtension.php b/src/DependencyInjection/AutowiredAttributeServicesExtension.php index 3b8182cf53..cf92776ad2 100644 --- a/src/DependencyInjection/AutowiredAttributeServicesExtension.php +++ b/src/DependencyInjection/AutowiredAttributeServicesExtension.php @@ -19,6 +19,7 @@ use stdClass; use function explode; use function strcasecmp; +use function strtolower; use function substr; final class AutowiredAttributeServicesExtension extends CompilerExtension @@ -39,6 +40,15 @@ public function loadConfiguration(): void $builder = $this->getContainerBuilder(); $autowiredParameters = Attributes::findTargetMethodParameters(AutowiredParameter::class); + $constructorParameters = []; + foreach ($autowiredParameters as $parameter) { + if (strcasecmp($parameter->method, '__construct') !== 0) { + continue; + } + $lowerClass = strtolower($parameter->class); + $constructorParameters[$lowerClass] ??= []; + $constructorParameters[$lowerClass][] = $parameter; + } foreach (Attributes::findTargetClasses(AutowiredService::class) as $class) { $reflection = new ReflectionClass($class->name); @@ -53,7 +63,7 @@ public function loadConfiguration(): void $definition->setFactory(new Statement([new Reference(substr($ref, 1)), $method])); } - $this->processParameters($class->name, $definition, $autowiredParameters); + $this->processConstructorParameters($class->name, $definition, $constructorParameters); foreach (ValidateServiceTagsExtension::INTERFACE_TAG_MAPPING as $interface => $tag) { if (!$reflection->implementsInterface($interface)) { @@ -76,7 +86,7 @@ public function loadConfiguration(): void $definition->setFactory(new Statement([new Reference(substr($ref, 1)), $method])); } - $this->processParameters($class->name, $definition, $autowiredParameters); + $this->processConstructorParameters($class->name, $definition, $constructorParameters); } foreach (Attributes::findTargetClasses(GenerateFactory::class) as $class) { @@ -89,7 +99,7 @@ public function loadConfiguration(): void } $resultDefinition = $definition->getResultDefinition(); - $this->processParameters($class->name, $resultDefinition, $autowiredParameters); + $this->processConstructorParameters($class->name, $resultDefinition, $constructorParameters); } /** @var stdClass&object{level: int|null} $config */ @@ -109,7 +119,7 @@ public function loadConfiguration(): void ->setAutowired($class->name) ->addTag(LazyRegistry::RULE_TAG); - $this->processParameters($class->name, $definition, $autowiredParameters); + $this->processConstructorParameters($class->name, $definition, $constructorParameters); } foreach (Attributes::findTargetClasses(RegisteredCollector::class) as $class) { @@ -123,24 +133,18 @@ public function loadConfiguration(): void ->setAutowired($class->name) ->addTag(RegistryFactory::COLLECTOR_TAG); - $this->processParameters($class->name, $definition, $autowiredParameters); + $this->processConstructorParameters($class->name, $definition, $constructorParameters); } } /** * @param class-string $className - * @param TargetMethodParameter[] $autowiredParameters + * @param array>> $constructorParameters */ - private function processParameters(string $className, ServiceDefinition $definition, array $autowiredParameters): void + private function processConstructorParameters(string $className, ServiceDefinition $definition, array $constructorParameters): void { $builder = $this->getContainerBuilder(); - foreach ($autowiredParameters as $autowiredParameter) { - if (strcasecmp($autowiredParameter->method, '__construct') !== 0) { - continue; - } - if (strcasecmp($autowiredParameter->class, $className) !== 0) { - continue; - } + foreach ($constructorParameters[strtolower($className)] ?? [] as $autowiredParameter) { $ref = $autowiredParameter->attribute->ref; if ($ref === null) { $argument = Helpers::expand(