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 f1d8575

Browse filesBrowse files
committed
bug #21034 [FrameworkBundle] Make TemplateController working without the Templating component (dunglas)
This PR was merged into the 2.8 branch. Discussion ---------- [FrameworkBundle] Make TemplateController working without the Templating component | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a The template controller was missed during #15502. Btw I've added some tests for this controller. Commits ------- 89ccbc4 [FrameworkBundle] Make TemplateController working without the Templating component
2 parents 8a27d79 + 89ccbc4 commit f1d8575
Copy full SHA for f1d8575

File tree

2 files changed

+77
-2
lines changed
Filter options

2 files changed

+77
-2
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ class TemplateController extends ContainerAware
3333
*/
3434
public function templateAction($template, $maxAge = null, $sharedAge = null, $private = null)
3535
{
36-
/** @var $response \Symfony\Component\HttpFoundation\Response */
37-
$response = $this->container->get('templating')->renderResponse($template);
36+
/* @var $response \Symfony\Component\HttpFoundation\Response */
37+
if ($this->container->has('templating')) {
38+
$response = $this->container->get('templating')->renderResponse($template);
39+
} elseif ($this->container->has('twig')) {
40+
$response = new Response($this->container->get('twig')->render($template));
41+
} else {
42+
throw new \LogicException('You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.');
43+
}
3844

3945
if ($maxAge) {
4046
$response->setMaxAge($maxAge);
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Controller;
13+
14+
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
15+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
16+
use Symfony\Component\HttpFoundation\Response;
17+
18+
/**
19+
* @author Kévin Dunglas <dunglas@gmail.com>
20+
*/
21+
class TemplateControllerTest extends TestCase
22+
{
23+
public function testTwig()
24+
{
25+
$twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock();
26+
$twig->expects($this->once())->method('render')->willReturn('bar');
27+
28+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
29+
$container->expects($this->at(0))->method('has')->will($this->returnValue(false));
30+
$container->expects($this->at(1))->method('has')->will($this->returnValue(true));
31+
$container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
32+
33+
$controller = new TemplateController();
34+
$controller->setContainer($container);
35+
36+
$this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent());
37+
}
38+
39+
public function testTemplating()
40+
{
41+
$templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface')->getMock();
42+
$templating->expects($this->once())->method('renderResponse')->willReturn(new Response('bar'));
43+
44+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
45+
$container->expects($this->at(0))->method('has')->willReturn(true);
46+
$container->expects($this->at(1))->method('get')->will($this->returnValue($templating));
47+
48+
$controller = new TemplateController();
49+
$controller->setContainer($container);
50+
51+
$this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent());
52+
}
53+
54+
/**
55+
* @expectedException \LogicException
56+
* @expectedExceptionMessage You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.
57+
*/
58+
public function testNoTwigNorTemplating()
59+
{
60+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
61+
$container->expects($this->at(0))->method('has')->willReturn(false);
62+
$container->expects($this->at(1))->method('has')->willReturn(false);
63+
64+
$controller = new TemplateController();
65+
$controller->setContainer($container);
66+
67+
$controller->templateAction('mytemplate')->getContent();
68+
}
69+
}

0 commit comments

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