8000 [HttpKernel] refactored the HTTP content renderer to make it easier to extend by fabpot · Pull Request #6810 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] refactored the HTTP content renderer to make it easier to extend #6810

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

Merged
merged 1 commit into from
Jan 21, 2013
Merged
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 @@ -13,6 +13,7 @@

use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
use Symfony\Bridge\Twig\Tests\TestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpContentRenderer;

class HttpKernelExtensionTest extends TestCase
Expand All @@ -30,7 +31,7 @@ protected function setUp()

public function testRenderWithoutMasterRequest()
{
$kernel = $this->getHttpContentRenderer($this->returnValue('foo'));
$kernel = $this->getHttpContentRenderer($this->returnValue(new Response('foo')));

$this->assertEquals('foo', $this->renderTemplate($kernel));
}
Expand Down
35 changes: 31 additions & 4 deletions src/Symfony/Component/HttpKernel/HttpContentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\HttpKernel;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
Expand Down Expand Up @@ -78,9 +80,6 @@ public function onKernelResponse(FilterResponseEvent $event)
/**
* Renders a URI and returns the Response content.
*
* When the Response is a StreamedResponse, the content is streamed immediately
* instead of being returned.
*
* Available options:
*
* * ignore_errors: true to return an empty string in case of an error
Expand All @@ -92,6 +91,7 @@ public function onKernelResponse(FilterResponseEvent $event)
* @return string|null The Response content or null when the Response is streamed
*
* @throws \InvalidArgumentException when the strategy does not exist
* @throws \RuntimeException when the Response is not successful
*/
public function render($uri, $strategy = 'default', array $options = array())
{
Expand All @@ -103,7 +103,34 @@ public function render($uri, $strategy = 'default', array $options = array())
throw new \InvalidArgumentException(sprintf('The "%s" rendering strategy does not exist.', $strategy));
}

return $this->strategies[$strategy]->render($uri, $this->requests ? $this->requests[0] : null, $options);
$request = $this->requests ? $this->requests[0] : null;

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

/**
* Delivers the Response as a string.
*
* When the Response is a StreamedResponse, the content is streamed immediately
* instead of being returned.
*
* @param Response $response A Response instance
*
* @return string|null The Response content or null when the Response is streamed
*
* @throws \RuntimeException when the Response is not successful
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public function render should also document this exception.

*/
protected function deliver(Response $response)
{
if (!$response->isSuccessful()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
}

if (!$response instanceof StreamedResponse) {
return $response->getContent();
}

$response->sendContent();
}

public static function getSubscribedEvents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\RenderingStrategy;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Controller\ControllerReference;

Expand Down Expand Up @@ -51,7 +52,7 @@ public function render($uri, Request $request = null, array $options = array())

$level = ob_get_level();
try {
return $this->handle($subRequest);
return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
} catch (\Exception $e) {
// let's clean up the output buffers that were created by the sub-request
while (ob_get_level() > $level) {
Expand All @@ -68,22 +69,9 @@ public function render($uri, Request $request = null, array $options = array())
if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
throw $e;
}
}
}

protected function handle(Request $request)
{
$response = $this->kernel->handle($request, HttpKernelInterface::SUB_REQUEST, false);

if (!$response->isSuccessful()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
return new Response();
}

if (!$response instanceof StreamedResponse) {
return $response->getContent();
}

$response->sendContent();
}

protected function createSubRequest($uri, Request $request = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\RenderingStrategy;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\HttpCache\Esi;

Expand Down Expand Up @@ -69,7 +70,9 @@ public function render($uri, Request $request = null, array $options = array())
$alt = $this->generateProxyUri($alt, $request);
}

return $this->esi->renderIncludeTag($uri, $alt, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : '');
$tag = $this->esi->renderIncludeTag($uri, $alt, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : '');

return new Response($tag);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\RenderingStrategy;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\UriSigner;
Expand Down Expand 10000 Up @@ -69,7 +70,7 @@ public function render($uri, Request $request = null, array $options = array())
$content = $template;
}

return sprintf('<hx:include src="%s">%s</hx:include>', $uri, $content);
return new Response(sprintf('<hx:include src="%s">%s</hx:include>', $uri, $content));
}

private function templateExists($template)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ interface RenderingStrategyInterface
/**
* Renders a URI and returns the Response content.
*
* When the Response is a StreamedResponse, the content is streamed immediately
* instead of being returned.
*
* @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
* @param Request $request A Request instance
* @param array $options An array of options
*
* @return string|null The Response content or null when the Response is streamed
* @return Response A Response instance
*/
public function render($uri, Request $request = null, array $options = array());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests;

use Symfony\Component\HttpKernel\HttpContentRenderer;
use Symfony\Component\HttpFoundation\Response;

class HttpContentRendererTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -43,7 +44,7 @@ public function testRender()
->expects($this->any())
->method('render')
->with('/', null, array('foo' => 'foo', 'ignore_errors' => true))
->will($this->returnValue('foo'))
->will($this->returnValue(new Response('foo')))
;

$renderer = new HttpContentRenderer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public function testRender()
{
$strategy = new DefaultRenderingStrategy($this->getKernel($this->returnValue(new Response('foo'))));

$this->assertEquals('foo', $strategy->render('/'));
$this->assertEquals('foo', $strategy->render('/')->getContent());
}

public function testRenderWithControllerReference()
{
$strategy = new DefaultRenderingStrategy($this->getKernel($this->returnValue(new Response('foo'))));
$strategy->setUrlGenerator($this->getUrlGenerator());

$this->assertEquals('foo', $strategy->render(new ControllerReference('main_controller', array(), array())));
$this->assertEquals('foo', $strategy->render(new ControllerReference('main_controller', array(), array()))->getContent());
}

/**
Expand All @@ -53,14 +53,14 @@ public function testRenderExceptionNoIgnoreErrors()
{
$strategy = new DefaultRenderingStrategy($this->getKernel($this->throwException(new \RuntimeException('foo'))));

$this->assertEquals('foo', $strategy->render('/'));
$this->assertEquals('foo', $strategy->render('/')->getContent());
}

public function testRenderExceptionIgnoreErrors()
{
$strategy = new DefaultRenderingStrategy($this->getKernel($this->throwException(new \RuntimeException('foo'))));

$this->assertNull($strategy->render('/', null, array('ignore_errors' => true)));
$this->assertEmpty($strategy->render('/', null, array('ignore_errors' => true))->getContent());
}

public function testRenderExceptionIgnoreErrorsWithAlt()
Expand All @@ -70,7 +70,7 @@ public function testRenderExceptionIgnoreErrorsWithAlt()
$this->returnValue(new Response('bar'))
)));

$this->assertEquals('bar', $strategy->render('/', null, array('ignore_errors' => true, 'alt' => '/foo')));
$this->assertEquals('bar', $strategy->render('/', null, array('ignore_errors' => true, 'alt' => '/foo'))->getContent());
}

private function getKernel($returnValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up< 107C2 /tool-tip> @@ -49,10 +49,10 @@ public function testRender()
$request = Request::create('/');
$request->headers->set('Surrogate-Capability', 'ESI/1.0');

$this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request));
$this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment')));
$this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo')));
$this->assertEquals('<esi:include src="/main_controller.html" alt="/alt_controller.html" />', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array()))));
$this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent());
$this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent());
$this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo'))->getContent());
$this->assertEquals('<esi:include src="/main_controller.html" alt="/alt_controller.html" />', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent());
}

private function getDefaultStrategy($called = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,30 @@ public function testRenderWithControllerAndSigner()
{
$strategy = new HIncludeRenderingStrategy(null, new UriSigner('foo'));
$strategy->setUrlGenerator($this->getUrlGenerator());
$this->assertEquals('<hx:include src="/main_controller.html?_hash=6MuxpWUHcqIddMMmoN36uPsEjws%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array())));
$this->assertEquals('<hx:include src="/main_controller.html?_hash=6MuxpWUHcqIddMMmoN36uPsEjws%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array()))->getContent());
}

public function testRenderWithUri()
{
$strategy = new HIncludeRenderingStrategy();
$this->assertEquals('<hx:include src="/foo"></hx:include>', $strategy->render('/foo'));
$this->assertEquals('<hx:include src="/foo"></hx:include>', $strategy->render('/foo')->getContent());

$strategy = new HIncludeRenderingStrategy(null, new UriSigner('foo'));
$this->assertEquals('<hx:include src="/foo"></hx:include>', $strategy->render('/foo'));
$this->assertEquals('<hx:include src="/foo"></hx:include>', $strategy->render('/foo')->getContent());
}

public function testRenderWhithDefault()
{
// only default
$strategy = new HIncludeRenderingStrategy();
$this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', null, array('default' => 'default')));
$this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', null, array('default' => 'default'))->getContent());

// only global default
$strategy = new HIncludeRenderingStrategy(null, null, 'global_default');
$this->assertEquals('<hx:include src="/foo">global_default</hx:include>', $strategy->render('/foo', null, array()));
$this->assertEquals('<hx:include src="/foo">global_default</hx:include>', $strategy->render('/foo', null, array())->getContent());

// global default and default
$strategy = new HIncludeRenderingStrategy(null, null, 'global_default');
$this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', null, array('default' => 'default')));
$this->assertEquals('<hx:include src="/foo">default</hx:include>', $strategy->render('/foo', null, array('default' => 'default'))->getContent());
}
}
0