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

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\RuntimeError;
  14. /**
  15.  * Default base class for compiled templates.
  16.  *
  17.  * This class is an implementation detail of how template compilation currently
  18.  * works, which might change. It should never be used directly. Use $twig->load()
  19.  * instead, which returns an instance of \Twig\TemplateWrapper.
  20.  *
  21.  * @author Fabien Potencier <fabien@symfony.com>
  22.  *
  23.  * @internal
  24.  */
  25. abstract class Template
  26. {
  27.     public const ANY_CALL 'any';
  28.     public const ARRAY_CALL 'array';
  29.     public const METHOD_CALL 'method';
  30.     protected $parent;
  31.     protected $parents = [];
  32.     protected $blocks = [];
  33.     protected $traits = [];
  34.     protected $traitAliases = [];
  35.     protected $extensions = [];
  36.     protected $sandbox;
  37.     private $useYield;
  38.     public function __construct(
  39.         protected Environment $env,
  40.     ) {
  41.         $this->useYield $env->useYield();
  42.         $this->extensions $env->getExtensions();
  43.     }
  44.     /**
  45.      * Returns the template name.
  46.      */
  47.     abstract public function getTemplateName(): string;
  48.     /**
  49.      * Returns debug information about the template.
  50.      *
  51.      * @return array<int, int> Debug information
  52.      */
  53.     abstract public function getDebugInfo(): array;
  54.     /**
  55.      * Returns information about the original template source code.
  56.      */
  57.     abstract public function getSourceContext(): Source;
  58.     /**
  59.      * Returns the parent template.
  60.      *
  61.      * This method is for internal use only and should never be called
  62.      * directly.
  63.      *
  64.      * @return self|TemplateWrapper|false The parent template or false if there is no parent
  65.      */
  66.     public function getParent(array $context): self|TemplateWrapper|false
  67.     {
  68.         if (null !== $this->parent) {
  69.             return $this->parent;
  70.         }
  71.         if (!$parent $this->doGetParent($context)) {
  72.             return false;
  73.         }
  74.         if ($parent instanceof self || $parent instanceof TemplateWrapper) {
  75.             return $this->parents[$parent->getSourceContext()->getName()] = $parent;
  76.         }
  77.         if (!isset($this->parents[$parent])) {
  78.             $this->parents[$parent] = $this->load($parent, -1);
  79.         }
  80.         return $this->parents[$parent];
  81.     }
  82.     protected function doGetParent(array $context): bool|string|self|TemplateWrapper
  83.     {
  84.         return false;
  85.     }
  86.     public function isTraitable(): bool
  87.     {
  88.         return true;
  89.     }
  90.     /**
  91.      * Displays a parent block.
  92.      *
  93.      * This method is for internal use only and should never be called
  94.      * directly.
  95.      *
  96.      * @param string $name    The block name to display from the parent
  97.      * @param array  $context The context
  98.      * @param array  $blocks  The current set of blocks
  99.      */
  100.     public function displayParentBlock($name, array $context, array $blocks = []): void
  101.     {
  102.         foreach ($this->yieldParentBlock($name$context$blocks) as $data) {
  103.             echo $data;
  104.         }
  105.     }
  106.     /**
  107.      * Displays a block.
  108.      *
  109.      * This method is for internal use only and should never be called
  110.      * directly.
  111.      *
  112.      * @param string $name      The block name to display
  113.      * @param array  $context   The context
  114.      * @param array  $blocks    The current set of blocks
  115.      * @param bool   $useBlocks Whether to use the current set of blocks
  116.      */
  117.     public function displayBlock($name, array $context, array $blocks = [], $useBlocks true, ?self $templateContext null): void
  118.     {
  119.         foreach ($this->yieldBlock($name$context$blocks$useBlocks$templateContext) as $data) {
  120.             echo $data;
  121.         }
  122.     }
  123.     /**
  124.      * Renders a parent block.
  125.      *
  126.      * This method is for internal use only and should never be called
  127.      * directly.
  128.      *
  129.      * @param string $name    The block name to render from the parent
  130.      * @param array  $context The context
  131.      * @param array  $blocks  The current set of blocks
  132.      *
  133.      * @return string The rendered block
  134.      */
  135.     public function renderParentBlock($name, array $context, array $blocks = []): string
  136.     {
  137.         if (!$this->useYield) {
  138.             if ($this->env->isDebug()) {
  139.                 ob_start();
  140.             } else {
  141.                 ob_start(function () { return ''; });
  142.             }
  143.             $this->displayParentBlock($name$context$blocks);
  144.             return ob_get_clean();
  145.         }
  146.         $content '';
  147.         foreach ($this->yieldParentBlock($name$context$blocks) as $data) {
  148.             $content .= $data;
  149.         }
  150.         return $content;
  151.     }
  152.     /**
  153.      * Renders a block.
  154.      *
  155.      * This method is for internal use only and should never be called
  156.      * directly.
  157.      *
  158.      * @param string $name      The block name to render
  159.      * @param array  $context   The context
  160.      * @param array  $blocks    The current set of blocks
  161.      * @param bool   $useBlocks Whether to use the current set of blocks
  162.      *
  163.      * @return string The rendered block
  164.      */
  165.     public function renderBlock($name, array $context, array $blocks = [], $useBlocks true): string
  166.     {
  167.         if (!$this->useYield) {
  168.             $level ob_get_level();
  169.             if ($this->env->isDebug()) {
  170.                 ob_start();
  171.             } else {
  172.                 ob_start(function () { return ''; });
  173.             }
  174.             try {
  175.                 $this->displayBlock($name$context$blocks$useBlocks);
  176.             } catch (\Throwable $e) {
  177.                 while (ob_get_level() > $level) {
  178.                     ob_end_clean();
  179.                 }
  180.                 throw $e;
  181.             }
  182.             return ob_get_clean();
  183.         }
  184.         $content '';
  185.         foreach ($this->yieldBlock($name$context$blocks$useBlocks) as $data) {
  186.             $content .= $data;
  187.         }
  188.         return $content;
  189.     }
  190.     /**
  191.      * Returns whether a block exists or not in the current context of the template.
  192.      *
  193.      * This method checks blocks defined in the current template
  194.      * or defined in "used" traits or defined in parent templates.
  195.      *
  196.      * @param string $name    The block name
  197.      * @param array  $context The context
  198.      * @param array  $blocks  The current set of blocks
  199.      *
  200.      * @return bool true if the block exists, false otherwise
  201.      */
  202.     public function hasBlock($name, array $context, array $blocks = []): bool
  203.     {
  204.         if (isset($blocks[$name])) {
  205.             return $blocks[$name][0] instanceof self;
  206.         }
  207.         if (isset($this->blocks[$name])) {
  208.             return true;
  209.         }
  210.         if ($parent $this->getParent($context)) {
  211.             return $parent->hasBlock($name$context);
  212.         }
  213.         return false;
  214.     }
  215.     /**
  216.      * Returns all block names in the current context of the template.
  217.      *
  218.      * This method checks blocks defined in the current template
  219.      * or defined in "used" traits or defined in parent templates.
  220.      *
  221.      * @param array $context The context
  222.      * @param array $blocks  The current set of blocks
  223.      *
  224.      * @return array<string> An array of block names
  225.      */
  226.     public function getBlockNames(array $context, array $blocks = []): array
  227.     {
  228.         $names array_merge(array_keys($blocks), array_keys($this->blocks));
  229.         if ($parent $this->getParent($context)) {
  230.             $names array_merge($names$parent->getBlockNames($context));
  231.         }
  232.         return array_unique($names);
  233.     }
  234.     /**
  235.      * @param string|TemplateWrapper|array<string|TemplateWrapper> $template
  236.      */
  237.     protected function load(string|TemplateWrapper|array $templateint $lineint|null $index null): self
  238.     {
  239.         try {
  240.             if (\is_array($template)) {
  241.                 return $this->env->resolveTemplate($template)->unwrap();
  242.             }
  243.             if ($template instanceof TemplateWrapper) {
  244.                 return $template->unwrap();
  245.             }
  246.             if ($template === $this->getTemplateName()) {
  247.                 $class = static::class;
  248.                 if (false !== $pos strrpos($class'___', -1)) {
  249.                     $class substr($class0$pos);
  250.                 }
  251.             } else {
  252.                 $class $this->env->getTemplateClass($template);
  253.             }
  254.             return $this->env->loadTemplate($class$template$index);
  255.         } catch (Error $e) {
  256.             if (!$e->getSourceContext()) {
  257.                 $e->setSourceContext($this->getSourceContext());
  258.             }
  259.             if ($e->getTemplateLine() > 0) {
  260.                 throw $e;
  261.             }
  262.             if (-=== $line) {
  263.                 $e->guess();
  264.             } else {
  265.                 $e->setTemplateLine($line);
  266.             }
  267.             throw $e;
  268.         }
  269.     }
  270.     /**
  271.      * @param string|TemplateWrapper|array<string|TemplateWrapper> $template
  272.      *
  273.      * @deprecated since Twig 3.21 and will be removed in 4.0. Use Template::load() instead.
  274.      */
  275.     protected function loadTemplate($template$templateName nullint|null $line nullint|null $index null): self|TemplateWrapper
  276.     {
  277.         trigger_deprecation('twig/twig''3.21''The "%s" method is deprecated.'__METHOD__);
  278.         if (null === $line) {
  279.             $line = -1;
  280.         }
  281.         if ($template instanceof self) {
  282.             return $template;
  283.         }
  284.         return $this->load($template$line$index);
  285.     }
  286.     /**
  287.      * @internal
  288.      *
  289.      * @return $this
  290.      */
  291.     public function unwrap(): self
  292.     {
  293.         return $this;
  294.     }
  295.     /**
  296.      * Returns all blocks.
  297.      *
  298.      * This method is for internal use only and should never be called
  299.      * directly.
  300.      *
  301.      * @return array An array of blocks
  302.      */
  303.     public function getBlocks(): array
  304.     {
  305.         return $this->blocks;
  306.     }
  307.     public function display(array $context, array $blocks = []): void
  308.     {
  309.         foreach ($this->yield($context$blocks) as $data) {
  310.             echo $data;
  311.         }
  312.     }
  313.     public function render(array $context): string
  314.     {
  315.         if (!$this->useYield) {
  316.             $level ob_get_level();
  317.             if ($this->env->isDebug()) {
  318.                 ob_start();
  319.             } else {
  320.                 ob_start(function () { return ''; });
  321.             }
  322.             try {
  323.                 $this->display($context);
  324.             } catch (\Throwable $e) {
  325.                 while (ob_get_level() > $level) {
  326.                     ob_end_clean();
  327.                 }
  328.                 throw $e;
  329.             }
  330.             return ob_get_clean();
  331.         }
  332.         $content '';
  333.         foreach ($this->yield($context) as $data) {
  334.             $content .= $data;
  335.         }
  336.         return $content;
  337.     }
  338.     /**
  339.      * @return iterable<scalar|\Stringable|null>
  340.      */
  341.     public function yield(array $context, array $blocks = []): iterable
  342.     {
  343.         $context += $this->env->getGlobals();
  344.         $blocks array_merge($this->blocks$blocks);
  345.         try {
  346.             yield from $this->doDisplay($context$blocks);
  347.         } catch (Error $e) {
  348.             if (!$e->getSourceContext()) {
  349.                 $e->setSourceContext($this->getSourceContext());
  350.             }
  351.             // this is mostly useful for \Twig\Error\LoaderError exceptions
  352.             // see \Twig\Error\LoaderError
  353.             if (-=== $e->getTemplateLine()) {
  354.                 $e->guess();
  355.             }
  356.             throw $e;
  357.         } catch (\Throwable $e) {
  358.             $e = new RuntimeError(\sprintf('An exception has been thrown during the rendering of a template ("%s").'$e->getMessage()), -1$this->getSourceContext(), $e);
  359.             $e->guess();
  360.             throw $e;
  361.         }
  362.     }
  363.     /**
  364.      * @return iterable<scalar|\Stringable|null>
  365.      */
  366.     public function yieldBlock($name, array $context, array $blocks = [], $useBlocks true, ?self $templateContext null): iterable
  367.     {
  368.         if ($useBlocks && isset($blocks[$name])) {
  369.             $template $blocks[$name][0];
  370.             $block $blocks[$name][1];
  371.         } elseif (isset($this->blocks[$name])) {
  372.             $template $this->blocks[$name][0];
  373.             $block $this->blocks[$name][1];
  374.         } else {
  375.             $template null;
  376.             $block null;
  377.         }
  378.         // avoid RCEs when sandbox is enabled
  379.         if (null !== $template && !$template instanceof self) {
  380.             throw new \LogicException('A block must be a method on a \Twig\Template instance.');
  381.         }
  382.         if (null !== $template) {
  383.             try {
  384.                 yield from $template->$block($context$blocks);
  385.             } catch (Error $e) {
  386.                 if (!$e->getSourceContext()) {
  387.                     $e->setSourceContext($template->getSourceContext());
  388.                 }
  389.                 // this is mostly useful for \Twig\Error\LoaderError exceptions
  390.                 // see \Twig\Error\LoaderError
  391.                 if (-=== $e->getTemplateLine()) {
  392.                     $e->guess();
  393.                 }
  394.                 throw $e;
  395.             } catch (\Throwable $e) {
  396.                 $e = new RuntimeError(\sprintf('An exception has been thrown during the rendering of a template ("%s").'$e->getMessage()), -1$template->getSourceContext(), $e);
  397.                 $e->guess();
  398.                 throw $e;
  399.             }
  400.         } elseif ($parent $this->getParent($context)) {
  401.             yield from $parent->unwrap()->yieldBlock($name$contextarray_merge($this->blocks$blocks), false$templateContext ?? $this);
  402.         } elseif (isset($blocks[$name])) {
  403.             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());
  404.         } else {
  405.             throw new RuntimeError(\sprintf('Block "%s" on template "%s" does not exist.'$name$this->getTemplateName()), -1, ($templateContext ?? $this)->getSourceContext());
  406.         }
  407.     }
  408.     /**
  409.      * Yields a parent block.
  410.      *
  411.      * This method is for internal use only and should never be called
  412.      * directly.
  413.      *
  414.      * @param string $name    The block name to display from the parent
  415.      * @param array  $context The context
  416.      * @param array  $blocks  The current set of blocks
  417.      *
  418.      * @return iterable<scalar|\Stringable|null>
  419.      */
  420.     public function yieldParentBlock($name, array $context, array $blocks = []): iterable
  421.     {
  422.         if (isset($this->traits[$name])) {
  423.             yield from $this->traits[$name][0]->yieldBlock($this->traitAliases[$name] ?? $name$context$blocksfalse);
  424.         } elseif ($parent $this->getParent($context)) {
  425.             yield from $parent->unwrap()->yieldBlock($name$context$blocksfalse);
  426.         } else {
  427.             throw new RuntimeError(\sprintf('The template has no parent and no traits defining the "%s" block.'$name), -1$this->getSourceContext());
  428.         }
  429.     }
  430.     protected function hasMacro(string $name, array $context): bool
  431.     {
  432.         if (method_exists($this$name)) {
  433.             return true;
  434.         }
  435.         if (!$parent $this->getParent($context)) {
  436.             return false;
  437.         }
  438.         return $parent->hasMacro($name$context);
  439.     }
  440.     protected function getTemplateForMacro(string $name, array $contextint $lineSource $source): self
  441.     {
  442.         if (method_exists($this$name)) {
  443.             return $this;
  444.         }
  445.         $parent $this;
  446.         while ($parent $parent->getParent($context)) {
  447.             if (method_exists($parent$name)) {
  448.                 return $parent;
  449.             }
  450.         }
  451.         throw new RuntimeError(\sprintf('Macro "%s" is not defined in template "%s".'substr($name\strlen('macro_')), $this->getTemplateName()), $line$source);
  452.     }
  453.     /**
  454.      * Auto-generated method to display the template with the given context.
  455.      *
  456.      * @param array $context An array of parameters to pass to the template
  457.      * @param array $blocks  An array of blocks to pass to the template
  458.      *
  459.      * @return iterable<scalar|\Stringable|null>
  460.      */
  461.     abstract protected function doDisplay(array $context, array $blocks = []): iterable;
  462. }