Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitee6a55d

Browse files
Merge branch '6.3' into 6.4
* 6.3: [DependencyInjection][HttpKernel] Fix using `#[AutowireCallable]` with controller arguments
2 parentsf77ca5c +2f8af44 commitee6a55d

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

‎src/Symfony/Component/DependencyInjection/Attribute/AutowireCallable.php‎

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

1212
namespaceSymfony\Component\DependencyInjection\Attribute;
1313

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

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

3940
parent::__construct($callable ?? [newReference($service),$method ??'__invoke'], lazy:$lazy);
4041
}
42+
43+
publicfunctionbuildDefinition(mixed$value, ?string$type,\ReflectionParameter$parameter):Definition
44+
{
45+
return (newDefinition($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
}

‎src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php‎

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

318318
if ($attributeinstanceof AutowireCallable) {
319-
$value = (newDefinition($type =\is_string($attribute->lazy) ?$attribute->lazy : ($type ?:'Closure')))
320-
->setFactory(['Closure','fromCallable'])
321-
->setArguments([\is_array($value) ?$value + [1 =>'__invoke'] :$value])
322-
->setLazy($attribute->lazy ||'Closure' !==$type &&'callable' !== (string)$parameter->getType());
319+
$value =$attribute->buildDefinition($value,$type,$parameter);
323320
}elseif ($lazy =$attribute->lazy) {
324321
$definition = (newDefinition($type))
325322
->setFactory('current')

‎src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\HttpKernel\DependencyInjection;
1313

1414
useSymfony\Component\DependencyInjection\Attribute\Autowire;
15+
useSymfony\Component\DependencyInjection\Attribute\AutowireCallable;
1516
useSymfony\Component\DependencyInjection\Attribute\Target;
1617
useSymfony\Component\DependencyInjection\ChildDefinition;
1718
useSymfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
@@ -160,7 +161,12 @@ public function process(ContainerBuilder $container)
160161
}
161162

162163
if ($autowireAttributes) {
163-
$value =$autowireAttributes[0]->newInstance()->value;
164+
$attribute =$autowireAttributes[0]->newInstance();
165+
$value =$parameterBag->resolveValue($attribute->value);
166+
167+
if ($attributeinstanceof AutowireCallable) {
168+
$value =$attribute->buildDefinition($value,$type,$p);
169+
}
164170

165171
if ($valueinstanceof Reference) {
166172
$args[$p->name] =$type ?newTypedReference($value,$type,$invalidBehavior,$p->name) :newReference($value,$invalidBehavior);

‎src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
namespaceSymfony\Component\HttpKernel\Tests\DependencyInjection;
1313

1414
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\DependencyInjection\Argument\LazyClosure;
1516
useSymfony\Component\DependencyInjection\Argument\RewindableGenerator;
1617
useSymfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1718
useSymfony\Component\DependencyInjection\Attribute\Autowire;
19+
useSymfony\Component\DependencyInjection\Attribute\AutowireCallable;
1820
useSymfony\Component\DependencyInjection\Attribute\TaggedIterator;
1921
useSymfony\Component\DependencyInjection\Attribute\TaggedLocator;
2022
useSymfony\Component\DependencyInjection\Attribute\Target;
@@ -479,7 +481,7 @@ public function testAutowireAttribute()
479481

480482
$locator =$container->get($locatorId)->get('foo::fooAction');
481483

482-
$this->assertCount(8,$locator->getProvidedServices());
484+
$this->assertCount(9,$locator->getProvidedServices());
483485
$this->assertInstanceOf(\stdClass::class,$locator->get('service1'));
484486
$this->assertSame('foo/bar',$locator->get('value'));
485487
$this->assertSame('foo',$locator->get('expression'));
@@ -488,6 +490,9 @@ public function testAutowireAttribute()
488490
$this->assertSame('bar',$locator->get('rawValue'));
489491
$this->assertSame('@bar',$locator->get('escapedRawValue'));
490492
$this->assertSame('foo',$locator->get('customAutowire'));
493+
$this->assertInstanceOf(FooInterface::class,$autowireCallable =$locator->get('autowireCallable'));
494+
$this->assertInstanceOf(LazyClosure::class,$autowireCallable);
495+
$this->assertInstanceOf(\stdClass::class,$autowireCallable->service);
491496
$this->assertFalse($locator->has('service2'));
492497
}
493498

@@ -628,6 +633,11 @@ public function __construct(string $parameter)
628633
}
629634
}
630635

636+
interface FooInterface
637+
{
638+
publicfunctionfoo();
639+
}
640+
631641
class WithAutowireAttribute
632642
{
633643
publicfunctionfooAction(
@@ -647,6 +657,8 @@ public function fooAction(
647657
string$escapedRawValue,
648658
#[CustomAutowire('some.parameter')]
649659
string$customAutowire,
660+
#[AutowireCallable(service:'some.id', method:'bar')]
661+
FooInterface$autowireCallable,
650662
#[Autowire(service:'invalid.id')]
651663
\stdClass$service2 =null,
652664
) {

‎src/Symfony/Component/HttpKernel/composer.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"symfony/config":"^6.1|^7.0",
3131
"symfony/console":"^5.4|^6.0|^7.0",
3232
"symfony/css-selector":"^5.4|^6.0|^7.0",
33-
"symfony/dependency-injection":"^6.3|^7.0",
33+
"symfony/dependency-injection":"^6.3.4|^7.0",
3434
"symfony/dom-crawler":"^5.4|^6.0|^7.0",
3535
"symfony/expression-language":"^5.4|^6.0|^7.0",
3636
"symfony/finder":"^5.4|^6.0|^7.0",
@@ -57,7 +57,7 @@
5757
"symfony/config":"<6.1",
5858
"symfony/console":"<5.4",
5959
"symfony/form":"<5.4",
60-
"symfony/dependency-injection":"<6.3",
60+
"symfony/dependency-injection":"<6.3.4",
6161
"symfony/doctrine-bridge":"<5.4",
6262
"symfony/http-client":"<5.4",
6363
"symfony/http-client-contracts":"<2.5",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp