From 3cb96fac202638b480a87eabd2a37580a8052fb6 Mon Sep 17 00:00:00 2001 From: Valentin Date: Wed, 2 May 2018 22:40:40 +0300 Subject: [PATCH] [TemplateI18nBundle] Added TemplateI18nController. For fast rendering of localized static pages. --- Controller/TemplateI18nController.php | 47 +++++++++++++++++++++++++++ Resources/config/services.php | 9 +++++ 2 files changed, 56 insertions(+) create mode 100644 Controller/TemplateI18nController.php diff --git a/Controller/TemplateI18nController.php b/Controller/TemplateI18nController.php new file mode 100644 index 0000000..75d76a7 --- /dev/null +++ b/Controller/TemplateI18nController.php @@ -0,0 +1,47 @@ +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; + } +} diff --git a/Resources/config/services.php b/Resources/config/services.php index d4c25b2..efdbb95 100644 --- a/Resources/config/services.php +++ b/Resources/config/services.php @@ -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; @@ -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), + ]); };