vendor/twig/twig/lib/Twig/Template.php line 363

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