Skip to content

Commit 721f47e

Browse files
committed
Merge pull request #927 from Engerim/controller_invoke
Add support for invokable controller
2 parents 6ec796c + 6ab9ef7 commit 721f47e

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

EventListener/ParamFetcherListener.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ public function onKernelController(FilterControllerEvent $event)
5151
$request = $event->getRequest();
5252
$paramFetcher = $this->container->get('fos_rest.request.param_fetcher');
5353

54-
$paramFetcher->setController($event->getController());
55-
$attributeName = $this->getAttributeName($event->getController());
54+
$controller = $event->getController();
55+
56+
if (is_callable($controller) && method_exists($controller, '__invoke')) {
57+
$controller = array($controller, '__invoke');
58+
}
59+
60+
$paramFetcher->setController($controller);
61+
$attributeName = $this->getAttributeName($controller);
5662
$request->attributes->set($attributeName, $paramFetcher);
5763

5864
if ($this->setParamsAsAttributes) {

Tests/EventListener/ParamFetcherListenerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ public function testSettingParamFetcherByTypehint($actionName, $expectedAttribut
9797
$this->assertSame($this->paramFetcher, $request->attributes->get($expectedAttribute));
9898
}
9999

100+
/**
101+
* Tests that the ParamFetcher can be injected in a invokable controller.
102+
*/
103+
public function testSettingParamFetcherForInvokable()
104+
{
105+
$request = new Request();
106+
107+
$event = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Event\\FilterControllerEvent')
108+
->disableOriginalConstructor()
109+
->getMock();
110+
111+
$event->expects($this->atLeastOnce())
112+
->method('getRequest')
113+
->will($this->returnValue($request));
114+
115+
$controller = new ParamFetcherController();
116+
117+
$event->expects($this->atLeastOnce())
118+
->method('getController')
119+
->will($this->returnValue($controller));
120+
121+
$this->paramFetcher->expects($this->once())
122+
->method('all')
123+
->will($this->returnValue(array()));
124+
125+
$this->paramFetcherListener->onKernelController($event);
126+
127+
$this->assertSame($this->paramFetcher, $request->attributes->get('pfInvokable'));
128+
}
129+
100130
public function setParamFetcherByTypehintProvider()
101131
{
102132
return array(

Tests/Fixtures/Controller/ParamFetcherController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ public function byTypeAction(ParamFetcher $pf)
2828
*/
2929
public function notProvidedAction()
3030
{}
31+
32+
/**
33+
* Make sure the ParamFetcher can be set for controller which are used as invokable
34+
*/
35+
public function __invoke(ParamFetcher $pfInvokable)
36+
{}
3137
}

0 commit comments

Comments
 (0)