Skip to content

[WIP] #15502 Make template shortcuts be usable without Templating component #15620

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 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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