-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TemplateController for even more effortless templating.
- Loading branch information
Denis Brumann
committed
Feb 4, 2018
1 parent
5320189
commit a467b02
Showing
5 changed files
with
163 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace jæm3l\TemplateBundle\Controller; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Twig\Environment; | ||
|
||
final class TemplateController | ||
{ | ||
private $twig; | ||
|
||
public function __construct(Environment $twig) | ||
{ | ||
$this->twig = $twig; | ||
} | ||
|
||
public function templateAction(string $template, ?int $maxAge, ?int $sharedMaxAge, ?bool $private): Response | ||
{ | ||
$twigTemplate = $this->twig->createTemplate($template); | ||
|
||
$response = new Response($twigTemplate->render([])); | ||
|
||
if ($maxAge) { | ||
$response->setMaxAge($maxAge); | ||
} | ||
if ($sharedMaxAge) { | ||
$response->setSharedMaxAge($sharedMaxAge); | ||
} | ||
if ($private) { | ||
$response->setPrivate(); | ||
} elseif (false === $private || (null === $private && ($maxAge || $sharedMaxAge))) { | ||
$response->setPublic(); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
public function __invoke(string $template, ?int $maxAge, ?int $sharedMaxAge, ?bool $private): Response | ||
{ | ||
return $this->templateAction($template, $maxAge, $sharedMaxAge, $private); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace jæm3l\TemplateBundle\Tests\Controller; | ||
|
||
use jæm3l\TemplateBundle\Controller\TemplateController; | ||
use PHPUnit\Framework\TestCase; | ||
use Twig\Environment; | ||
use Twig\Loader\ArrayLoader; | ||
|
||
class TemplateControllerTest extends TestCase | ||
{ | ||
/** | ||
* @var TemplateController | ||
*/ | ||
private $controller; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$loader = new ArrayLoader([]); | ||
$twig = new Environment($loader); | ||
|
||
$this->controller = new TemplateController($twig); | ||
} | ||
|
||
public function test_it_renders_basic_template() | ||
{ | ||
$template = 'Hello World!'; | ||
|
||
$response = $this->controller->templateAction($template, null, null, null); | ||
|
||
$this->assertSame($template, $response->getContent()); | ||
$this->assertSame('no-cache, private', $response->headers->get('Cache-Control')); | ||
} | ||
|
||
public function test_it_sets_max_age_on_response() | ||
{ | ||
$template = 'Hello World!'; | ||
|
||
$response = $this->controller->templateAction($template, 360, null, null); | ||
|
||
$this->assertContains('max-age=360', $response->headers->get('Cache-Control')); | ||
$this->assertContains('public', $response->headers->get('Cache-Control')); | ||
} | ||
|
||
public function test_it_sets_shared_max_age_on_response() | ||
{ | ||
$template = 'Hello World!'; | ||
|
||
$response = $this->controller->templateAction($template, null, 360, null); | ||
|
||
$this->assertContains('s-maxage=360', $response->headers->get('Cache-Control')); | ||
$this->assertContains('public', $response->headers->get('Cache-Control')); | ||
} | ||
|
||
public function test_it_sets_cache_private_on_response() | ||
{ | ||
$template = 'Hello World!'; | ||
|
||
$response = $this->controller->templateAction($template, null, null, true); | ||
|
||
$this->assertContains('private', $response->headers->get('Cache-Control')); | ||
} | ||
|
||
public function test_it_sets_cache_public_on_response() | ||
{ | ||
$template = 'Hello World!'; | ||
|
||
$response = $this->controller->templateAction($template, null, null, false); | ||
|
||
$this->assertContains('public', $response->headers->get('Cache-Control')); | ||
} | ||
} |