Skip to content

Commit e607a7f

Browse files
committed
Merge branch '3.4' into 4.2
* 3.4: [WIP] [DependencyInjection] Fix a wrong error when using a factory and a call
2 parents 1806e43 + 5243aca commit e607a7f

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

Compiler/AutowirePass.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,15 @@ private function autowireCalls(\ReflectionClass $reflectionClass, bool $isRoot):
159159
if ($method instanceof \ReflectionFunctionAbstract) {
160160
$reflectionMethod = $method;
161161
} else {
162-
$reflectionMethod = $this->getReflectionMethod(new Definition($reflectionClass->name), $method);
162+
$definition = new Definition($reflectionClass->name);
163+
try {
164+
$reflectionMethod = $this->getReflectionMethod($definition, $method);
165+
} catch (RuntimeException $e) {
166+
if ($definition->getFactory()) {
167+
continue;
168+
}
169+
throw $e;
170+
}
163171
}
164172

165173
$arguments = $this->autowireMethod($reflectionMethod, $arguments);

Compiler/ResolveBindingsPass.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,14 @@ protected function processValue($value, $isRoot = false)
115115
if ($method instanceof \ReflectionFunctionAbstract) {
116116
$reflectionMethod = $method;
117117
} else {
118-
$reflectionMethod = $this->getReflectionMethod($value, $method);
118+
try {
119+
$reflectionMethod = $this->getReflectionMethod($value, $method);
120+
} catch (RuntimeException $e) {
121+
if ($value->getFactory()) {
122+
continue;
123+
}
124+
throw $e;
125+
}
119126
}
120127

121128
foreach ($reflectionMethod->getParameters() as $key => $parameter) {

Tests/Compiler/AutowirePassTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\DependencyInjection\Reference;
2828
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic;
2929
use Symfony\Component\DependencyInjection\TypedReference;
30+
use Symfony\Component\HttpKernel\HttpKernelInterface;
3031

3132
require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';
3233

@@ -546,6 +547,18 @@ public function testSetterInjection()
546547
);
547548
}
548549

550+
public function testWithNonExistingSetterAndAutowiring()
551+
{
552+
$container = new ContainerBuilder();
553+
554+
$definition = $container->register(HttpKernelInterface::class, HttpKernelInterface::class)->setAutowired(true);
555+
$definition->addMethodCall('setLogger');
556+
$this->expectException(RuntimeException::class);
557+
(new ResolveClassPass())->process($container);
558+
(new AutowireRequiredMethodsPass())->process($container);
559+
(new AutowirePass())->process($container);
560+
}
561+
549562
public function testExplicitMethodInjection()
550563
{
551564
$container = new ContainerBuilder();

Tests/Compiler/ResolveBindingsPassTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
1717
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
1818
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Definition;
20+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1921
use Symfony\Component\DependencyInjection\Reference;
2022
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
2123
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
2224
use Symfony\Component\DependencyInjection\Tests\Fixtures\ParentNotExists;
2325
use Symfony\Component\DependencyInjection\TypedReference;
26+
use Symfony\Component\HttpKernel\HttpKernelInterface;
2427

2528
require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';
2629

@@ -112,6 +115,23 @@ public function testScalarSetter()
112115
$this->assertEquals([['setDefaultLocale', ['fr']]], $definition->getMethodCalls());
113116
}
114117

118+
public function testWithNonExistingSetterAndBinding()
119+
{
120+
$container = new ContainerBuilder();
121+
122+
$bindings = [
123+
'$c' => (new Definition('logger'))->setFactory('logger'),
124+
];
125+
126+
$definition = $container->register(HttpKernelInterface::class, HttpKernelInterface::class);
127+
$definition->addMethodCall('setLogger');
128+
$definition->setBindings($bindings);
129+
$this->expectException(RuntimeException::class);
130+
131+
$pass = new ResolveBindingsPass();
132+
$pass->process($container);
133+
}
134+
115135
public function testTupleBinding()
116136
{
117137
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)