Skip to content

Commit 8a34076

Browse files
committed
[DependencyInjection][HttpKernel] Fix using #[AutowireCallable] with controller arguments
1 parent 474cfbc commit 8a34076

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Attribute/AutowireCallable.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Attribute;
1313

14+
use Symfony\Component\DependencyInjection\Definition;
1415
use Symfony\Component\DependencyInjection\Exception\LogicException;
1516
use Symfony\Component\DependencyInjection\Reference;
1617

@@ -38,4 +39,12 @@ public function __construct(
3839

3940
parent::__construct($callable ?? [new Reference($service), $method ?? '__invoke'], lazy: $lazy);
4041
}
42+
43+
public function buildDefinition(mixed $value, ?string $type, \ReflectionParameter $parameter): Definition
44+
{
45+
return (new Definition($type = \is_string($this->lazy) ? $this->lazy : ($type ?: 'Closure')))
46+
->setFactory(['Closure', 'fromCallable'])
47+
->setArguments([\is_array($value) ? $value + [1 => '__invoke'] : $value])
48+
->setLazy($this->lazy || 'Closure' !== $type && 'callable' !== (string) $parameter->getType());
49+
}
4150
}

Compiler/AutowirePass.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
314314
}
315315

316316
if ($attribute instanceof AutowireCallable) {
317-
$value = (new Definition($type = \is_string($attribute->lazy) ? $attribute->lazy : ($type ?: 'Closure')))
318-
->setFactory(['Closure', 'fromCallable'])
319-
->setArguments([\is_array($value) ? $value + [1 => '__invoke'] : $value])
320-
->setLazy($attribute->lazy || 'Closure' !== $type && 'callable' !== (string) $parameter->getType());
317+
$value = $attribute->buildDefinition($value, $type, $parameter);
321318
} elseif ($lazy = $attribute->lazy) {
322319
$definition = (new Definition($type))
323320
->setFactory('current')

0 commit comments

Comments
 (0)