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

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\DependencyInjection\ContainerAwareInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Templating\EngineInterface;
  15. use Twig\Environment;
  16. /**
  17.  * TemplateController.
  18.  *
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  *
  21.  * @final since version 3.4
  22.  */
  23. class TemplateController implements ContainerAwareInterface
  24. {
  25.     /**
  26.      * @deprecated since version 3.4, to be removed in 4.0
  27.      */
  28.     protected $container;
  29.     private $twig;
  30.     private $templating;
  31.     public function __construct(Environment $twig nullEngineInterface $templating null)
  32.     {
  33.         $this->twig $twig;
  34.         $this->templating $templating;
  35.     }
  36.     /**
  37.      * @deprecated since version 3.4, to be removed in 4.0 alongside with the ContainerAwareInterface type.
  38.      */
  39.     public function setContainer(ContainerInterface $container null)
  40.     {
  41.         @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 3.4 and will be removed in 4.0. Inject a Twig Environment or an EngineInterface using the constructor instead.'__METHOD__), \E_USER_DEPRECATED);
  42.         if ($container->has('templating')) {
  43.             $this->templating $container->get('templating');
  44.         } elseif ($container->has('twig')) {
  45.             $this->twig $container->get('twig');
  46.         }
  47.         $this->container $container;
  48.     }
  49.     /**
  50.      * Renders a template.
  51.      *
  52.      * @param string    $template  The template name
  53.      * @param int|null  $maxAge    Max age for client caching
  54.      * @param int|null  $sharedAge Max age for shared (proxy) caching
  55.      * @param bool|null $private   Whether or not caching should apply for client caches only
  56.      *
  57.      * @return Response A Response instance
  58.      */
  59.     public function templateAction($template$maxAge null$sharedAge null$private null)
  60.     {
  61.         if ($this->templating) {
  62.             $response = new Response($this->templating->render($template));
  63.         } elseif ($this->twig) {
  64.             $response = new Response($this->twig->render($template));
  65.         } else {
  66.             throw new \LogicException('You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.');
  67.         }
  68.         if (null !== $maxAge) {
  69.             $response->setMaxAge($maxAge);
  70.         }
  71.         if (null !== $sharedAge) {
  72.             $response->setSharedMaxAge($sharedAge);
  73.         }
  74.         if ($private) {
  75.             $response->setPrivate();
  76.         } elseif (false === $private || (null === $private && (null !== $maxAge || null !== $sharedAge))) {
  77.             $response->setPublic();
  78.         }
  79.         return $response;
  80.     }
  81. }