Skip to content

Commit

Permalink
Added ability to set controller result in the kernel.view event
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy David authored and fabpot committed Nov 21, 2012
1 parent 94426b9 commit 6ff0dc6
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
Expand Up @@ -51,4 +51,16 @@ public function getControllerResult()
{
return $this->controllerResult;
}

/**
* Assign the return value of the controller
*
* @param array The controller return value
*
* @api
*/
public function setControllerResult(array $controllerResult)
{
$this->controllerResult = $controllerResult;
}
}
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Tests\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;

class ControllerResponseInjectorListener
{
protected $newParameters = array();

public function __construct(array $newParameters)
{
$this->newParameters = $newParameters;
}

public function onKernelView(GetResponseForControllerResultEvent $event)
{
$controllerResult = $event->getControllerResult();

$event->setControllerResult(array_merge($controllerResult, $this->newParameters));
}
}
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Tests\EventListener;

use Symfony\Component\HttpKernel\EventListener\ResponseListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventDispatcher;

class ControllerResponseTest extends \PHPUnit_Framework_TestCase
{
private $dispatcher;

private $kernel;

protected function setUp()
{
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
$this->markTestSkipped('The "EventDispatcher" component is not available');
}

$this->dispatcher = new EventDispatcher();
$listener = new ControllerResponseInjectorListener(array(
'bar' => 'bar',
));
$this->dispatcher->addListener(KernelEvents::VIEW, array($listener, 'onKernelView'));

$this->kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');

}

protected function tearDown()
{
$this->dispatcher = null;
$this->kernel = null;
}

public function testControllerResponseIsChainable()
{

$controllerResponse = array('foo' => 'foo');

$event = new GetResponseForControllerResultEvent($this->kernel, new Request(), HttpKernelInterface::MASTER_REQUEST, $controllerResponse);
$this->dispatcher->dispatch(KernelEvents::VIEW, $event);

$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $event->getControllerResult());
}
}

0 comments on commit 6ff0dc6

Please sign in to comment.