Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to test first and then trim? Why not always trim?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trim means triggering copy-on-write when the controller string usually comes from shared memory, which means also recomputing the hash of the string, even if it doesn't change.

$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());
}
}
Original file line number Diff line number Diff line change
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