vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/TwigEngine.php line 14

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\TwigBundle;
  11. @trigger_error('The '.TwigEngine::class.' class is deprecated since version 4.3 and will be removed in 5.0; use \Twig\Environment instead.'E_USER_DEPRECATED);
  12. use Symfony\Bridge\Twig\TwigEngine as BaseEngine;
  13. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  14. use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
  15. use Symfony\Component\Config\FileLocatorInterface;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Templating\TemplateNameParserInterface;
  18. use Twig\Environment;
  19. use Twig\Error\Error;
  20. /**
  21.  * This engine renders Twig templates.
  22.  *
  23.  * @author Fabien Potencier <fabien@symfony.com>
  24.  *
  25.  * @deprecated since version 4.3, to be removed in 5.0; use Twig instead.
  26.  */
  27. class TwigEngine extends BaseEngine implements EngineInterface
  28. {
  29.     protected $locator;
  30.     public function __construct(Environment $environmentTemplateNameParserInterface $parserFileLocatorInterface $locator)
  31.     {
  32.         parent::__construct($environment$parser);
  33.         $this->locator $locator;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public function render($name, array $parameters = [])
  39.     {
  40.         try {
  41.             return parent::render($name$parameters);
  42.         } catch (Error $e) {
  43.             if ($name instanceof TemplateReference && !method_exists($e'setSourceContext')) {
  44.                 try {
  45.                     // try to get the real name of the template where the error occurred
  46.                     $name $e->getTemplateName();
  47.                     $path = (string) $this->locator->locate($this->parser->parse($name));
  48.                     $e->setTemplateName($path);
  49.                 } catch (\Exception $e2) {
  50.                 }
  51.             }
  52.             throw $e;
  53.         }
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      *
  58.      * @throws Error if something went wrong like a thrown exception while rendering the template
  59.      */
  60.     public function renderResponse($view, array $parameters = [], Response $response null)
  61.     {
  62.         if (null === $response) {
  63.             $response = new Response();
  64.         }
  65.         $response->setContent($this->render($view$parameters));
  66.         return $response;
  67.     }
  68. }