8000 bug #18289 [FrameworkBundle] Return the invokable service if its name… · symfony/symfony@f9b0aaa · GitHub
[go: up one dir, main page]

Skip to content

Commit f9b0aaa

Browse files
committed
bug #18289 [FrameworkBundle] Return the invokable service if its name is the class name (dunglas)
This PR was squashed before being merged into the 3.1-dev branch (closes #18289). Discussion ---------- [FrameworkBundle] Return the invokable service if its name is the class name | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a if a service is invokable and has the same name than its class name, the controller resolver of FrameworkBundle doesn't retrieve the service and tries to construct a new instance of the class instead. This is a very rare edge case, but this fix is useful for dunglas/DunglasActionBundle#36: referencing auto-registered controllers following the ADR style in YAML and XML routing files will be more intuitive. Currently: `defaults: { _controller: 'Your\Action\FQN:__invoke' }`, after this fix: `defaults: { _controller: 'Your\Action\FQN' }`. This PR also fix a currently useless test. Commits ------- 70b9309 [FrameworkBundle] Return the invokable service if its name is the class name
2 parents 3c75c48 + 70b9309 commit f9b0aaa

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
/**
@@ -182,3 +210,14 @@ public function __invoke()
182210
{
183211
}
184212
}
213+
214+
class InvokableController
215+
{
216+
public function __construct($bar) // mandatory argument to prevent automatic instantiation
217+
{
218+
}
219+
220+
public function __invoke()
221+
{
222+
}
223+
}

0 commit comments

Comments
 (0)
0