vendor/twig/twig/src/Template.php line 392

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.         if ($this->env->isDebug()) {
  200.             ob_start();
  201.         } else {
  202.             ob_start(function () { return ''; });
  203.         }
  204.         $this->displayParentBlock($name$context$blocks);
  205.         return ob_get_clean();
  206.     }
  207.     /**
  208.      * Renders a block.
  209.      *
  210.      * This method is for internal use only and should never be called
  211.      * directly.
  212.      *
  213.      * @param string $name      The block name to render
  214.      * @param array  $context   The context
  215.      * @param array  $blocks    The current set of blocks
  216.      * @param bool   $useBlocks Whether to use the current set of blocks
  217.      *
  218.      * @return string The rendered block
  219.      */
  220.     public function renderBlock($name, array $context, array $blocks = [], $useBlocks true)
  221.     {
  222.         if ($this->env->isDebug()) {
  223.             ob_start();
  224.         } else {
  225.             ob_start(function () { return ''; });
  226.         }
  227.         $this->displayBlock($name$context$blocks$useBlocks);
  228.         return ob_get_clean();
  229.     }
  230.     /**
  231.      * Returns whether a block exists or not in the current context of the template.
  232.      *
  233.      * This method checks blocks defined in the current template
  234.      * or defined in "used" traits or defined in parent templates.
  235.      *
  236.      * @param string $name    The block name
  237.      * @param array  $context The context
  238.      * @param array  $blocks  The current set of blocks
  239.      *
  240.      * @return bool true if the block exists, false otherwise
  241.      */
  242.     public function hasBlock($name, array $context, array $blocks = [])
  243.     {
  244.         if (isset($blocks[$name])) {
  245.             return $blocks[$name][0] instanceof self;
  246.         }
  247.         if (isset($this->blocks[$name])) {
  248.             return true;
  249.         }
  250.         if (false !== $parent $this->getParent($context)) {
  251.             return $parent->hasBlock($name$context);
  252.         }
  253.         return false;
  254.     }
  255.     /**
  256.      * Returns all block names in the current context of the template.
  257.      *
  258.      * This method checks blocks defined in the current template
  259.      * or defined in "used" traits or defined in parent templates.
  260.      *
  261.      * @param array $context The context
  262.      * @param array $blocks  The current set of blocks
  263.      *
  264.      * @return array An array of block names
  265.      */
  266.     public function getBlockNames(array $context, array $blocks = [])
  267.     {
  268.         $names array_merge(array_keys($blocks), array_keys($this->blocks));
  269.         if (false !== $parent $this->getParent($context)) {
  270.             $names array_merge($names$parent->getBlockNames($context));
  271.         }
  272.         return array_unique($names);
  273.     }
  274.     /**
  275.      * @return Template|TemplateWrapper
  276.      */
  277.     protected function loadTemplate($template$templateName null$line null$index null)
  278.     {
  279.         try {
  280.             if (\is_array($template)) {
  281.                 return $this->env->resolveTemplate($template);
  282.             }
  283.             if ($template instanceof self || $template instanceof TemplateWrapper) {
  284.                 return $template;
  285.             }
  286.             if ($template === $this->getTemplateName()) {
  287.                 $class = \get_class($this);
  288.                 if (false !== $pos strrpos($class'___', -1)) {
  289.                     $class substr($class0$pos);
  290.                 }
  291.                 return $this->env->loadClass($class$template$index);
  292.             }
  293.             return $this->env->loadTemplate($template$index);
  294.         } catch (Error $e) {
  295.             if (!$e->getSourceContext()) {
  296.                 $e->setSourceContext($templateName ? new Source(''$templateName) : $this->getSourceContext());
  297.             }
  298.             if ($e->getTemplateLine() > 0) {
  299.                 throw $e;
  300.             }
  301.             if (!$line) {
  302.                 $e->guess();
  303.             } else {
  304.                 $e->setTemplateLine($line);
  305.             }
  306.             throw $e;
  307.         }
  308.     }
  309.     /**
  310.      * @internal
  311.      *
  312.      * @return Template
  313.      */
  314.     protected function unwrap()
  315.     {
  316.         return $this;
  317.     }
  318.     /**
  319.      * Returns all blocks.
  320.      *
  321.      * This method is for internal use only and should never be called
  322.      * directly.
  323.      *
  324.      * @return array An array of blocks
  325.      */
  326.     public function getBlocks()
  327.     {
  328.         return $this->blocks;
  329.     }
  330.     public function display(array $context, array $blocks = [])
  331.     {
  332.         $this->displayWithErrorHandling($this->env->mergeGlobals($context), array_merge($this->blocks$blocks));
  333.     }
  334.     public function render(array $context)
  335.     {
  336.         $level ob_get_level();
  337.         if ($this->env->isDebug()) {
  338.             ob_start();
  339.         } else {
  340.             ob_start(function () { return ''; });
  341.         }
  342.         try {
  343.             $this->display($context);
  344.         } catch (\Throwable $e) {
  345.             while (ob_get_level() > $level) {
  346.                 ob_end_clean();
  347.             }
  348.             throw $e;
  349.         }
  350.         return ob_get_clean();
  351.     }
  352.     protected function displayWithErrorHandling(array $context, array $blocks = [])
  353.     {
  354.         try {
  355.             $this->doDisplay($context$blocks);
  356.         } catch (Error $e) {
  357.             if (!$e->getSourceContext()) {
  358.                 $e->setSourceContext($this->getSourceContext());
  359.             }
  360.             // this is mostly useful for \Twig\Error\LoaderError exceptions
  361.             // see \Twig\Error\LoaderError
  362.             if (-=== $e->getTemplateLine()) {
  363.                 $e->guess();
  364.             }
  365.             throw $e;
  366.         } catch (\Exception $e) {
  367.             $e = new RuntimeError(sprintf('An exception has been thrown during the rendering of a template ("%s").'$e->getMessage()), -1$this->getSourceContext(), $e);
  368.             $e->guess();
  369.             throw $e;
  370.         }
  371.     }
  372.     /**
  373.      * Auto-generated method to display the template with the given context.
  374.      *
  375.      * @param array $context An array of parameters to pass to the template
  376.      * @param array $blocks  An array of blocks to pass to the template
  377.      */
  378.     abstract protected function doDisplay(array $context, array $blocks = []);
  379. }
  380. class_alias('Twig\Template''Twig_Template');