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 268bd76

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

File tree

5 files changed

+104
-61
lines changed
Filter options

5 files changed

+104
-61
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\TemplatedResponseInterface;
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 TemplatedResponseInterface) {
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/TemplatedResponse.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Templating/TemplatedResponse.php
+9-28Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,26 @@
1414
use Symfony\Component\HttpFoundation\Response;
1515

1616
/**
17-
* Represents a template reference.
18-
*
1917
* @author Pierre du Plessis <pdples@gmail.com>
2018
*/
21-
class Template
19+
class TemplatedResponse implements TemplatedResponseInterface
2220
{
2321
private $template;
24-
2522
private $parameters;
23+
private $response;
2624

27-
private $statusCode;
28-
29-
private $headers;
30-
31-
public function __construct($template, array $parameters = array(), $statusCode = Response::HTTP_OK, array $headers = array())
25+
public function __construct($template, array $parameters = array(), Response $response = null)
3226
{
3327
$this->template = $template;
3428
$this->parameters = $parameters;
35-
$this->statusCode = $statusCode;
36-
$this->headers = $headers;
37-
}
38-
39-
public function getTemplate()
40-
{
41-
return $this->template;
42-
}
43-
44-
public function getParameters()
45-
{
46-
return $this->parameters;
47-
}
48-
49-
public function getStatusCode()
50-
{
51-
return $this->statusCode;
29+
$this->response = $response ?: new Response();
5230
}
5331

54-
public function getHeaders()
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getResponse(EngineInterface $templating)
5536
{
56-
return $this->headers;
37+
return $templating->renderResponse($this->template, $this->parameters, $this->response);
5738
}
5839
}
+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 TemplatedResponseInterface
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\TemplatedResponse;
1717
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
18+
use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponseInterface;
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 TemplatedResponse('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(TemplatedResponseInterface::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->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('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+
/*
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\Tests\Templating;
13+
14+
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
15+
use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponse;
16+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
17+
use Symfony\Component\HttpFoundation\Response;
18+
19+
class TemplatedResponseTest extends TestCase
20+
{
21+
public function testResponse()
22+
{
23+
$templating = $this->getMockBuilder(EngineInterface::class)->getMock();
24+
25+
$templating->expects($this->once())
26+
->method('renderResponse')
27+
->with('dummy_template.html.php', array('var' => 'dummy'))
28+
->will($this->returnValue(new Response()));
29+
30+
$templateResponse = new TemplatedResponse('dummy_template.html.php', array('var' => 'dummy'));
31+
32+
$this->assertInstanceOf(Response::class, $templateResponse->getResponse($templating));
33+
}
34+
35+
public function testSameResponse()
36+
{
37+
$templating = $this->getMockBuilder(EngineInterface::class)->getMock();
38+
39+
$response = new Response();
40+
$templating->expects($this->once())
41+
->method('renderResponse')
42+
->with('dummy_template.html.php', array('var' => 'dummy'))
43+
->will($this->returnValue($response));
44+
45+
$templateResponse = new TemplatedResponse('dummy_template.html.php', array('var' => 'dummy'), $response);
46+
47+
$this->assertSame($response, $templateResponse->getResponse($templating));
48+
}
49+
}

0 commit comments

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