Example #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;
 }
Example #2
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) {
         if ($env->hasExtension('escaper') && ($defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename')))) {
             $this->defaultStrategy = $defaultStrategy;
         }
         $this->safeVars = array();
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_AutoEscape) {
         $this->statusStack[] = $node->getAttribute('value');
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Block) {
         $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env);
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Import) {
         $this->safeVars[] = $node->getNode('var')->getAttribute('name');
     }
     return $node;
 }
Example #3
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);
     }
 }
Example #4
0
 /**
  * Removes "raw" filters.
  *
  * @param IfwPsn_Vendor_Twig_NodeInterface $node A Node
  * @param IfwPsn_Vendor_Twig_Environment   $env  The current Twig environment
  */
 protected function optimizeRawFilter(IfwPsn_Vendor_Twig_NodeInterface $node, IfwPsn_Vendor_Twig_Environment $env)
 {
     if ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Filter && 'raw' == $node->getNode('filter')->getAttribute('value')) {
         return $node->getNode('node');
     }
     return $node;
 }