Skip to content

Commit

Permalink
[WIP] #15502 Make template shortcuts be usable without Templating com…
Browse files Browse the repository at this point in the history
…ponent
  • Loading branch information
Koc authored and fabpot committed Sep 14, 2015
1 parent f3bfc19 commit d547ec0
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 7 deletions.
46 changes: 39 additions & 7 deletions src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
Expand Up @@ -159,7 +159,15 @@ protected function denyAccessUnlessGranted($attributes, $object = null, $message
*/
public function renderView($view, array $parameters = array())
{
return $this->container->get('templating')->render($view, $parameters);
if ($this->container->has('templating')) {
return $this->container->get('templating')->render($view, $parameters);
}

if (!$this->container->has('twig')) {
throw new \LogicException('You can not use the renderView method if the Templating Component or the Twig Bundle are not available.');
}

return $this->container->get('twig')->render($view, $parameters);
}

/**
Expand All @@ -173,7 +181,21 @@ public function renderView($view, array $parameters = array())
*/
public function render($view, array $parameters = array(), Response $response = null)
{
return $this->container->get('templating')->renderResponse($view, $parameters, $response);
if ($this->container->has('templating')) {
return $this->container->get('templating')->renderResponse($view, $parameters, $response);
}

if (!$this->container->has('twig')) {
throw new \LogicException('You can not use the render method if the Templating Component or the Twig Bundle are not available.');
}

if (null === $response) {
$response = new Response();
}

$response->setContent($this->container->get('twig')->render($view, $parameters));

return $response;
}

/**
Expand All @@ -187,11 +209,21 @@ public function render($view, array $parameters = array(), Response $response =
*/
public function stream($view, array $parameters = array(), StreamedResponse $response = null)
{
$templating = $this->container->get('templating');

$callback = function () use ($templating, $view, $parameters) {
$templating->stream($view, $parameters);
};
if ($this->container->has('templating')) {
$templating = $this->container->get('templating');

$callback = function () use ($templating, $view, $parameters) {
$templating->stream($view, $parameters);
};
} elseif ($this->container->has('twig')) {
$twig = $this->container->get('twig');

$callback = function () use ($twig, $view, $parameters) {
$twig->display($view, $parameters);
};
} else {
throw new \LogicException('You can not use the stream method if the Templating Component or the Twig Bundle are not available.');
}

if (null === $response) {
return new StreamedResponse($callback);
Expand Down
111 changes: 111 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php
Expand Up @@ -96,6 +96,52 @@ public function testGetUserWithEmptyContainer()
$controller->getUser();
}

// public function testRenderWithTwig()
// {
//
// }
//
// public function testRenderWithTemplating()
// {
//
// }

/**
* @expectedException \LogicException
* @expectedExceptionMessage You can not use the render method if the Templating Component or the Twig Bundle are not available.
*/
public function testRenderWithEmptyContainer()
{
$container = $this->getContainerWithoutTwigAndTemplating();
$controller = new TestController();
$controller->setContainer($container);
$controller->render('dummy.html.twig');
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage You can not use the renderView method if the Templating Component or the Twig Bundle are not available.
*/
public function testRenderViewWithEmptyContainer()
{
$container = $this->getContainerWithoutTwigAndTemplating();
$controller = new TestController();
$controller->setContainer($container);
$controller->renderView('dummy.html.twig');
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage You can not use the stream method if the Templating Component or the Twig Bundle are not available.
*/
public function testStreamWithEmptyContainer()
{
$container = $this->getContainerWithoutTwigAndTemplating();
$controller = new TestController();
$controller->setContainer($container);
$controller->stream('dummy.html.twig');
}

/**
* @param $token
*
Expand Down Expand Up @@ -124,6 +170,71 @@ private function getContainerWithTokenStorage($token = null)

return $container;
}

/**
* @return ContainerInterface
*/
private function getContainerWithTwig()
{
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container
->expects($this->once())
->method('has')
->with('twig')
->will($this->returnValue(true));

$twig = $this->getMock('Twig_Environment');
$container
->expects($this->once())
->method('get')
->with('twig')
->will($this->returnValue($twig));

return $container;
}

/**
* @return ContainerInterface
*/
private function getContainerWithTemplating()
{
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container
->expects($this->once())
->method('has')
->with('templating')
->will($this->returnValue(true));

$templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
$container
->expects($this->once())
->method('get')
->with('templating')
->will($this->returnValue($templating));

return $container;
}

/**
* @return ContainerInterface
*/
public function getContainerWithoutTwigAndTemplating()
{
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container
->expects($this->once())
->method('has')
->with('twig')
->will($this->returnValue(false));

$container
->expects($this->once())
->method('has')
->with('templating')
->will($this->returnValue(false));

return $container;
}
}

class TestController extends Controller
Expand Down

0 comments on commit d547ec0

Please sign in to comment.