8000 merged branch fabpot/content-renderer-refactoring (PR #6810) · symfony/symfony@b7614e9 · GitHub
[go: up one dir, main page]

Skip to content

Commit b7614e9

Browse files
committed
merged branch fabpot/content-renderer-refactoring (PR #6810)
This PR was merged into the master branch. Commits ------- aadefd3 [HttpKernel] refactored the HTTP content renderer to make it easier to extend Discussion ---------- [HttpKernel] refactored the HTTP content renderer to make it easier to extend | Q | A | ------------- | --- | Bug fix? | no | New feature? | kinda | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a This makes the StreamedResponse logic reusable for other strategies and it also makes the RenderingStrategy interface less fuzzy about its contract. That also makes features like #4470 easier to implement from the outside. --------------------------------------------------------------------------- by stof at 2013-01-20T11:01:29Z :+1:
2 parents e769d7f + aadefd3 commit b7614e9

File tree

10 files changed

+60
-42
lines changed
  • RenderingStrategy
  • Tests
  • 10 files changed

    +60
    -42
    lines changed

    src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php

    Lines changed: 2 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -13,6 +13,7 @@
    1313

    1414
    use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
    1515
    use Symfony\Bridge\Twig\Tests\TestCase;
    16+
    use Symfony\Component\HttpFoundation\Response;
    1617
    use Symfony\Component\HttpKernel\HttpContentRenderer;
    1718

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

    3132
    public function testRenderWithoutMasterRequest()
    3233
    {
    33-
    $kernel = $this->getHttpContentRenderer($this->returnValue('foo'));
    34+
    $kernel = $this->getHttpContentRenderer($this->returnValue(new Response('foo')));
    3435

    3536
    $this->assertEquals('foo', $this->renderTemplate($kernel));
    3637
    }

    src/Symfony/Component/HttpKernel/HttpContentRenderer.php

    Lines changed: 31 additions & 4 deletions
    Original file line numberDiff line numberDiff line change
    @@ -12,6 +12,8 @@
    1212
    namespace Symfony\Component\HttpKernel;
    1313

    1414
    use Symfony\Component\HttpFoundation\Request;
    15+
    use Symfony\Component\HttpFoundation\Response;
    16+
    use Symfony\Component\HttpFoundation\StreamedResponse;
    1517
    use Symfony\Component\HttpKernel\Controller\ControllerReference;
    1618
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    1719
    use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
    @@ -78,9 +80,6 @@ public function onKernelResponse(FilterResponseEvent $event)
    7880
    /**
    7981
    * Renders a URI and returns the Response content.
    8082
    *
    81-
    * When the Response is a StreamedResponse, the content is streamed immediately
    82-
    * instead of being returned.
    83-
    *
    8483
    * Available options:
    8584
    *
    8685
    * * ignore_errors: true to return an empty string in case of an error
    @@ -92,6 +91,7 @@ public function onKernelResponse(FilterResponseEvent $event)
    9291
    * @return string|null The Response content or null when the Response is streamed
    9392
    *
    9493
    * @throws \InvalidArgumentException when the strategy does not exist
    94+
    * @throws \RuntimeException when the Response is not successful
    9595
    */
    9696
    public function render($uri, $strategy = 'default', array $options = array())
    9797
    {
    @@ -103,7 +103,34 @@ public function render($uri, $strategy = 'default', array $options = array())
    103103
    throw new \InvalidArgumentException(sprintf('The "%s" rendering strategy does not exist.', $strategy));
    104104
    }
    105105

    106-
    return $this->strategies[$strategy]->render($uri, $this->requests ? $this->requests[0] : null, $options);
    106+
    $request = $this->requests ? $this->requests[0] : null;
    107+
    108+
    return $this->deliver($this->strategies[$strategy]->render($uri, $request, $options));
    109+
    }
    110+
    111+
    /**
    112+
    * Delivers the Response as a string.
    113+
    *
    114+
    * When the Response is a StreamedResponse, the content is streamed immediately
    115+
    * instead of being returned.
    116+
    *
    117+
    * @param Response $response A Response instance
    118+
    *
    119+
    * @return string|null The Response content or null when the Response is streamed
    120+
    *
    121+
    * @throws \RuntimeException when the Response is not successful
    122+
    */
    123+
    protected function deliver(Response $response)
    124+
    {
    125+
    if (!$response->isSuccessful()) {
    126+
    throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
    127+
    }
    128+
    129+
    if (!$response instanceof StreamedResponse) {
    130+
    return $response->getContent();
    131+
    }
    132+
    133+
    $response->sendContent();
    107134
    }
    108135

    109136
    public static function getSubscribedEvents()

    src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php

    Lines changed: 3 additions & 15 deletions
    Original file line numberDiff line numberDiff line change
    @@ -12,6 +12,7 @@
    1212
    namespace Symfony\Component\HttpKernel\RenderingStrategy;
    1313

    1414
    use Symfony\Component\HttpFoundation\Request;
    15+
    use Symfony\Component\HttpFoundation\Response;
    1516
    use Symfony\Component\HttpKernel\HttpKernelInterface;
    1617
    use Symfony\Component\HttpKernel\Controller\ControllerReference;
    1718

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

    5253
    $level = ob_get_level();
    5354
    try {
    54-
    return $this->handle($subRequest);
    55+
    return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
    5556
    } catch (\Exception $e) {
    5657
    // let's clean up the output buffers that were created by the sub-request
    5758
    while (ob_get_level() > $level) {
    @@ -68,22 +69,9 @@ public function render($uri, Request $request = null, array $options = array())
    6869
    if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
    6970
    throw $e;
    7071
    }
    71-
    }
    72-
    }
    73-
    74-
    protected function handle(Request $request)
    75-
    {
    76-
    $response = $this->kernel->handle($request, HttpKernelInterface::SUB_REQUEST, false);
    7772

    78-
    if (!$response->isSuccessful()) {
    79-
    throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
    73+
    return new Response();
    8074
    }
    81-
    82-
    if (!$response instanceof StreamedResponse) {
    83-
    return $response->getContent();
    84-
    }
    85-
    86-
    $response->sendContent();
    8775
    }
    8876

    8977
    protected function createSubRequest($uri, Request $request = null)

    src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php

    Lines changed: 4 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -12,6 +12,7 @@
    1212
    namespace Symfony\Component\HttpKernel\RenderingStrategy;
    1313

    1414
    use Symfony\Component\HttpFoundation\Request;
    15+
    use Symfony\Component\HttpFoundation\Response;
    1516
    use Symfony\Component\HttpKernel\Controller\ControllerReference;
    1617
    use Symfony\Component\HttpKernel\HttpCache\Esi;
    1718

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

    72-
    return $this->esi->renderIncludeTag($uri, $alt, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : '');
    73+
    $tag = $this->esi->renderIncludeTag($uri, $alt, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : '');
    74+
    75+
    return new Response($tag);
    7376
    }
    7477

    7578
    /**

    src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php

    Lines changed: 2 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -12,6 +12,7 @@
    1212
    namespace Symfony\Component\HttpKernel\RenderingStrategy;
    1313

    1414
    use Symfony\Component\HttpFoundation\Request;
    15+
    use Symfony\Component\HttpFoundation\Response;
    1516
    use Symfony\Component\Templating\EngineInterface;
    1617
    use Symfony\Component\HttpKernel\Controller\ControllerReference;
    1718
    use Symfony\Component\HttpKernel\UriSigner;
    @@ -69,7 +70,7 @@ public function render($uri, Request $request = null, array $options = array())
    6970
    $content = $template;
    7071
    }
    7172

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

    7576
    private function templateExists($template)

    src/Symfony/Component/HttpKernel/RenderingStrategy/RenderingStrategyInterface.php

    Lines changed: 1 addition & 4 deletions
    Original file line numberDiff line numberDiff line change
    @@ -26,14 +26,11 @@ interface RenderingStrategyInterface
    2626
    /**
    2727
    * Renders a URI and returns the Response content.
    2828
    *
    29-
    * When the Response is a StreamedResponse, the content is streamed immediately
    30-
    * instead of being returned.
    31-
    *
    3229
    * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
    3330
    * @param Request $request A Request instance
    3431
    * @param array $options An array of options
    3532
    *
    36-
    * @return string|null The Response content or null when the Response is streamed
    33+
    * @return Response A Response instance
    3734
    */
    3835
    public function render($uri, Request $request = null, array $options = array());
    3936

    src/Symfony/Component/HttpKernel/Tests/HttpContentRendererTest.php

    Lines changed: 2 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -12,6 +12,7 @@
    1212
    namespace Symfony\Component\HttpKernel\Tests;
    1313

    1414
    use Symfony\Component\HttpKernel\HttpContentRenderer;
    15+
    use Symfony\Component\HttpFoundation\Response;
    1516

    1617
    class HttpContentRendererTest extends \PHPUnit_Framework_TestCase
    1718
    {
    @@ -43,7 +44,7 @@ public function testRender()
    4344
    ->expects($this->any())
    4445
    ->method('render')
    4546
    ->with('/', null, array('foo' => 'foo', 'ignore_errors' => true))
    46-
    ->will($this->returnValue('foo'))
    47+
    ->will($this->returnValue(new Response('foo')))
    4748
    ;
    4849

    4950
    $renderer = new HttpContentRenderer();

    src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php

    Lines changed: 5 additions & 5 deletions
    < 10000 td data-grid-cell-id="diff-c746b5ef3f9dbfa98a71a4f86e838f35d6ce0ac15c2bc7376c7533db2d42fbe9-46-46-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
    Original file line numberDiff line numberDiff line change
    @@ -35,15 +35,15 @@ public function testRender()
    3535
    {
    3636
    $strategy = new DefaultRenderingStrategy($this->getKernel($this->returnValue(new Response('foo'))));
    3737

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

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

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

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

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

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

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

    6666
    public function testRenderExceptionIgnoreErrorsWithAlt()
    @@ -70,7 +70,7 @@ public function testRenderExceptionIgnoreErrorsWithAlt()
    7070
    $this->returnValue(new Response('bar'))
    7171
    )));
    7272

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

    7676
    private function getKernel($returnValue)

    src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php

    Lines changed: 4 additions & 4 deletions
    Original file line numberDiff line numberDiff line change
    @@ -49,10 +49,10 @@ public function testRender()
    4949
    $request = Request::create('/');
    5050
    $request->headers->set('Surrogate-Capability', 'ESI/1.0');
    5151

    52-
    $this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request));
    53-
    $this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment')));
    54-
    $this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo')));
    55-
    $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()))));
    52+
    $this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent());
    53+
    $this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent());
    54+
    $this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo'))->getContent());
    55+
    $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());
    5656
    }
    5757

    5858
    private function getDefaultStrategy($called = false)

    src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php

    Lines changed: 6 additions & 6 deletions
    Original file line numberDiff line numberDiff line change
    @@ -42,30 +42,30 @@ public function testRenderWithControllerAndSigner()
    4242
    {
    4343
    $strategy = new HIncludeRenderingStrategy(null, new UriSigner('foo'));
    4444
    $strategy->setUrlGenerator($this->getUrlGenerator());
    45-
    $this->assertEquals('<hx:include src="/main_controller.html?_hash=6MuxpWUHcqIddMMmoN36uPsEjws%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array())));
    45+
    $this->assertEquals('<hx:include src="/main_controller.html?_hash=6MuxpWUHcqIddMMmoN36uPsEjws%3D"></hx:include>', $strategy->render(new ControllerReference('main_controller', array(), array()))->getContent());
    4646
    }
    4747

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

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

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

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

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

    0 commit comments

    Comments
     (0)
    0