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

Appearance settings

Commit 86210b3

Browse filesBrowse files
committed
bug #30889 [DependencyInjection] Fix a wrong error when using a factory (Simperfit)
This PR was merged into the 3.4 branch. Discussion ---------- [DependencyInjection] Fix a wrong error when using a factory …d a call | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #30885 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> This a work in progress, it needs tests, firstly I wanted to ask the author to test and tell if it's ok. Commits ------- 5d4e3a2 [WIP] [DependencyInjection] Fix a wrong error when using a factory and a call
2 parents fb1c154 + 5d4e3a2 commit 86210b3
Copy full SHA for 86210b3

File tree

4 files changed

+74
-2
lines changed
Filter options

4 files changed

+74
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
+9-1Lines changed: 9 additions & 1 deletion
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/ResolveBindingsPass.php
+8-1Lines 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) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
+13Lines changed: 13 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveBindingsPassTest.php
+44Lines changed: 44 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

@@ -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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.