Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 091395a

Browse files
committed
Prevent from using Lazy with Autowire
1 parent 7349202 commit 091395a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,14 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
300300
};
301301

302302
if ($checkAttributes) {
303-
$attributes = $parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF) + $parameter->getAttributes(Lazy::class, \ReflectionAttribute::IS_INSTANCEOF);
303+
$autowire = $parameter->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF);
304+
$lazy = $parameter->getAttributes(Lazy::class, \ReflectionAttribute::IS_INSTANCEOF);
305+
306+
if (\count($autowire) > 0 && \count($lazy) > 0) {
307+
throw new AutowiringFailedException($this->currentId, "'Lazy' and 'Autowire' attributes cannot be used on the same argument.");
308+
}
309+
310+
$attributes = $autowire + $lazy;
304311
foreach ($attributes as $attribute) {
305312
$attribute = $attribute->newInstance();
306313
$invalidBehavior = $parameter->allowsNull() ? ContainerInterface::NULL_ON_INVALID_REFERENCE : ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE;

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,4 +1380,17 @@ public function testLazyServiceAttribute()
13801380
$expected = new Reference('.lazy.'.A::class);
13811381
$this->assertEquals($expected, $container->getDefinition('foo')->getArgument(0));
13821382
}
1383+
1384+
public function testLazyNotCompatibleWithAutowire()
1385+
{
1386+
$container = new ContainerBuilder();
1387+
$container->register('a', A::class)->setAutowired(true);
1388+
$container->register('foo', LazyAutowireServiceAttributesAutowiring::class)->setAutowired(true);
1389+
1390+
try {
1391+
(new AutowirePass())->process($container);
1392+
} catch (AutowiringFailedException $e) {
1393+
$this->assertSame("'Lazy' and 'Autowire' attributes cannot be used on the same argument.", $e->getMessage());
1394+
}
1395+
}
13831396
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,10 @@ public function __construct(#[Lazy] A $a)
133133
{
134134
}
135135
}
136+
137+
class LazyAutowireServiceAttributesAutowiring
138+
{
139+
public function __construct(#[Lazy]#[Autowire(lazy: true)] A $a)
140+
{
141+
}
142+
}

0 commit comments

Comments
 (0)