vendor/twig/twig/src/Template.php line359

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  6.  * (c) Armin Ronacher
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Twig;
  12. use Twig\Error\Error;
  13. use Twig\Error\LoaderError;
  14. use Twig\Error\RuntimeError;
  15. /**
  16.  * Default base class for compiled templates.
  17.  *
  18.  * This class is an implementation detail of how template compilation currently
  19.  * works, which might change. It should never be used directly. Use $twig->load()
  20.  * instead, which returns an instance of \Twig\TemplateWrapper.
  21.  *
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  *
  24.  * @internal
  25.  */
  26. abstract class Template
  27. {
  28.     const ANY_CALL 'any';
  29.     const ARRAY_CALL 'array';
  30.     const METHOD_CALL 'method';
  31.     protected $parent;
  32.     protected $parents = [];
  33.     protected $env;
  34.     protected $blocks = [];
  35.     protected $traits = [];
  36.     protected $extensions = [];
  37.     protected $sandbox;
  38.     public function __construct(Environment $env)
  39.     {
  40.         $this->env $env;
  41.         $this->extensions $env->getExtensions();
  42.     }
  43.     /**
  44.      * @internal this method will be removed in 3.0 and is only used internally to provide an upgrade path from 1.x to 2.0
  45.      */
  46.     public function __toString()
  47.     {
  48.         return $this->getTemplateName();
  49.     }
  50.     /**
  51.      * Returns the template name.
  52.      *
  53.      * @return string The template name
  54.      */
  55.     abstract public function getTemplateName();
  56.     /**
  57.      * Returns debug information about the template.
  58.      *
  59.      * @return array Debug information
  60.      */
  61.     abstract public function getDebugInfo();
  62.     /**
  63.      * Returns information about the original template source code.
  64.      *
  65.      * @return Source
  66.      */
  67.     public function getSourceContext()
  68.     {
  69.         return new Source(''$this->getTemplateName());
  70.     }
  71.     /**
  72.      * Returns the parent template.
  73.      *
  74.      * This method is for internal use only and should never be called
  75.      * directly.
  76.      *
  77.      * @param array $context
  78.      *
  79.      * @return Template|TemplateWrapper|false The parent template or false if there is no parent
  80.      */
  81.     public function getParent(array $context)
  82.     {
  83.         if (null !== $this->parent) {
  84.             return $this->parent;
  85.         }
  86.         try {
  87.             $parent $this->doGetParent($context);
  88.             if (false === $parent) {
  89.                 return false;
  90.             }
  91.             if ($parent instanceof self || $parent instanceof TemplateWrapper) {
  92.                 return $this->parents[$parent->getSourceContext()->getName()] = $parent;
  93.             }
  94.             if (!isset($this->parents[$parent])) {
  95.                 $this->parents[$parent] = $this->loadTemplate($parent);
  96.             }
  97.         } catch (LoaderError $e) {
  98.             $e->setSourceContext(null);
  99.             $e->guess();
  100.             throw $e;
  101.         }
  102.         return $this->parents[$parent];
  103.     }
  104.     protected function doGetParent(array $context)
  105.     {
  106.         return false;
  107.     }
  108.     public function isTraitable()
  109.     {
  110.         return true;
  111.     }
  112.     /**
  113.      * Displays a parent block.
  114.      *
  115.      * This method is for internal use only and should never be called
  116.      * directly.
  117.      *
  118.      * @param string $name    The block name to display from the parent
  119.      * @param array  $context The context
  120.      * @param array  $blocks  The current set of blocks
  121.      */
  122.     public function displayParentBlock($name, array $context, array $blocks = [])
  123.     {
  124.         if (isset($this->traits[$name])) {
  125.             $this->traits[$name][0]->displayBlock($name$context$blocksfalse);
  126.         } elseif (false !== $parent $this->getParent($context)) {
  127.             $parent->displayBlock($name$context$blocksfalse);
  128.         } else {
  129.             throw new RuntimeError(sprintf('The template has no parent and no traits defining the "%s" block.'$name), -1$this->getSourceContext());
  130.         }
  131.     }
  132.     /**
  133.      * Displays a block.
  134.      *
  135.      * This method is for internal use only and should never be called
  136.      * directly.
  137.      *
  138.      * @param string $name      The block name to display
  139.      * @param array  $context   The context
  140.      * @param array  $blocks    The current set of blocks
  141.      * @param bool   $useBlocks Whether to use the current set of blocks
  142.      */
  143.     public function displayBlock($name, array $context, array $blocks = [], $useBlocks trueself $templateContext null)
  144.     {
  145.         if ($useBlocks && isset($blocks[$name])) {
  146.             $template $blocks[$name][0];
  147.             $block $blocks[$name][1];
  148.         } elseif (isset($this->blocks[$name])) {
  149.             $template $this->blocks[$name][0];
  150.             $block $this->blocks[$name][1];
  151.         } else {
  152.             $template null;
  153.             $block null;
  154.         }
  155.         // avoid RCEs when sandbox is enabled
  156.         if (null !== $template && !$template instanceof self) {
  157.             throw new \LogicException('A block must be a method on a \Twig\Template instance.');
  158.         }
  159.         if (null !== $template) {
  160.             try {
  161.                 $template->$block($context$blocks);
  162.             } catch (Error $e) {
  163.                 if (!$e->getSourceContext()) {
  164.                     $e->setSourceContext($template->getSourceContext());
  165.                 }
  166.                 // this is mostly useful for \Twig\Error\LoaderError exceptions
  167.                 // see \Twig\Error\LoaderError
  168.                 if (-=== $e->getTemplateLine()) {
  169.                     $e->guess();
  170.                 }
  171.                 throw $e;
  172.             } catch (\Exception $e) {
  173.                 $e = new RuntimeError(sprintf('An exception has been thrown during the rendering of a template ("%s").'$e->getMessage()), -1$template->getSourceContext(), $e);
  174.                 $e->guess();
  175.                 throw $e;
  176.             }
  177.         } elseif (false !== $parent $this->getParent($context)) {
  178.             $parent->displayBlock($name$contextarray_merge($this->blocks$blocks), false$templateContext ?? $this);
  179.         } elseif (isset($blocks[$name])) {
  180.             throw new RuntimeError(sprintf('Block "%s" should not call parent() in "%s" as the block does not exist in the parent template "%s".'$name$blocks[$name][0]->getTemplateName(), $this->getTemplateName()), -1$blocks[$name][0]->getSourceContext());
  181.         } else {
  182.             throw new RuntimeError(sprintf('Block "%s" on template "%s" does not exist.'$name$this->getTemplateName()), -1, ($templateContext ?? $this)->getSourceContext());
  183.         }
  184.     }
  185.     /**
  186.      * Renders a parent block.
  187.      *
  188.      * This method is for internal use only and should never be called
  189.      * directly.
  190.      *
  191.      * @param string $name    The block name to render from the parent
  192.      * @param array  $context The context
  193.      * @param array  $blocks  The current set of blocks
  194.      *
  195.      * @return string The rendered block
  196.      */
  197.     public function renderParentBlock($name, array $context, array $blocks = [])
  198.     {
  199.         ob_start();
  200.         $this->displayParentBlock($name$context$blocks);
  201.         return ob_get_clean();
  202.     }
  203.     /**
  204.      * Renders a block.
  205.      *
  206.      * This method is for internal use only and should never be called
  207.      * directly.
  208.      *
  209.      * @param string $name      The block name to render
  210.      * @param array  $context   The context
  211.      * @param array  $blocks    The current set of blocks
  212.      * @param bool   $useBlocks Whether to use the current set of blocks
  213.      *
  214.      * @return string The rendered block
  215.      */
  216.     public function renderBlock($name, array $context, array $blocks = [], $useBlocks true)
  217.     {
  218.         ob_start();
  219.         $this->displayBlock($name$context$blocks$useBlocks);
  220.         return ob_get_clean();
  221.     }
  222.     /**
  223.      * Returns whether a block exists or not in the current context of the template.
  224.      *
  225.      * This method checks blocks defined in the current template
  226.      * or defined in "used" traits or defined in parent templates.
  227.      *
  228.      * @param string $name    The block name
  229.      * @param array  $context The context
  230.      * @param array  $blocks  The current set of blocks
  231.      *
  232.      * @return bool true if the block exists, false otherwise
  233.      */
  234.     public function hasBlock($name, array $context, array $blocks = [])
  235.     {
  236.         if (isset($blocks[$name])) {
  237.             return $blocks[$name][0] instanceof self;
  238.         }
  239.         if (isset($this->blocks[$name])) {
  240.             return true;
  241.         }
  242.         if (false !== $parent $this->getParent($context)) {
  243.             return $parent->hasBlock($name$context);
  244.         }
  245.         return false;
  246.     }
  247.     /**
  248.      * Returns all block names in the current context of the template.
  249.      *
  250.      * This method checks blocks defined in the current template
  251.      * or defined in "used" traits or defined in parent templates.
  252.      *
  253.      * @param array $context The context
  254.      * @param array $blocks  The current set of blocks
  255.      *
  256.      * @return array An array of block names
  257.      */
  258.     public function getBlockNames(array $context, array $blocks = [])
  259.     {
  260.         $names array_merge(array_keys($blocks), array_keys($this->blocks));
  261.         if (false !== $parent $this->getParent($context)) {
  262.             $names array_merge($names$parent->getBlockNames($context));
  263.         }
  264.         return array_unique($names);
  265.     }
  266.     protected function loadTemplate($template$templateName null$line null$index null)
  267.     {
  268.         try {
  269.             if (\is_array($template)) {
  270.                 return $this->env->resolveTemplate($template);
  271.             }
  272.             if ($template instanceof self || $template instanceof TemplateWrapper) {
  273.                 return $template;
  274.             }
  275.             if ($template === $this->getTemplateName()) {
  276.                 $class = \get_class($this);
  277.                 if (false !== $pos strrpos($class'___', -1)) {
  278.                     $class substr($class0$pos);
  279.                 }
  280.                 return $this->env->loadClass($class$template$index);
  281.             }
  282.             return $this->env->loadTemplate($template$index);
  283.         } catch (Error $e) {
  284.             if (!$e->getSourceContext()) {
  285.                 $e->setSourceContext($templateName ? new Source(''$templateName) : $this->getSourceContext());
  286.             }
  287.             if ($e->getTemplateLine() > 0) {
  288.                 throw $e;
  289.             }
  290.             if (!$line) {
  291.                 $e->guess();
  292.             } else {
  293.                 $e->setTemplateLine($line);
  294.             }
  295.             throw $e;
  296.         }
  297.     }
  298.     /**
  299.      * Returns all blocks.
  300.      *
  301.      * This method is for internal use only and should never be called
  302.      * directly.
  303.      *
  304.      * @return array An array of blocks
  305.      */
  306.     public function getBlocks()
  307.     {
  308.         return $this->blocks;
  309.     }
  310.     public function display(array $context, array $blocks = [])
  311.     {
  312.         $this->displayWithErrorHandling($this->env->mergeGlobals($context), array_merge($this->blocks$blocks));
  313.     }
  314.     public function render(array $context)
  315.     {
  316.         $level ob_get_level();
  317.         ob_start();
  318.         try {
  319.             $this->display($context);
  320.         } catch (\Throwable $e) {
  321.             while (ob_get_level() > $level) {
  322.                 ob_end_clean();
  323.             }
  324.             throw $e;
  325.         }
  326.         return ob_get_clean();
  327.     }
  328.     protected function displayWithErrorHandling(array $context, array $blocks = [])
  329.     {
  330.         try {
  331.             $this->doDisplay($context$blocks);
  332.         } catch (Error $e) {
  333.             if (!$e->getSourceContext()) {
  334.                 $e->setSourceContext($this->getSourceContext());
  335.             }
  336.             // this is mostly useful for \Twig\Error\LoaderError exceptions
  337.             // see \Twig\Error\LoaderError
  338.             if (-=== $e->getTemplateLine()) {
  339.                 $e->guess();
  340.             }
  341.             throw $e;
  342.         } catch (\Exception $e) {
  343.             $e = new RuntimeError(sprintf('An exception has been thrown during the rendering of a template ("%s").'$e->getMessage()), -1$this->getSourceContext(), $e);
  344.             $e->guess();
  345.             throw $e;
  346.         }
  347.     }
  348.     /**
  349.      * Auto-generated method to display the template with the given context.
  350.      *
  351.      * @param array $context An array of parameters to pass to the template
  352.      * @param array $blocks  An array of blocks to pass to the template
  353.      */
  354.     abstract protected function doDisplay(array $context, array $blocks = []);
  355. }
  356. class_alias('Twig\Template''Twig_Template');