8000 bug #32541 [HttpKernel] trim the leading backslash in the controller … · nicolas-grekas/symfony@1ff2ab3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ff2ab3

Browse files
committed
bug symfony#32541 [HttpKernel] trim the leading backslash in the controller init (Simperfit, fabpot)
This PR was submitted for the 4.4 branch but it was merged into the 4.3 branch instead (closes symfony#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 | symfony#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 symfony#30045 (comment) Commits ------- 3c8d395 [HttpKernel] fixed class having a leading \ in a route controller 6fdf252 [HttpKernel] trim the leading backslash in the controller init
2 parents ece1532 + 3c8d395 commit 1ff2ab3

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Symfony/Component/HttpKernel/Controller/ContainerControllerResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ protected function createController($controller)
4747
*/
4848
protected function instantiateController($class)
4949
{
50+
$class = ltrim($class, '\\');
51+
5052
if ($this->container->has($class)) {
5153
return $this->container->get($class);
5254
}

src/Symfony/Component/HttpKernel/Tests/Controller/ContainerControllerResolverTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,36 @@ public function testGetControllerInvokableServiceWithClassNameAsName()
119119
$this->assertSame($service, $controller);
120120
}
121121

122+
/**
123+
* @dataProvider getControllers
124+
*/
125+
public function testInstantiateControllerWhenControllerStartsWithABackslash($controller)
126+
{
127+
$service = new ControllerTestService('foo');
128+
$class = ControllerTestService::class;
129+
130+
$container = $this->createMockContainer();
131+
$container->expects($this->once())->method('has')->with($class)->willReturn(true);
132+
$container->expects($this->once())->method('get')->with($class)->willReturn($service);
133+
134+
$resolver = $this->createControllerResolver(null, $container);
135+
$request = Request::create('/');
136+
$request->attributes->set('_controller', $controller);
137+
138+
$controller = $resolver->getController($request);
139+
140+
$this->assertInstanceOf(ControllerTestService::class, $controller[0]);
141+
$this->assertSame('action', $controller[1]);
142+
}
143+
144+
public function getControllers()
145+
{
146+
return [
147+
['\\'.ControllerTestService::class.'::action'],
148+
['\\'.ControllerTestService::class.':action'],
149+
];
150+
}
151+
122152
public function testExceptionWhenUsingRemovedControllerServiceWithClassNameAsName()
123153
{
124154
$this->expectException('InvalidArgumentException');

0 commit comments

Comments
 (0)
0