Skip to content

Commit

Permalink
bug #26773 [HttpKernel] Make ServiceValueResolver work if controller …
Browse files Browse the repository at this point in the history
…namespace starts with a backslash in routing (mathieutu)

This PR was submitted for the 4.0 branch but it was squashed and merged into the 3.4 branch instead (closes #26773).

Discussion
----------

[HttpKernel] Make ServiceValueResolver work if controller namespace starts with a backslash in routing

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26772
| License       | MIT

Hi folks,

This PR fixes #26772:
>Controller "App\Controllers\Foo" requires that you provide a value for the "$foo" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

Thank you for your work!

PS: This is my first (of many planned!) PR to Symfony, so please let me know if I did something wrong.

Commits
-------

3b47441 [HttpKernel] Make ServiceValueResolver work if controller namespace starts with a backslash in routing
  • Loading branch information
nicolas-grekas committed Apr 14, 2018
2 parents 6bdaa40 + 3b47441 commit 811c4dd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Expand Up @@ -39,9 +39,15 @@ public function supports(Request $request, ArgumentMetadata $argument)

if (\is_array($controller) && \is_callable($controller, true) && \is_string($controller[0])) {
$controller = $controller[0].'::'.$controller[1];
} elseif (!\is_string($controller) || '' === $controller) {
return false;
}

return \is_string($controller) && $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
if ('\\' === $controller[0]) {
$controller = ltrim($controller, '\\');
}

return $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
}

/**
Expand All @@ -53,6 +59,10 @@ public function resolve(Request $request, ArgumentMetadata $argument)
$controller = $controller[0].'::'.$controller[1];
}

if ('\\' === $controller[0]) {
$controller = ltrim($controller, '\\');
}

yield $this->container->get($controller)->get($argument->getName());
}
}
Expand Up @@ -47,6 +47,25 @@ public function testExistingController()
$this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
}

public function testExistingControllerWithATrailingBackSlash()
{
$resolver = new ServiceValueResolver(new ServiceLocator(array(
'App\\Controller\\Mine::method' => function () {
return new ServiceLocator(array(
'dummy' => function () {
return new DummyService();
},
));
},
)));

$request = $this->requestWithAttributes(array('_controller' => '\\App\\Controller\\Mine::method'));
$argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);

$this->assertTrue($resolver->supports($request, $argument));
$this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
}

public function testControllerNameIsAnArray()
{
$resolver = new ServiceValueResolver(new ServiceLocator(array(
Expand Down

0 comments on commit 811c4dd

Please sign in to comment.