vendor/symfony/framework-bundle/Controller/TemplateController.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\FrameworkBundle\Controller;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Templating\EngineInterface;
  13. use Twig\Environment;
  14. /**
  15.  * TemplateController.
  16.  *
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  *
  19.  * @final
  20.  */
  21. class TemplateController
  22. {
  23.     private $twig;
  24.     private $templating;
  25.     public function __construct(Environment $twig nullEngineInterface $templating null)
  26.     {
  27.         $this->twig $twig;
  28.         $this->templating $templating;
  29.     }
  30.     /**
  31.      * Renders a template.
  32.      *
  33.      * @param string    $template  The template name
  34.      * @param int|null  $maxAge    Max age for client caching
  35.      * @param int|null  $sharedAge Max age for shared (proxy) caching
  36.      * @param bool|null $private   Whether or not caching should apply for client caches only
  37.      */
  38.     public function templateAction(string $templateint $maxAge nullint $sharedAge nullbool $private null): Response
  39.     {
  40.         if ($this->templating) {
  41.             $response = new Response($this->templating->render($template));
  42.         } elseif ($this->twig) {
  43.             $response = new Response($this->twig->render($template));
  44.         } else {
  45.             throw new \LogicException('You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.');
  46.         }
  47.         if ($maxAge) {
  48.             $response->setMaxAge($maxAge);
  49.         }
  50.         if ($sharedAge) {
  51.             $response->setSharedMaxAge($sharedAge);
  52.         }
  53.         if ($private) {
  54.             $response->setPrivate();
  55.         } elseif (false === $private || (null === $private && ($maxAge || $sharedAge))) {
  56.             $response->setPublic();
  57.         }
  58.         return $response;
  59.     }
  60.     public function __invoke(string $templateint $maxAge nullint $sharedAge nullbool $private null): Response
  61.     {
  62.         return $this->templateAction($template$maxAge$sharedAge$private);
  63.     }
  64. }