Skip to content

Commit 5d4e3a2

Browse files
author
Amrouche Hamza
committed
[WIP] [DependencyInjection] Fix a wrong error when using a factory and a call
1 parent d77ea48 commit 5d4e3a2

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,15 @@ private function autowireCalls(\ReflectionClass $reflectionClass, array $methodC
181181
if ($method instanceof \ReflectionFunctionAbstract) {
182182
$reflectionMethod = $method;
183183
} else {
184-
$reflectionMethod = $this->getReflectionMethod(new Definition($reflectionClass->name), $method);
184+
$definition = new Definition($reflectionClass->name);
185+
try {
186+
$reflectionMethod = $this->getReflectionMethod($definition, $method);
187+
} catch (RuntimeException $e) {
188+
if ($definition->getFactory()) {
189+
continue;
190+
}
191+
throw $e;
192+
}
185193
}
186194

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

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

+8-1
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) {

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

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\DependencyInjection\Reference;
2424
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic;
2525
use Symfony\Component\DependencyInjection\TypedReference;
26+
use Symfony\Component\HttpKernel\HttpKernelInterface;
2627

2728
require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';
2829

@@ -605,6 +606,18 @@ public function testSetterInjection()
605606
);
606607
}
607608

609+
public function testWithNonExistingSetterAndAutowiring()
610+
{
611+
$container = new ContainerBuilder();
612+
613+
$definition = $container->register(HttpKernelInterface::class, HttpKernelInterface::class)->setAutowired(true);
614+
$definition->addMethodCall('setLogger');
615+
$this->expectException(RuntimeException::class);
616+
(new ResolveClassPass())->process($container);
617+
(new AutowireRequiredMethodsPass())->process($container);
618+
(new AutowirePass())->process($container);
619+
}
620+
608621
public function testExplicitMethodInjection()
609622
{
610623
$container = new ContainerBuilder();

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

+44
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

@@ -111,4 +114,45 @@ public function testScalarSetter()
111114

112115
$this->assertEquals([['setDefaultLocale', ['fr']]], $definition->getMethodCalls());
113116
}
117+
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+
135+
public function testTupleBinding()
136+
{
137+
$container = new ContainerBuilder();
138+
139+
$bindings = [
140+
'$c' => new BoundArgument(new Reference('bar')),
141+
CaseSensitiveClass::class.'$c' => new BoundArgument(new Reference('foo')),
142+
];
143+
144+
$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
145+
$definition->addMethodCall('setSensitiveClass');
146+
$definition->addMethodCall('setAnotherC');
147+
$definition->setBindings($bindings);
148+
149+
$pass = new ResolveBindingsPass();
150+
$pass->process($container);
151+
152+
$expected = [
153+
['setSensitiveClass', [new Reference('foo')]],
154+
['setAnotherC', [new Reference('bar')]],
155+
];
156+
$this->assertEquals($expected, $definition->getMethodCalls());
157+
}
114158
}

0 commit comments

Comments
 (0)