Exemple #1
0
 /**
  * Called before child nodes are visited.
  *
  * @param IfwPsn_Vendor_Twig_NodeInterface $node The node to visit
  * @param IfwPsn_Vendor_Twig_Environment   $env  The Twig environment instance
  *
  * @return IfwPsn_Vendor_Twig_NodeInterface The modified node
  */
 public function enterNode(IfwPsn_Vendor_Twig_NodeInterface $node, IfwPsn_Vendor_Twig_Environment $env)
 {
     if ($node instanceof IfwPsn_Vendor_Twig_Node_Module) {
         $this->inAModule = true;
         $this->tags = array();
         $this->filters = array();
         $this->functions = array();
         return $node;
     } elseif ($this->inAModule) {
         // look for tags
         if ($node->getNodeTag()) {
             $this->tags[] = $node->getNodeTag();
         }
         // look for filters
         if ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Filter) {
             $this->filters[] = $node->getNode('filter')->getAttribute('value');
         }
         // look for functions
         if ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Function) {
             $this->functions[] = $node->getAttribute('name');
         }
         // wrap print to check __toString() calls
         if ($node instanceof IfwPsn_Vendor_Twig_Node_Print) {
             return new IfwPsn_Vendor_Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag());
         }
     }
     return $node;
 }
Exemple #2
0
 public function __construct(IfwPsn_Vendor_Twig_NodeInterface $node, IfwPsn_Vendor_Twig_Node_Expression_Constant $filterName, IfwPsn_Vendor_Twig_NodeInterface $arguments, $lineno, $tag = null)
 {
     $default = new IfwPsn_Vendor_Twig_Node_Expression_Filter($node, new IfwPsn_Vendor_Twig_Node_Expression_Constant('default', $node->getLine()), $arguments, $node->getLine());
     if ('default' === $filterName->getAttribute('value') && ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Name || $node instanceof IfwPsn_Vendor_Twig_Node_Expression_GetAttr)) {
         $test = new IfwPsn_Vendor_Twig_Node_Expression_Test_Defined(clone $node, 'defined', new IfwPsn_Vendor_Twig_Node(), $node->getLine());
         $false = count($arguments) ? $arguments->getNode(0) : new IfwPsn_Vendor_Twig_Node_Expression_Constant('', $node->getLine());
         $node = new IfwPsn_Vendor_Twig_Node_Expression_Conditional($test, $default, $false, $node->getLine());
     } else {
         $node = $default;
     }
     parent::__construct($node, $filterName, $arguments, $lineno, $tag);
 }
Exemple #3
0
 /**
  * Adds debugging information.
  *
  * @param IfwPsn_Vendor_Twig_NodeInterface $node The related twig node
  *
  * @return IfwPsn_Vendor_Twig_Compiler The current compiler instance
  */
 public function addDebugInfo(IfwPsn_Vendor_Twig_NodeInterface $node)
 {
     if ($node->getLine() != $this->lastLine) {
         $this->write(sprintf("// line %d\n", $node->getLine()));
         // when mbstring.func_overload is set to 2
         // mb_substr_count() replaces substr_count()
         // but they have different signatures!
         if ((int) ini_get('mbstring.func_overload') & 2) {
             // this is much slower than the "right" version
             $this->sourceLine += mb_substr_count(mb_substr($this->source, $this->sourceOffset), "\n");
         } else {
             $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
         }
         $this->sourceOffset = strlen($this->source);
         $this->debugInfo[$this->sourceLine] = $node->getLine();
         $this->lastLine = $node->getLine();
     }
     return $this;
 }
Exemple #4
0
 protected function checkLoopUsageBody(IfwPsn_Vendor_Twig_TokenStream $stream, IfwPsn_Vendor_Twig_NodeInterface $node)
 {
     if ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof IfwPsn_Vendor_Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) {
         $attribute = $node->getNode('attribute');
         if ($attribute instanceof IfwPsn_Vendor_Twig_Node_Expression_Constant && in_array($attribute->getAttribute('value'), array('length', 'revindex0', 'revindex', 'last'))) {
             throw new IfwPsn_Vendor_Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition', $attribute->getAttribute('value')), $node->getLine(), $stream->getFilename());
         }
     }
     // should check for parent.loop.XXX usage
     if ($node instanceof IfwPsn_Vendor_Twig_Node_For) {
         return;
     }
     foreach ($node as $n) {
         if (!$n) {
             continue;
         }
         $this->checkLoopUsageBody($stream, $n);
     }
 }
Exemple #5
0
 protected function filterBodyNodes(IfwPsn_Vendor_Twig_NodeInterface $node)
 {
     // check that the body does not contain non-empty output nodes
     if ($node instanceof IfwPsn_Vendor_Twig_Node_Text && !ctype_space($node->getAttribute('data')) || !$node instanceof IfwPsn_Vendor_Twig_Node_Text && !$node instanceof IfwPsn_Vendor_Twig_Node_BlockReference && $node instanceof IfwPsn_Vendor_Twig_NodeOutputInterface) {
         if (false !== strpos((string) $node, chr(0xef) . chr(0xbb) . chr(0xbf))) {
             throw new IfwPsn_Vendor_Twig_Error_Syntax('A template that extends another one cannot have a body but a byte order mark (BOM) has been detected; it must be removed.', $node->getLine(), $this->getFilename());
         }
         throw new IfwPsn_Vendor_Twig_Error_Syntax('A template that extends another one cannot have a body.', $node->getLine(), $this->getFilename());
     }
     // bypass "set" nodes as they "capture" the output
     if ($node instanceof IfwPsn_Vendor_Twig_Node_Set) {
         return $node;
     }
     if ($node instanceof IfwPsn_Vendor_Twig_NodeOutputInterface) {
         return;
     }
     foreach ($node as $k => $n) {
         if (null !== $n && null === $this->filterBodyNodes($n)) {
             $node->removeNode($k);
         }
     }
     return $node;
 }
Exemple #6
0
 protected function getEscaperFilter($type, IfwPsn_Vendor_Twig_NodeInterface $node)
 {
     $line = $node->getLine();
     $name = new IfwPsn_Vendor_Twig_Node_Expression_Constant('escape', $line);
     $args = new IfwPsn_Vendor_Twig_Node(array(new IfwPsn_Vendor_Twig_Node_Expression_Constant((string) $type, $line), new IfwPsn_Vendor_Twig_Node_Expression_Constant(null, $line), new IfwPsn_Vendor_Twig_Node_Expression_Constant(true, $line)));
     return new IfwPsn_Vendor_Twig_Node_Expression_Filter($node, $name, $args, $line);
 }
Exemple #7
0
 public function parseTestExpression(IfwPsn_Vendor_Twig_Parser $parser, IfwPsn_Vendor_Twig_NodeInterface $node)
 {
     $stream = $parser->getStream();
     $name = $stream->expect(IfwPsn_Vendor_Twig_Token::NAME_TYPE)->getValue();
     $class = $this->getTestNodeClass($parser, $name, $node->getLine());
     $arguments = null;
     if ($stream->test(IfwPsn_Vendor_Twig_Token::PUNCTUATION_TYPE, '(')) {
         $arguments = $parser->getExpressionParser()->parseArguments(true);
     }
     return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
 }
Exemple #8
0
 /**
  * Optimizes "for" tag by removing the "loop" variable creation whenever possible.
  *
  * @param IfwPsn_Vendor_Twig_NodeInterface $node A Node
  * @param IfwPsn_Vendor_Twig_Environment   $env  The current Twig environment
  */
 protected function enterOptimizeFor(IfwPsn_Vendor_Twig_NodeInterface $node, IfwPsn_Vendor_Twig_Environment $env)
 {
     if ($node instanceof IfwPsn_Vendor_Twig_Node_For) {
         // disable the loop variable by default
         $node->setAttribute('with_loop', false);
         array_unshift($this->loops, $node);
     } elseif (!$this->loops) {
         // we are outside a loop
         return;
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Name && 'loop' === $node->getAttribute('name')) {
         $this->addLoopToCurrent();
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_BlockReference || $node instanceof IfwPsn_Vendor_Twig_Node_Expression_BlockReference) {
         $this->addLoopToCurrent();
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Include && !$node->getAttribute('only')) {
         $this->addLoopToAll();
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_GetAttr && (!$node->getNode('attribute') instanceof IfwPsn_Vendor_Twig_Node_Expression_Constant || 'parent' === $node->getNode('attribute')->getAttribute('value')) && (true === $this->loops[0]->getAttribute('with_loop') || $node->getNode('node') instanceof IfwPsn_Vendor_Twig_Node_Expression_Name && 'loop' === $node->getNode('node')->getAttribute('name'))) {
         $this->addLoopToAll();
     }
 }