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

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 = array();
  29.     protected $env;
  30.     protected $blocks = array();
  31.     protected $traits = array();
  32.     /**
  33.      * @internal
  34.      */
  35.     protected $extensions = array();
  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|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) {
  94.                 return $this->parents[$parent->getTemplateName()] = $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 = array())
  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 = array(), $useBlocks true)
  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);
  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]->getTemplateName());
  186.         } else {
  187.             throw new Twig_Error_Runtime(sprintf('Block "%s" on template "%s" does not exist.'$name$this->getTemplateName()), -1$this->getTemplateName());
  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 = array())
  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 = array(), $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 = array())
  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 = array())
  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) {
  286.                 return $template;
  287.             }
  288.             if ($template instanceof Twig_TemplateWrapper) {
  289.                 return $template;
  290.             }
  291.             return $this->env->loadTemplate($template$index);
  292.         } catch (Twig_Error $e) {
  293.             if (!$e->getSourceContext()) {
  294.                 $e->setSourceContext($templateName ? new Twig_Source(''$templateName) : $this->getSourceContext());
  295.             }
  296.             if ($e->getTemplateLine()) {
  297.                 throw $e;
  298.             }
  299.             if (!$line) {
  300.                 $e->guess();
  301.             } else {
  302.                 $e->setTemplateLine($line);
  303.             }
  304.             throw $e;
  305.         }
  306.     }
  307.     /**
  308.      * Returns all blocks.
  309.      *
  310.      * This method is for internal use only and should never be called
  311.      * directly.
  312.      *
  313.      * @return array An array of blocks
  314.      *
  315.      * @internal
  316.      */
  317.     public function getBlocks()
  318.     {
  319.         return $this->blocks;
  320.     }
  321.     public function display(array $context, array $blocks = array())
  322.     {
  323.         $this->displayWithErrorHandling($this->env->mergeGlobals($context), array_merge($this->blocks$blocks));
  324.     }
  325.     public function render(array $context)
  326.     {
  327.         $level ob_get_level();
  328.         ob_start();
  329.         try {
  330.             $this->display($context);
  331.         } catch (Throwable $e) {
  332.             while (ob_get_level() > $level) {
  333.                 ob_end_clean();
  334.             }
  335.             throw $e;
  336.         }
  337.         return ob_get_clean();
  338.     }
  339.     protected function displayWithErrorHandling(array $context, array $blocks = array())
  340.     {
  341.         try {
  342.             $this->doDisplay($context$blocks);
  343.         } catch (Twig_Error $e) {
  344.             if (!$e->getSourceContext()) {
  345.                 $e->setSourceContext($this->getSourceContext());
  346.             }
  347.             // this is mostly useful for Twig_Error_Loader exceptions
  348.             // see Twig_Error_Loader
  349.             if (false === $e->getTemplateLine()) {
  350.                 $e->setTemplateLine(-1);
  351.                 $e->guess();
  352.             }
  353.             throw $e;
  354.         } catch (Exception $e) {
  355.             throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").'$e->getMessage()), -1$this->getSourceContext(), $e);
  356.         }
  357.     }
  358.     /**
  359.      * Auto-generated method to display the template with the given context.
  360.      *
  361.      * @param array $context An array of parameters to pass to the template
  362.      * @param array $blocks  An array of blocks to pass to the template
  363.      */
  364.     abstract protected function doDisplay(array $context, array $blocks = array());
  365. }
  366. class_alias('Twig_Template''Twig\Template'false);