Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
[TemplateI18nBundle] Added TemplateI18nController.
Browse files Browse the repository at this point in the history
For fast rendering of localized static pages.
  • Loading branch information
vudaltsov committed May 2, 2018
1 parent 78d154b commit 3cb96fa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Controller/TemplateI18nController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Ruwork\TemplateI18nBundle\Controller;

use Ruwork\TemplateI18nBundle\Resolver\LocalizedTemplateResolverInterface;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

final class TemplateI18nController
{
private $twig;
private $resolver;

public function __construct(Environment $twig, LocalizedTemplateResolverInterface $resolver)
{
$this->twig = $twig;
$this->resolver = $resolver;
}

public function __invoke(
string $template,
int $maxAge = null,
int $sharedAge = null,
bool $private = null
): Response {
$template = $this->resolver->resolve($template);
$response = new Response($this->twig->render($template));

if ($maxAge) {
$response->setMaxAge($maxAge);
}

if ($sharedAge) {
$response->setSharedMaxAge($sharedAge);
}

if ($private) {
$response->setPrivate();
} elseif (false === $private || (null === $private && ($maxAge || $sharedAge))) {
$response->setPublic();
}

return $response;
}
}
9 changes: 9 additions & 0 deletions Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Ruwork\TemplateI18nBundle\Controller\TemplateI18nController;
use Ruwork\TemplateI18nBundle\EventListener\TemplateAnnotationListener;
use Ruwork\TemplateI18nBundle\NamingStrategy\NamingStrategy;
use Ruwork\TemplateI18nBundle\Resolver\LocalizedTemplateResolver;
Expand Down Expand Up @@ -35,4 +36,12 @@
'$resolver' => ref(LocalizedTemplateResolverInterface::class),
])
->tag('kernel.event_subscriber');

$services->set('ruwork_template_i18n.controller')
->class(TemplateI18nController::class)
->public()
->args([
'$twig' => ref('twig'),
'$resolver' => ref(LocalizedTemplateResolverInterface::class),
]);
};

0 comments on commit 3cb96fa

Please sign in to comment.