Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitffb4ff3

Browse files
committed
bug#18394 [FrameworkBundle][2.7] Return the invokable service if its name is the class name (dunglas)
This PR was merged into the 2.7 branch.Discussion----------[FrameworkBundle][2.7] 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/aBackport#18289 to 2.7 as this is a bug fix.Commits-------5c87d76 [FrameworkBundle] Return the invokable service if its name is the class name
2 parentsba1cd26 +5c87d76 commitffb4ff3

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
protectedfunctioninstantiateController($class)
8080
{
81+
if ($this->container->has($class)) {
82+
return$this->container->get($class);
83+
}
84+
8185
$controller =parent::instantiateController($class);
8286

8387
if ($controllerinstanceof 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
publicfunctiontestGetControllerInvokableService()
8989
{
90+
$invokableController =newInvokableController('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+
publicfunctiontestGetControllerInvokableServiceWithClassNameAsName()
114+
{
115+
$invokableController =newInvokableController('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+
publicfunction__construct($bar)// mandatory argument to prevent automatic instantiation
213+
{
214+
}
215+
216+
publicfunction__invoke()
217+
{
218+
}
219+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp