Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 315b627

Browse filesBrowse files
committed
Add TemplateResponse class to get a response from a template reference
1 parent ec36c71 commit 315b627
Copy full SHA for 315b627

File tree

5 files changed

+106
-51
lines changed
Filter options

5 files changed

+106
-51
lines changed

‎src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php
+7-10Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
namespace Symfony\Bundle\FrameworkBundle\EventListener;
1313

1414
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
15-
use Symfony\Bundle\FrameworkBundle\Templating\Template;
15+
use Symfony\Bundle\FrameworkBundle\Templating\TemplateResponseInterface;
1616
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17+
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
1819
use Symfony\Component\HttpKernel\KernelEvents;
1920

2021
/**
21-
* Listener to convert a template reference to a Response.
22-
*
2322
* @author Pierre du Plessis <pdples@gmail.com>
2423
*/
2524
class TemplateListener implements EventSubscriberInterface
@@ -42,18 +41,16 @@ public function onView(GetResponseForControllerResultEvent $event)
4241
{
4342
$result = $event->getControllerResult();
4443

45-
if (!$result instanceof Template) {
44+
if (!$result instanceof TemplateResponseInterface) {
4645
return;
4746
}
4847

49-
$response = $this->templating->renderResponse($result->getTemplate(), $result->getParameters());
48+
$response = $result->getResponse($this->templating);
5049

51-
if ($statusCode = $result->getStatusCode()) {
52-
$response->setStatusCode($statusCode);
53-
}
50+
if (!$response instanceof Response) {
51+
$msg = sprintf('The method %s::getResponse() must return a response (%s given).', get_class($result), is_object($response) ? get_class($response) : gettype($response));
5452

55-
if ($headers = $result->getHeaders()) {
56-
$response->headers->add($headers);
53+
throw new \LogicException($msg);
5754
}
5855

5956
$event->setResponse($response);

‎src/Symfony/Bundle/FrameworkBundle/Templating/Template.php renamed to ‎src/Symfony/Bundle/FrameworkBundle/Templating/TemplateResponse.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Templating/TemplateResponse.php
+11-18Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@
1818
*
1919
* @author Pierre du Plessis <pdples@gmail.com>
2020
*/
21-
class Template
21+
class TemplateResponse implements TemplateResponseInterface
2222
{
2323
private $template;
24-
2524
private $parameters;
26-
2725
private $statusCode;
28-
2926
private $headers;
3027

3128
public function __construct($template, array $parameters = array(), $statusCode = Response::HTTP_OK, array $headers = array())
@@ -36,23 +33,19 @@ public function __construct($template, array $parameters = array(), $statusCode
3633
$this->headers = $headers;
3734
}
3835

39-
public function getTemplate()
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function getResponse(EngineInterface $templating)
4040
{
41-
return $this->template;
42-
}
41+
$response = $templating->renderResponse($this->template, $this->parameters);
4342

44-
public function getParameters()
45-
{
46-
return $this->parameters;
47-
}
43+
if ($this->statusCode) {
44+
$response->setStatusCode($this->statusCode);
45+
}
4846

49-
public function getStatusCode()
50-
{
51-
return $this->statusCode;
52-
}
47+
$response->headers->add($this->headers);
5348

54-
public function getHeaders()
55-
{
56-
return $this->headers;
49+
return $response;
5750
}
5851
}
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Templating;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
16+
/**
17+
* @author Pierre du Plessis <pdples@gmail.com>
18+
*/
19+
interface TemplateResponseInterface
20+
{
21+
/**
22+
* @param EngineInterface $templating
23+
*
24+
* @return Response
25+
*/
26+
public function getResponse(EngineInterface $templating);
27+
}

‎src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php
+12-23Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\EventListener\TemplateListener;
1515
use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine;
16-
use Symfony\Bundle\FrameworkBundle\Templating\Template;
16+
use Symfony\Bundle\FrameworkBundle\Templating\TemplateResponse;
1717
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
18+
use Symfony\Bundle\FrameworkBundle\Templating\TemplateResponseInterface;
1819
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1920
use Symfony\Component\DependencyInjection\Container;
2021
use Symfony\Component\HttpFoundation\Request;
@@ -29,7 +30,7 @@ class TemplateListenerTest extends TestCase
2930
{
3031
public function testTemplateReference()
3132
{
32-
$template = new Template('dummy_template.html.php', array('var' => 'dummy'));
33+
$template = new TemplateResponse('dummy_template.html.php', array('var' => 'dummy'));
3334

3435
$event = $this->getEvent($template);
3536

@@ -42,34 +43,22 @@ public function testTemplateReference()
4243
$this->assertSame(200, $response->getStatusCode());
4344
}
4445

45-
public function testTemplateReferenceWithStatusCode()
46+
public function testInvalidResponse()
4647
{
47-
$template = new Template('dummy_template.html.php', array('var' => 'dummy'), 404);
48+
$templating = $this->getPhpEngine();
4849

49-
$event = $this->getEvent($template);
50-
51-
$listener = new TemplateListener($this->getPhpEngine());
52-
$listener->onView($event);
50+
$template = $this->getMockBuilder(TemplateResponseInterface::class)->getMock();
51+
$template->expects($this->once())
52+
->method('getResponse')
53+
->with($templating)
54+
->will($this->throwException(new \LogicException()));
5355

54-
$response = $event->getResponse();
55-
56-
$this->assertSame('This is dummy content', $response->getContent());
57-
$this->assertSame(404, $response->getStatusCode());
58-
}
59-
60-
public function testTemplateReferenceWithHeaders()
61-
{
62-
$template = new Template('dummy_template.html.php', array('var' => 'dummy'), 200, array('content-type' => 'application/json'));
56+
$this->expectException('LogicException');
6357

6458
$event = $this->getEvent($template);
6559

66-
$listener = new TemplateListener($this->getPhpEngine());
60+
$listener = new TemplateListener($templating);
6761
$listener->onView($event);
68-
69-
$response = $event->getResponse();
70-
71-
$this->assertSame('This is dummy content', $response->getContent());
72-
$this->assertSame(array('cache-control' => array('no-cache, private'), 'content-type' => array('application/json')), $response->headers->all());
7362
}
7463

7564
private function getEvent($template)
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* This file is part of the symfony-config project.
4+
*
5+
* @author pierre
6+
* @copyright Copyright (c) 2017
7+
*/
8+
9+
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
10+
11+
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
12+
use Symfony\Bundle\FrameworkBundle\Templating\TemplateResponse;
13+
use Symfony\Component\HttpFoundation\Response;
14+
15+
class TemplateResponseTest extends \PHPUnit_Framework_TestCase
16+
{
17+
public function testResponse()
18+
{
19+
$templating = $this->getMockBuilder(EngineInterface::class)->getMock();
20+
21+
$templating->expects($this->once())
22+
->method('renderResponse')
23+
->with('dummy_template.html.php', array('var' => 'dummy'))
24+
->will($this->returnValue(new Response()));
25+
26+
$templateResponse = new TemplateResponse('dummy_template.html.php', array('var' => 'dummy'));
27+
28+
$response = $templateResponse->getResponse($templating);
29+
30+
$this->assertInstanceOf(Response::class, $response);
31+
}
32+
33+
public function testResponseStatusCode()
34+
{
35+
$templating = $this->getMockBuilder(EngineInterface::class)->getMock();
36+
37+
$templating->expects($this->once())
38+
->method('renderResponse')
39+
->with('dummy_template.html.php', array('var' => 'dummy'))
40+
->will($this->returnValue(new Response('', 404)));
41+
42+
$templateResponse = new TemplateResponse('dummy_template.html.php', array('var' => 'dummy'), 404);
43+
44+
$response = $templateResponse->getResponse($templating);
45+
46+
$this->assertInstanceOf(Response::class, $response);
47+
$this->assertSame(404, $response->getStatusCode());
48+
}
49+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.