8000 merged branch ltouroumov/2.1 (PR #5885) · rvdbogerd/symfony@63b4486 · GitHub
[go: up one dir, main page]

Skip to content

Commit 63b4486

Browse files
committed
merged branch ltouroumov/2.1 (PR symfony#5885)
This PR was submitted for the 2.1 branch but it was merged into the master branch instead (closes symfony#5885). Commits ------- efc22a8 [HttpKernel] Added ability to set controller result in the kernel.view event Discussion ---------- [HttpKernel] Added ability to set controller result in the kernel.view event Added the method `GetResponseForControllerResultEvent:setControllerResult(array $controllerResult)` to allow kernel.view events to inject data into the result. --------------------------------------------------------------------------- by stof at 2012-11-01T13:48:51Z new features canot be added in the 2.1 branch, which is a maintenance branch and so receives only bugfixes. --------------------------------------------------------------------------- by fabpot at 2012-11-02T07:13:56Z I'm not opposed to the change but can you share your use case with us? --------------------------------------------------------------------------- by ltouroumov at 2012-11-02T07:55:55Z I have custom annotations that allow controller methods to be invoked before t 10000 he actions is run (`@BeforeFilter`) and after the action is run (`@ViewFilter`) and after the response is sent (`@ResponseFilter`). The view filter allows to alter the controller result for example to inject parent entities into the template. If you need more details I suppose I could post the code but I would have to ask the boss first. --------------------------------------------------------------------------- by ltouroumov at 2012-11-02T07:57:53Z Also I fixed the missing dollar sign in the [`6ffea1dc1cfa23844c6d90a1bfa9a282d1807a61`](https://github.com/ltouroumov/symfony/commit/6ffea1dc1cfa23844c6d90a1bfa9a282d1807a61) commit. But I can't seem to attach it to the pull request. --------------------------------------------------------------------------- by henrikbjorn at 2012-11-02T08:12:20Z Needs tests. --------------------------------------------------------------------------- by ltouroumov at 2012-11-02T08:38:03Z Is it really necessary to write a test for a simple setter ? I'm not opposed to testing, just looking for a justification. And should I create a new test case or is there one ? --------------------------------------------------------------------------- by henrikbjorn at 2012-11-02T08:42:49Z Since your code had a clear syntax error i think that a test is in order ;) --------------------------------------------------------------------------- by ltouroumov at 2012-11-02T08:45:03Z Can't argue with that ! Would putting it in `Symfony\Component\HttpKernel\Tests\EventListener\ControllerResponseTest` do the trick ? --------------------------------------------------------------------------- by henrikbjorn at 2012-11-02T09:16:55Z Sounds fair enough. --------------------------------------------------------------------------- by ltouroumov at 2012-11-02T09:31:09Z This tests covers my use case(s), but there might be others I didn't think of. --------------------------------------------------------------------------- by ludekstepan at 2012-11-20T17:15:45Z +1 Please merge into master, I have a use case for it, now I'm relying on a Reflection to set the private property, but that's really a dirty solution. The setter would be perfect!
2 parents 94426b9 + 6ff0dc6 commit 63b4486

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

src/Symfony/Component/HttpKernel/Event/GetResponseForControllerResultEvent.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,16 @@ public function getControllerResult()
5151
{
5252
return $this->controllerResult;
5353
}
54+
55+
/**
56+
* Assign the return value of the controller
57+
*
58+
* @param array The controller return value
59+
*
60+
* @api
61+
*/
62+
public function setControllerResult(array $controllerResult)
63+
{
64+
$this->controllerResult = $controllerResult;
65+
}
5466
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\EventListener;
13+
14+
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
15+
16+
class ControllerResponseInjectorListener
17+
{
18+
protected $newParameters = array();
19+
20+
public function __construct(array $newParameters)
21+
{
22+
$this->newParameters = $newParameters;
23+
}
24+
25+
public function onKernelView(GetResponseForControllerResultEvent $event)
26+
{
27+
$controllerResult = $event->getControllerResult();
28+
29+
$event->setControllerResult(array_merge($controllerResult, $this->newParameters));
30+
}
31+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\EventListener;
13+
14+
use Symfony\Component\HttpKernel\EventListener\ResponseListener;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
18+
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
19+
use Symfony\Component\HttpKernel\KernelEvents;
20+
use Symfony\Component\EventDispatcher\EventDispatcher;
21+
22+
class ControllerResponseTest extends \PHPUnit_Framework_TestCase
23+
{
24+
private $dispatcher;
25+
26+
private $kernel;
27+
28+
protected function setUp()
29+
{
30+
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
31+
$this->markTestSkipped('The "EventDispatcher" component is not available');
32+
}
33+
34+
$this->dispatcher = new EventDispatcher();
35+
$listener = new ControllerResponseInjectorListener(array(
36+
'bar' => 'bar',
37+
));
38+
$this->dispatcher->addListener(KernelEvents::VIEW, array($listener, 'onKernelView'));
39+
40+
$this->kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
41+
42+
}
43+
44+
protected function tearDown()
45+
{
46+
$this->dispatcher = null;
47+
$this->kernel = null;
48+
}
49+
50+
public function testControllerResponseIsChainable()
51+
{
52+
53+
$controllerResponse = array('foo' => 'foo');
54+
55+
$event = new GetResponseForControllerResultEvent($this->kernel, new Request(), HttpKernelInterface::MASTER_REQUEST, $controllerResponse);
56+
$this->dispatcher->dispatch(KernelEvents::VIEW, $event);
57+
58+
$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $event->getControllerResult());
59+
}
60+
}

0 commit comments

Comments
 (0)
0