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

Skip to content

Commit 0ff4480

Browse files
committed
bug #24600 [HttpKernel] Don't bind scalar values to controller method arguments (yceruto)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpKernel] Don't bind scalar values to controller method arguments | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24555 (comment) | License | MIT | Doc PR | - See linked issue. Let's suppose we have this configuration: ```yaml services: _defaults: # ... bind: $foo: '%foobar%' ``` `$foo` was successfully bound to any controller constructor, but in another controller I have this edit action (nothing to do with the intention of bind such a parameter, but it has the same name): ```php /** * @route("/{foo}/edit") */ public function editAction(string $foo) {} ``` triggering: > Type error: Argument 1 passed to Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument::__construct() must be an instance of Symfony\Component\DependencyInjection\Reference, string given, called in /home/yceruto/github/symfony/symfony-demo/vendor/symfony/dependency-injection/Compiler/ServiceLocatorTagPass.php on line 81 or after #24582: > Invalid service locator definition: only services can be referenced, "string" found for key "foo". Inject parameter values using constructors instead. Commits ------- a1df9af don't bind scalar values to controller method arguments
2 parents 79caee2 + a1df9af commit 0ff4480

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public function process(ContainerBuilder $container)
135135
$binding = $bindings[$bindingName];
136136

137137
list($bindingValue, $bindingId) = $binding->getValues();
138+
139+
if (!$bindingValue instanceof Reference) {
140+
continue;
141+
}
142+
138143
$binding->setValues(array($bindingValue, $bindingId, true));
139144
$args[$p->name] = $bindingValue;
140145

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,22 @@ public function provideBindings()
310310
{
311311
return array(array(ControllerDummy::class), array('$bar'));
312312
}
313+
314+
public function testDoNotBindScalarValueToControllerArgument()
315+
{
316+
$container = new ContainerBuilder();
317+
$resolver = $container->register('argument_resolver.service')->addArgument(array());
318+
319+
$container->register('foo', ArgumentWithoutTypeController::class)
320+
->setBindings(array('$someArg' => '%foo%'))
321+
->addTag('controller.service_arguments');
322+
323+
$pass = new RegisterControllerArgumentLocatorsPass();
324+
$pass->process($container);
325+
326+
$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
327+
$this->assertEmpty($locator);
328+
}
313329
}
314330

315331
class RegisterTestController

0 commit comments

Comments
 (0)