Skip to content

Commit

Permalink
[HttpKernel] Make ServiceValueResolver work if controller namespace s…
Browse files Browse the repository at this point in the history
…tarts with a backslash in routing
  • Loading branch information
mathieutu authored and nicolas-grekas committed Apr 14, 2018
1 parent 6bdaa40 commit 3b47441
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 3b47441

Please sign in to comment.