Exemple #1
0
 /**
  * @group legacy
  */
 public function testLegacyConstructorSignature()
 {
     $stream = new Twig_TokenStream(array(), 'foo', '{{ foo }}');
     $this->assertEquals('foo', $stream->getFilename());
     $this->assertEquals('{{ foo }}', $stream->getSource());
     $this->assertEquals('foo', $stream->getSourceContext()->getName());
     $this->assertEquals('{{ foo }}', $stream->getSourceContext()->getCode());
 }
Exemple #2
0
 protected function checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node)
 {
     if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) {
         $attribute = $node->getNode('attribute');
         if ($attribute instanceof Twig_Node_Expression_Constant && in_array($attribute->getAttribute('value'), array('length', 'revindex0', 'revindex', 'last'))) {
             throw new Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition.', $attribute->getAttribute('value')), $node->getTemplateLine(), $stream->getSourceContext()->getName());
         }
     }
     // should check for parent.loop.XXX usage
     if ($node instanceof Twig_Node_For) {
         return;
     }
     foreach ($node as $n) {
         if (!$n) {
             continue;
         }
         $this->checkLoopUsageBody($stream, $n);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function parse(Twig_TokenStream $stream, $test = null, $dropNeedle = false)
 {
     // push all variables into the stack to keep the current state of the parser
     // using get_object_vars() instead of foreach would lead to https://bugs.php.net/71336
     // This hack can be removed when min version if PHP 7.0
     $vars = array();
     foreach ($this as $k => $v) {
         $vars[$k] = $v;
     }
     unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames']);
     $this->stack[] = $vars;
     // tag handlers
     if (null === $this->handlers) {
         $this->handlers = $this->env->getTokenParsers();
         $this->handlers->setParser($this);
     }
     // node visitors
     if (null === $this->visitors) {
         $this->visitors = $this->env->getNodeVisitors();
     }
     if (null === $this->expressionParser) {
         $this->expressionParser = new Twig_ExpressionParser($this, $this->env);
     }
     $this->stream = $stream;
     $this->parent = null;
     $this->blocks = array();
     $this->macros = array();
     $this->traits = array();
     $this->blockStack = array();
     $this->importedSymbols = array(array());
     $this->embeddedTemplates = array();
     try {
         $body = $this->subparse($test, $dropNeedle);
         if (null !== $this->parent && null === ($body = $this->filterBodyNodes($body))) {
             $body = new Twig_Node();
         }
     } catch (Twig_Error_Syntax $e) {
         if (!$e->getTemplateName()) {
             $e->setTemplateName($this->stream->getSourceContext()->getName());
         }
         if (!$e->getTemplateLine()) {
             $e->setTemplateLine($this->stream->getCurrent()->getLine());
         }
         throw $e;
     }
     $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->embeddedTemplates, $stream->getSourceContext());
     $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
     $node = $traverser->traverse($node);
     // restore previous stack so previous parse() call can resume working
     foreach (array_pop($this->stack) as $key => $val) {
         $this->{$key} = $val;
     }
     return $node;
 }