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) {
         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 #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) {
         $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 #3
0
 /**
  * Compiles a node.
  *
  * @param IfwPsn_Vendor_Twig_NodeInterface $node        The node to compile
  * @param integer            $indentation The current indentation
  *
  * @return IfwPsn_Vendor_Twig_Compiler The current compiler instance
  */
 public function compile(IfwPsn_Vendor_Twig_NodeInterface $node, $indentation = 0)
 {
     $this->lastLine = null;
     $this->source = '';
     $this->sourceOffset = 0;
     // source code starts at 1 (as we then increment it when we encounter new lines)
     $this->sourceLine = 1;
     $this->indentation = $indentation;
     if ($node instanceof IfwPsn_Vendor_Twig_Node_Module) {
         $this->filename = $node->getAttribute('filename');
     }
     $node->compile($this);
     return $this;
 }
Example #4
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;
 }