Skip to content

Commit

Permalink
bug #32541 [HttpKernel] trim the leading backslash in the controller …
Browse files Browse the repository at this point in the history
…init (Simperfit, fabpot)

This PR was submitted for the 4.4 branch but it was merged into the 4.3 branch instead (closes #32541).

Discussion
----------

[HttpKernel] trim the leading backslash in the controller init

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #29945
| License       | MIT
| Doc PR        | none <!-- required for new features -->

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

This fixes an error where the classes exists when using a leading backslash in the controller, it's not invalid to do so.

see #30045 (comment)

Commits
-------

3c8d395 [HttpKernel] fixed class having a leading \ in a route controller
6fdf252 [HttpKernel] trim the leading backslash in the controller init
  • Loading branch information
fabpot committed Aug 9, 2019
2 parents ece1532 + 3c8d395 commit 1ff2ab3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ protected function createController($controller)
*/
protected function instantiateController($class)
{
$class = ltrim($class, '\\');

if ($this->container->has($class)) {
return $this->container->get($class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,36 @@ public function testGetControllerInvokableServiceWithClassNameAsName()
$this->assertSame($service, $controller);
}

/**
* @dataProvider getControllers
*/
public function testInstantiateControllerWhenControllerStartsWithABackslash($controller)
{
$service = new ControllerTestService('foo');
$class = ControllerTestService::class;

$container = $this->createMockContainer();
$container->expects($this->once())->method('has')->with($class)->willReturn(true);
$container->expects($this->once())->method('get')->with($class)->willReturn($service);

$resolver = $this->createControllerResolver(null, $container);
$request = Request::create('/');
$request->attributes->set('_controller', $controller);

$controller = $resolver->getController($request);

$this->assertInstanceOf(ControllerTestService::class, $controller[0]);
$this->assertSame('action', $controller[1]);
}

public function getControllers()
{
return [
['\\'.ControllerTestService::class.'::action'],
['\\'.ControllerTestService::class.':action'],
];
}

public function testExceptionWhenUsingRemovedControllerServiceWithClassNameAsName()
{
$this->expectException('InvalidArgumentException');
Expand Down

0 comments on commit 1ff2ab3

Please sign in to comment.