Skip to content

Commit 3b47441

Browse files
mathieutunicolas-grekas
authored andcommitted
[HttpKernel] Make ServiceValueResolver work if controller namespace starts with a backslash in routing
1 parent 6bdaa40 commit 3b47441

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/ServiceValueResolver.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ public function supports(Request $request, ArgumentMetadata $argument)
3939

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

44-
return \is_string($controller) && $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
46+
if ('\\' === $controller[0]) {
47+
$controller = ltrim($controller, '\\');
48+
}
49+
50+
return $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
4551
}
4652

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

62+
if ('\\' === $controller[0]) {
63+
$controller = ltrim($controller, '\\');
64+
}
65+
5666
yield $this->container->get($controller)->get($argument->getName());
5767
}
5868
}

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/ServiceValueResolverTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ public function testExistingController()
4747
$this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
4848
}
4949

50+
public function testExistingControllerWithATrailingBackSlash()
51+
{
52+
$resolver = new ServiceValueResolver(new ServiceLocator(array(
53+
'App\\Controller\\Mine::method' => function () {
54+
return new ServiceLocator(array(
55+
'dummy' => function () {
56+
return new DummyService();
57+
},
58+
));
59+
},
60+
)));
61+
62+
$request = $this->requestWithAttributes(array('_controller' => '\\App\\Controller\\Mine::method'));
63+
$argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
64+
65+
$this->assertTrue($resolver->supports($request, $argument));
66+
$this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
67+
}
68+
5069
public function testControllerNameIsAnArray()
5170
{
5271
$resolver = new ServiceValueResolver(new ServiceLocator(array(

0 commit comments

Comments
 (0)