Permalink
Browse files

fixed Request management for FragmentHandler

  • Loading branch information...
fabpot committed Feb 6, 2013
1 parent 1b98ad3 commit ff9d6883bb0ec83af73d9b85b52ef9f6a9806091
@@ -49,16 +49,8 @@ protected function getFragmentHandler($return)
$strategy->expects($this->once())->method('getName')->will($this->returnValue('inline'));
$strategy->expects($this->once())->method('render')->will($return);
// simulate a master request
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock();
$event
->expects($this->once())
->method('getRequest')
->will($this->returnValue(Request::create('/')))
;
$renderer = new FragmentHandler(array($strategy));
$renderer->onKernelRequest($event);
$renderer->setRequest(Request::create('/'));
return $renderer;
}
@@ -14,9 +14,9 @@
<services>
<service id="fragment.handler" class="%fragment.handler.class%">
<tag name="kernel.event_subscriber" />
<argument type="collection" />
<argument>%kernel.debug%</argument>
<call method="setRequest"><argument type="service" id="request" on-invalid="null" strict="false" /></call>
</service>
<service id="fragment.renderer.inline" class="%fragment.renderer.inline.class%">
@@ -15,10 +15,6 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Renders a URI that represents a resource fragment.
@@ -30,11 +26,11 @@
*
* @see FragmentRendererInterface
*/
class FragmentHandler implements EventSubscriberInterface
class FragmentHandler
{
private $debug;
private $renderers;
private $requests;
private $request;
/**
* Constructor.
@@ -49,7 +45,6 @@ public function __construct(array $renderers = array(), $debug = false)
$this->addRenderer($renderer);
}
$this->debug = $debug;
$this->requests = array();
}
/**
@@ -63,23 +58,13 @@ public function addRenderer(FragmentRendererInterface $renderer)
}
/**
* Stores the Request object.
* Sets the current Request.
*
* @param GetResponseEvent $event A GetResponseEvent instance
* @param Request $request The current Request
*/
public function onKernelRequest(GetResponseEvent $event)
public function setRequest(Request $request = null)
{
array_unshift($this->requests, $event->getRequest());
}
/**
* Removes the most recent Request object.
*
* @param FilterResponseEvent $event A FilterResponseEvent instance
*/
public function onKernelResponse(FilterResponseEvent $event)
{
array_shift($this->requests);
$this->request = $request;
}
/**
@@ -108,7 +93,7 @@ public function render($uri, $renderer = 'inline', array $options = array())
throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
}
return $this->deliver($this->renderers[$renderer]->render($uri, $this->requests[0], $options));
return $this->deliver($this->renderers[$renderer]->render($uri, $this->request, $options));
}
/**
@@ -126,7 +111,7 @@ public function render($uri, $renderer = 'inline', array $options = array())
protected function deliver(Response $response)
{
if (!$response->isSuccessful()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->requests[0]->getUri(), $response->getStatusCode()));
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->request->getUri(), $response->getStatusCode()));
}
if (!$response instanceof StreamedResponse) {
@@ -136,14 +121,6 @@ protected function deliver(Response $response)
$response->sendContent();
}
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => 'onKernelRequest',
KernelEvents::RESPONSE => 'onKernelResponse',
);
}
// to be removed in 2.3
public function fixOptions(array $options)
{
@@ -17,13 +17,6 @@
class FragmentHandlerTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
$this->markTestSkipped('The "EventDispatcher" component is not available');
}
}
/**
* @expectedException \InvalidArgumentException
*/
@@ -102,14 +95,7 @@ protected function getHandler($returnValue, $arguments = array())
$handler = new FragmentHandler();
$handler->addRenderer($renderer);
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock();
$event
->expects($this->once())
->method('getRequest')
->will($this->returnValue(Request::create('/')))
;
$handler->onKernelRequest($event);
$handler->setRequest(Request::create('/'));
return $handler;
}

0 comments on commit ff9d688

Please sign in to comment.