8000 [FrameworkBundle] Add new TemplateResponse class by pierredup · Pull Request #21765 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Add new TemplateResponse class #21765

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
wants to merge 9 commits into from
Prev Previous commit
Next Next commit
Add TemplateResponse class to get a response from a template reference
  • Loading branch information
pierredup committed Feb 27, 2017
commit f9e2e6ebe4813670e891c57b4532069fffd510f7
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
namespace Symfony\Bundle\FrameworkBundle\EventListener;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Bundle\FrameworkBundle\Templating\Template;
use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponseInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\KernelEvents;

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

if (!$result instanceof Template) {
if (!$result instanceof TemplatedResponseInterface) {
return;
}

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

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

if ($headers = $result->getHeaders()) {
$response->headers->add($headers);
throw new \LogicException($msg);
}

$event->setResponse($response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

<service id="templating.loader" alias="templating.loader.filesystem" />

<service id="template_listener" class="Symfony\Bundle\FrameworkBundle\EventListener\TemplateListener">
<service id="templating.listener" class="Symfony\Bundle\FrameworkBundle\EventListener\TemplateListener">
<tag name="kernel.event_subscriber" />
<argument type="service" id="templating" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,26 @@
use Symfony\Component\HttpFoundation\Response;

/**
* Represents a template reference.
*
* @author Pierre du Plessis <pdples@gmail.com>
*/
class Template
class TemplatedResponse implements TemplatedResponseInterface
{
private $template;

private $parameters;
private $response;

private $statusCode;

private $headers;

public function __construct($template, array $parameters = array(), $statusCode = Response::HTTP_OK, array $headers = array())
public function __construct($template, array $parameters = array(), Response $response = null)
{
$this->template = $template;
$this->parameters = $parameters;
$this->statusCode = $statusCode;
$this->headers = $headers;
}

public function getTemplate()
{
return $this->template;
}

public function getParameters()
{
return $this->parameters;
}

public function getStatusCode()
{
return $this->statusCode;
$this->response = $response ?: new Response();
}

public function getHeaders()
/**
* {@inheritdoc}
*/
public function getResponse(EngineInterface $templating)
{
return $this->headers;
return $templating->renderResponse($this->template, $this->parameters, $this->response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Templating;

use Symfony\Component\HttpFoundation\Response;

/**
* @author Pierre du Plessis <pdples@gmail.com>
*/
interface TemplatedResponseInterface
{
/**
* @param EngineInterface $templating
*
* @return Response
*/
public function getResponse(EngineInterface $templating);
}
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('LogicException');
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

use Symfony\Bundle\FrameworkBundle\EventListener\TemplateListener;
use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine;
use Symfony\Bundle\FrameworkBundle\Templating\Template;
use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponse;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponseInterface;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -29,7 +30,7 @@ class TemplateListenerTest extends TestCase
{
public function testTemplateReference()
{
$template = new Template('dummy_template.html.php', array('var' => 'dummy'));
$template = new TemplatedResponse('dummy_template.html.php', array('var' => 'dummy'));

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

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

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

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

$listener = new TemplateListener($this->getPhpEngine());
$listener->onView($event);
$template = $this->getMockBuilder(TemplatedResponseInterface::class)->getMock();
$template->expects($this->once())
->method('getResponse')
->with($templating)
->will($this->throwException(new \LogicException()));

$response = $event->getResponse();

$this->assertSame('This is dummy content', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}

public function testTemplateReferenceWithHeaders()
{
$template = new Template('dummy_template.html.php', array('var' => 'dummy'), 200, array('content-type' => 'application/json'));

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

$listener = new TemplateListener($this->getPhpEngine());
$listener = new TemplateListener($templating);
$listener->onView($event);

$response = $event->getResponse();

$this->assertSame('This is dummy content', $response->getContent());
$this->assertSame(array('cache-control' => array('no-cache, private'), 'content-type' => array('application/json')), $response->headers->all());
}

private function getEvent($template)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Bundle\FrameworkBundle\Templating\TemplatedResponse;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\HttpFoundation\Response;

class TemplatedResponseTest extends TestCase
{
public function testResponse()
{
$templating = $this->getMockBuilder(EngineInterface::class)->getMock();

$templating->expects($this->once())
->method('renderResponse')
->with('dummy_template.html.php', array('var' => 'dummy'))
->will($this->returnValue(new Response()));

$templateResponse = new TemplatedResponse('dummy_template.html.php', array('var' => 'dummy'));

$this->assertInstanceOf(Response::class, $templateResponse->getResponse($templating));
}

public function testSameResponse()
{
$templating = $this->getMockBuilder(EngineInterface::class)->getMock();

$response = new Response();
$templating->expects($this->once())
->method('renderResponse')
->with('dummy_template.html.php', array('var' => 'dummy'))
->will($this->returnValue($response));

$templateResponse = new TemplatedResponse('dummy_template.html.php', array('var' => 'dummy'), $response);

$this->assertSame($response, $templateResponse->getResponse($templating));
}
}
0