Skip to content

Fragment handler call set request instead of using listener #8701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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%">
Expand Down
43 changes: 12 additions & 31 deletions src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -30,11 +26,11 @@
*
* @see FragmentRendererInterface
*/
class FragmentHandler implements EventSubscriberInterface
class FragmentHandler
{
private $debug;
private $renderers;
private $requests;
private $request;

/**
* Constructor.
Expand All @@ -49,7 +45,6 @@ public function __construct(array $renderers = array(), $debug = false)
$this->addRenderer($renderer);
}
$this->debug = $debug;
$this->requests = array();
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -108,7 +93,11 @@ 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));
if (null === $this->request) {
throw new \LogicException('Rendering a fragment can only be done when handling a master Request.');
}

return $this->deliver($this->renderers[$renderer]->render($uri, $this->request, $options));
}

/**
Expand All @@ -126,7 +115,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) {
Expand All @@ -136,14 +125,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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
Expand Down