예제 #1
0
파일: Sandbox.php 프로젝트: nmcteam/Twig
 public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof Twig_Node_Module) {
         $this->inAModule = true;
         $this->tags = array();
         $this->filters = array();
         return $node;
     } elseif ($this->inAModule) {
         // look for tags
         if ($node->getNodeTag()) {
             $this->tags[$node->getNodeTag()] = true;
         }
         // look for filters
         if ($node instanceof Twig_Node_Expression_Filter) {
             foreach ($node->getFilters() as $filter) {
                 $this->filters[$filter[0]] = true;
             }
         }
         // look for simple print statement ({{ article }})
         if ($node instanceof Twig_Node_Print && $node->getExpression() instanceof Twig_Node_Expression_Name) {
             return new Twig_Node_SandboxPrint($node->getExpression(), $node->getLine(), $node->getNodeTag());
         }
     }
     return $node;
 }
예제 #2
0
 /**
  * Called before child nodes are visited.
  *
  * @param Twig_NodeInterface $node The node to visit
  * @param Twig_Environment   $env  The Twig environment instance
  *
  * @return Twig_NodeInterface The modified node
  */
 public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof 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() && !isset($this->tags[$node->getNodeTag()])) {
             $this->tags[$node->getNodeTag()] = $node;
         }
         // look for filters
         if ($node instanceof Twig_Node_Expression_Filter && !isset($this->filters[$node->getNode('filter')->getAttribute('value')])) {
             $this->filters[$node->getNode('filter')->getAttribute('value')] = $node;
         }
         // look for functions
         if ($node instanceof Twig_Node_Expression_Function && !isset($this->functions[$node->getAttribute('name')])) {
             $this->functions[$node->getAttribute('name')] = $node;
         }
         // wrap print to check __toString() calls
         if ($node instanceof Twig_Node_Print) {
             return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag());
         }
     }
     return $node;
 }
예제 #3
0
 /**
  * Called before child nodes are visited.
  *
  * @param Twig_NodeInterface $node The node to visit
  * @param Twig_Environment   $env  The Twig environment instance
  *
  * @param Twig_NodeInterface The modified node
  */
 public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof Twig_Node_Module) {
         $this->inAModule = true;
         $this->tags = array();
         $this->filters = array();
         return $node;
     } elseif ($this->inAModule) {
         // look for tags
         if ($node->getNodeTag()) {
             $this->tags[] = $node->getNodeTag();
         }
         // look for filters
         if ($node instanceof Twig_Node_Expression_Filter) {
             for ($i = 0; $i < count($node->getNode('filters')); $i += 2) {
                 $this->filters[] = $node->getNode('filters')->getNode($i)->getAttribute('value');
             }
         }
         // look for simple print statements ({{ article }})
         if ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_Name) {
             return new Twig_Node_SandboxedPrint($node);
         }
     }
     return $node;
 }
 /**
  * Replaces "echo $this->renderBlock()" with "$this->displayBlock()".
  *
  * @param Twig_NodeInterface $node A Node
  * @param Twig_Environment   $env  The current Twig environment
  */
 protected function optimizeRenderBlock($node, $env)
 {
     if ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_BlockReference) {
         return new Twig_Node_BlockReference($node->getNode('expr')->getNode('name')->getAttribute('value'), $node->getLine(), $node->getNodeTag());
     }
     return $node;
 }
예제 #5
0
 /**
  * Defined by Twig_NodeVisitorInterface
  *
  * Extracts messages from calls to the translate function.
  */
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     if ($node instanceof \Skeleton\I18n\Template\Twig\Extension\Node\Trans\Tigron) {
         if ($node->getNodeTag() == 'trans') {
             $this->extracted[] = $node->getNode('body')->getAttribute('value');
         }
     } elseif ($node instanceof \Twig_Node_Print) {
         $n = $node->getNode('expr');
         while ($n instanceof \Twig_Node_Expression_Filter) {
             $filter = null;
             if ($n->hasNode('filter')) {
                 $filter = $n->getNode('filter')->getAttribute('value');
             }
             $n = $n->getNode('node');
             if ($n instanceof \Twig_Node_Expression_Constant and $filter == 'trans') {
                 $this->extracted[] = $n->getAttribute('value');
             }
         }
     } elseif ($node instanceof \Twig_Node_Expression_Array) {
         $data = $node->getIterator();
         foreach ($data as $row) {
             if ($row instanceof \Twig_Node_Expression_Filter) {
                 if ($row->hasNode('filter') and $row->getNode('filter')->getAttribute('value') == 'trans') {
                     $this->extracted[] = $row->getNode('node')->getAttribute('value');
                 }
             }
         }
     }
     return $node;
 }
예제 #6
0
파일: Sandbox.php 프로젝트: Zyko0/Baikal
 /**
  * Called before child nodes are visited.
  *
  * @param Twig_NodeInterface $node The node to visit
  * @param Twig_Environment   $env  The Twig environment instance
  *
  * @return Twig_NodeInterface The modified node
  */
 public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     // in a sandbox tag, only include tags are allowed
     if ($node instanceof Twig_Node_Sandbox && !$node->getNode('body') instanceof Twig_Node_Include) {
         foreach ($node->getNode('body') as $n) {
             if ($n instanceof Twig_Node_Text && ctype_space($n->getAttribute('data'))) {
                 continue;
             }
             if (!$n instanceof Twig_Node_Include) {
                 throw new Twig_Error_Syntax('Only "include" tags are allowed within a "sandbox" section', $n->getLine());
             }
         }
     }
     if ($node instanceof 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 Twig_Node_Expression_Filter) {
             $this->filters[] = $node->getNode('filter')->getAttribute('value');
         }
         // look for functions
         if ($node instanceof Twig_Node_Expression_Function) {
             $this->functions[] = $node->getAttribute('name');
         }
         // wrap print to check __toString() calls
         if ($node instanceof Twig_Node_Print) {
             return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag());
         }
     }
     return $node;
 }