8000 [FrameworkBundle] Return the invokable service if its name is the cla… · symfony/symfony@5c87d76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c87d76

Browse files
committed
[FrameworkBundle] Return the invokable service if its name is the class name
1 parent ba1cd26 commit 5c87d76

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ protected function createController($controller)
7878
*/
7979
protected function instantiateController($class)
8080
{
81+
if ($this->container->has($class)) {
82+
return $this->container->get($class);
83+
}
84+
8185
$controller = parent::instantiateController($class);
8286

8387
if ($controller instanceof ContainerAwareInterface) {

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerResolverTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public function testGetControllerService()
8787

8888
public function testGetControllerInvokableService()
8989
{
90+
$invokableController = new InvokableController('bar');
91+
9092
$container = $this->createMockContainer();
9193
$container->expects($this->once())
9294
->method('has')
@@ -96,7 +98,7 @@ public function testGetControllerInvokableService()
9698
$container->expects($this->once())
9799
->method('get')
98100
->with('foo')
99-
->will($this->returnValue($this))
101+
->will($this->returnValue($invokableController))
100102
;
101103

102104
$resolver = $this->createControllerResolver(null, null, $container);
@@ -105,7 +107,33 @@ public function testGetControllerInvokableService()
105107

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

108-
$this->assertInstanceOf(get_class($this), $controller);
110+
$this->assertEquals($invokableController, $controller);
111+
}
112+
113+
public function testGetControllerInvokableServiceWithClassNameAsName()
114+
{
115+
$invokableController = new InvokableController('bar');
116+
$className = __NAMESPACE__.'\InvokableController';
117+
118+
$container = $this->createMockContainer();
119+
$container->expects($this->once())
120+
->method('has')
121+
->with($className)
122+
->will($this->returnValue(true))
123+
;
124+
$container->expects($this->once())
125+
->method('get')
126+
->with($className)
127+
->will($this->returnValue($invokableController))
128+
;
129+
130+
$resolver = $this->createControllerResolver(null, null, $container);
131+
$request = Request::create('/');
132+
$request->attributes->set('_controller', $className);
133+
134+
$controller = $resolver->getController($request);
135+
136+
$this->assertEquals($invokableController, $controller);
109137
}
110138

111139
/**
@@ -178,3 +206,14 @@ public function __invoke()
178206
{
179207
}
180208
}
209+
210+
class InvokableController
211+
{
212+
public function __construct($bar) // mandatory argument to prevent automatic instantiation
213+
{
214+
}
215+
216+
public function __invoke()
217+
{
218+
}
219+
}

0 commit comments

Comments
 (0)
0