10000 [WIP] #15502 Make template shortcuts be usable without Templating com… · symfony/symfony@d547ec0 · GitHub
[go: up one dir, main page]

Skip to content

Commit d547ec0

Browse files
Kocfabpot
authored andcommitted
[WIP] #15502 Make template shortcuts be usable without Templating component
1 parent f3bfc19 commit d547ec0

File tree

2 files changed

+150
-7
lines changed

2 files changed

+150
-7
lines changed

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

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,15 @@ protected function denyAccessUnlessGranted($attributes, $object = null, $message
159159
*/
160160
public function renderView($view, array $parameters = array())
161161
{
162-
return $this->container->get('templating')->render($view, $parameters);
162+
if ($this->container->has('templating')) {
163+
return $this->container->get('templating')->render($view, $parameters);
164+
}
165+
166+
if (!$this->container->has('twig')) {
167+
throw new \LogicException('You can not use the renderView method if the Templating Component or the Twig Bundle are not available.');
168+
}
169+
170+
return $this->container->get('twig')->render($view, $parameters);
163171
}
164172

165173
/**
@@ -173,7 +181,21 @@ public function renderView($view, array $parameters = array())
173181
*/
174182
public function render($view, array $parameters = array(), Response $response = null)
175183
{
176-
return $this->container->get('templating')->renderResponse($view, $parameters, $response);
184+
if ($this->container->has('templating')) {
185+
return $this->container->get('templating')->renderResponse($view, $parameters, $response);
186+
}
187+
188+
if (!$this->container->has('twig')) {
189+
throw new \LogicException('You can not use the render method if the Templating Component or the Twig Bundle are not available.');
190+
}
191+
192+
if (null === $response) {
193+
$response = new Response();
194+
}
195+
196+
$response->setContent($this->container->get('twig')->render($view, $parameters));
197+
198+
return $response;
177199
}
178200

179201
/**
@@ -187,11 +209,21 @@ public function render($view, array $parameters = array(), Response $response =
187209
*/
188210
public function stream($view, array $parameters = array(), StreamedResponse $response = null)
189211
{
190-
$templating = $this->container->get('templating');
191-
192-
$callback = function () use ($templating, $view, $parameters) {
193-
$templating->stream($view, $parameters);
194-
};
212+
if ($this->container->has('templating')) {
213+
$templating = $this->container->get('templating');
214+
215+
$callback = function () use ($templating, $view, $parameters) {
216+
$templating->stream($view, $parameters);
217+
};
218+
} elseif ($this->container->has('twig')) {
219+
$twig = $this->container->get('twig');
220+
221+
$callback = function () use ($twig, $view, $parameters) {
222+
$twig->display($view, $parameters);
223+
};
224+
} else {
225+
throw new \LogicException('You can not use the stream method if the Templating Component or the Twig Bundle are not available.');
226+
}
195227

196228
if (null === $response) {
197229
return new StreamedResponse($callback);

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,52 @@ public function testGetUserWithEmptyContainer()
9696
$controller->getUser();
9797
}
9898

99+
// public function testRenderWithTwig()
100+
// {
101+
//
102+
// }
103+
//
104+
// public function testRenderWithTemplating()
105+
// {
106+
//
107+
// }
108+
109+
/**
110+
* @expectedException \LogicException
111+
* @expectedExceptionMessage You can not use the render method if the Templating Component or the Twig Bundle are not available.
112+
*/
113+
public function testRenderWithEmptyContainer()
114+
{
115+
$container = $this->getContainerWithoutTwigAndTemplating();
116+
$controller = new TestController();
117+
$controller->setContainer($container);
118+
$controller->render('dummy.html.twig');
119+
}
120+
121+
/**
122+
* @expectedException \LogicException
123+
* @expectedExceptionMessage You can not use the renderView method if the Templating Component or the Twig Bundle are not available.
12 B41A 4+
*/
125+
public function testRenderViewWithEmptyContainer()
126+
{
127+
$container = $this->getContainerWithoutTwigAndTemplating();
128+
$controller = new TestController();
129+
$controller->setContainer($container);
130+
$controller->renderView('dummy.html.twig');
131+
}
132+
133+
/**
134+
* @expectedException \LogicException
135+
* @expectedExceptionMessage You can not use the stream method if the Templating Component or the Twig Bundle are not available.
136+
*/
137+
public function testStreamWithEmptyContainer()
138+
{
139+
$container = $this->getContainerWithoutTwigAndTemplating();
140+
$controller = new TestController();
141+
$controller->setContainer($container);
142+
$controller->stream('dummy.html.twig');
143+
}
144+
99145
/**
100146
* @param $token
101147
*
@@ -124,6 +170,71 @@ private function getContainerWithTokenStorage($token = null)
124170

125171
return $container;
126172
}
173+
174+
/**
175+
* @return ContainerInterface
176+
*/
177+
private function getContainerWithTwig()
178+
{
179+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
180+
$container
181+
->expects($this->once())
182+
->method('has')
183+
->with('twig')
184+
->will($this->returnValue(true));
185+
186+
$twig = $this->getMock('Twig_Environment');
187+
$container
188+
->expects($this->once())
189+
->method('get')
190+
->with('twig')
191+
->will($this->returnValue($twig));
192+
193+
return $container;
194+
}
195+
196+
/**
197+
* @return ContainerInterface
198+
*/
199+
private function getContainerWithTemplating()
200+
{
201+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
202+
$container
203+
->expects($this->once())
204+
->method('has')
205+
->with('templating')
206+
->will($this->returnValue(true));
207+
208+
$templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
209+
$container
210+
->expects($this->once())
211+
->method('get')
212+
->with('templating')
213+
->will($this->returnValue($templating));
214+
215+
return $container;
216+
}
217+
218+
/**
219+
* @return ContainerInterface
220+
*/
221+
public function getContainerWithoutTwigAndTemplating()
222+
{
223+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
224+
$container
225+
->expects($this->once())
226+
->method('has')
227+
->with('twig')
228+
->will($this->returnValue(false));
229+
230+
$container
231+
->expects($this->once())
232+
->method('has')
233+
->with('templating')
234+
->will($this->returnValue(false));
235+
236+
return $container;
237+
}
127238
}
128239

129240
class TestController extends Controller

0 commit comments

Comments
 (0)
0