Skip to content

Commit

Permalink
bug #24600 [HttpKernel] Don't bind scalar values to controller method…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
fabpot committed Oct 18, 2017
2 parents 79caee2 + a1df9af commit 0ff4480
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Expand Up @@ -135,6 +135,11 @@ public function process(ContainerBuilder $container)
$binding = $bindings[$bindingName];

list($bindingValue, $bindingId) = $binding->getValues();

if (!$bindingValue instanceof Reference) {
continue;
}

$binding->setValues(array($bindingValue, $bindingId, true));
$args[$p->name] = $bindingValue;

Expand Down
Expand Up @@ -310,6 +310,22 @@ public function provideBindings()
{
return array(array(ControllerDummy::class), array('$bar'));
}

public function testDoNotBindScalarValueToControllerArgument()
{
$container = new ContainerBuilder();
$resolver = $container->register('argument_resolver.service')->addArgument(array());

$container->register('foo', ArgumentWithoutTypeController::class)
->setBindings(array('$someArg' => '%foo%'))
->addTag('controller.service_arguments');

$pass = new RegisterControllerArgumentLocatorsPass();
$pass->process($container);

$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
$this->assertEmpty($locator);
}
}

class RegisterTestController
Expand Down

0 comments on commit 0ff4480

Please sign in to comment.