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

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.         if (null !== $templating) {
  28.             @trigger_error(sprintf('Using a "%s" instance for "%s" is deprecated since version 4.4; use a \Twig\Environment instance instead.'EngineInterface::class, __CLASS__), E_USER_DEPRECATED);
  29.         }
  30.         $this->twig $twig;
  31.         $this->templating $templating;
  32.     }
  33.     /**
  34.      * Renders a template.
  35.      *
  36.      * @param string    $template  The template name
  37.      * @param int|null  $maxAge    Max age for client caching
  38.      * @param int|null  $sharedAge Max age for shared (proxy) caching
  39.      * @param bool|null $private   Whether or not caching should apply for client caches only
  40.      */
  41.     public function templateAction(string $templateint $maxAge nullint $sharedAge nullbool $private null): Response
  42.     {
  43.         if ($this->templating) {
  44.             $response = new Response($this->templating->render($template));
  45.         } elseif ($this->twig) {
  46.             $response = new Response($this->twig->render($template));
  47.         } else {
  48.             throw new \LogicException('You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.');
  49.         }
  50.         if ($maxAge) {
  51.             $response->setMaxAge($maxAge);
  52.         }
  53.         if ($sharedAge) {
  54.             $response->setSharedMaxAge($sharedAge);
  55.         }
  56.         if ($private) {
  57.             $response->setPrivate();
  58.         } elseif (false === $private || (null === $private && ($maxAge || $sharedAge))) {
  59.             $response->setPublic();
  60.         }
  61.         return $response;
  62.     }
  63.     public function __invoke(string $templateint $maxAge nullint $sharedAge nullbool $private null): Response
  64.     {
  65.         return $this->templateAction($template$maxAge$sharedAge$private);
  66.     }
  67. }