shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Set.php (3211B)


      1 <?php
      2 
      3 /*
      4  * This file is part of Twig.
      5  *
      6  * (c) 2010 Fabien Potencier
      7  *
      8  * For the full copyright and license information, please view the LICENSE
      9  * file that was distributed with this source code.
     10  */
     11 
     12 /**
     13  * Represents a set node.
     14  *
     15  * @author Fabien Potencier <fabien@symfony.com>
     16  */
     17 class Twig_Node_Set extends Twig_Node
     18 {
     19     public function __construct($capture, Twig_NodeInterface $names, Twig_NodeInterface $values, $lineno, $tag = null)
     20     {
     21         parent::__construct(array('names' => $names, 'values' => $values), array('capture' => $capture, 'safe' => false), $lineno, $tag);
     22 
     23         /*
     24          * Optimizes the node when capture is used for a large block of text.
     25          *
     26          * {% set foo %}foo{% endset %} is compiled to $context['foo'] = new Twig_Markup("foo");
     27          */
     28         if ($this->getAttribute('capture')) {
     29             $this->setAttribute('safe', true);
     30 
     31             $values = $this->getNode('values');
     32             if ($values instanceof Twig_Node_Text) {
     33                 $this->setNode('values', new Twig_Node_Expression_Constant($values->getAttribute('data'), $values->getLine()));
     34                 $this->setAttribute('capture', false);
     35             }
     36         }
     37     }
     38 
     39     public function compile(Twig_Compiler $compiler)
     40     {
     41         $compiler->addDebugInfo($this);
     42 
     43         if (count($this->getNode('names')) > 1) {
     44             $compiler->write('list(');
     45             foreach ($this->getNode('names') as $idx => $node) {
     46                 if ($idx) {
     47                     $compiler->raw(', ');
     48                 }
     49 
     50                 $compiler->subcompile($node);
     51             }
     52             $compiler->raw(')');
     53         } else {
     54             if ($this->getAttribute('capture')) {
     55                 $compiler
     56                     ->write("ob_start();\n")
     57                     ->subcompile($this->getNode('values'))
     58                 ;
     59             }
     60 
     61             $compiler->subcompile($this->getNode('names'), false);
     62 
     63             if ($this->getAttribute('capture')) {
     64                 $compiler->raw(" = ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())");
     65             }
     66         }
     67 
     68         if (!$this->getAttribute('capture')) {
     69             $compiler->raw(' = ');
     70 
     71             if (count($this->getNode('names')) > 1) {
     72                 $compiler->write('array(');
     73                 foreach ($this->getNode('values') as $idx => $value) {
     74                     if ($idx) {
     75                         $compiler->raw(', ');
     76                     }
     77 
     78                     $compiler->subcompile($value);
     79                 }
     80                 $compiler->raw(')');
     81             } else {
     82                 if ($this->getAttribute('safe')) {
     83                     $compiler
     84                         ->raw("('' === \$tmp = ")
     85                         ->subcompile($this->getNode('values'))
     86                         ->raw(") ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())")
     87                     ;
     88                 } else {
     89                     $compiler->subcompile($this->getNode('values'));
     90                 }
     91             }
     92         }
     93 
     94         $compiler->raw(";\n");
     95     }
     96 }