| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Gerard van Helden <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Zicht Online <http://zicht.nl> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Zicht\Bundle\FrameworkExtraBundle\Twig\ControlStructures; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Twig_Compiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Represents a 'with' node. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class WithNode extends \Twig_Node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * Constructs the node. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @param array $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param array $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param int $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param null|string $line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param string $tag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function __construct($items, $body, $options, $line, $tag) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         parent::__construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             array('body' => $body), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             array('items' => $items, 'options' => $options), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $line, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $tag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Checks if an option is set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function hasOption($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $this->getAttribute('options') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             && in_array($value, $this->getAttribute('options')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Compiles the node. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param \Twig_Compiler $compiler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     public function compile(Twig_Compiler $compiler) | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $compiler | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             ->addDebugInfo($this) | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             ->write('if(!isset($withStack)) {' . "\n") | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             ->indent() | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             ->write('$withStack = array();' . "\n") | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             ->outdent() | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             ->write('}' . "\n") | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             ->write("\n") | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             ->write('array_push($withStack, $context);' . "\n"); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $compiler->write('$values = array_merge(' . "\n")->indent(); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         $i = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         foreach ($this->getAttribute('items') as $argument) { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             if ($i ++ > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                 $compiler->raw(',' . "\n"); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             $this->compileArgument($compiler, $argument); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         $compiler->raw("\n")->outdent()->write(");\n"); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         if (!$this->hasOption('always')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             $compiler->write( | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 'if (count(array_filter($values, function($o) { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                     if ($o instanceof \Countable) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                         return count($o) > 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                     } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                         return !empty($o); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |                 }))) {' | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         if ($this->hasOption('merged')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             $compiler->write('$values += $context;' . "\n"); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             $compiler->write('$values += array(\'_parent\' => $context);'); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $compiler | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             ->write('$context = $values;') | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             ->subcompile($this->getNode('body')); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         if (!$this->hasOption('always')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             $compiler->write('}'); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         $compiler->write('$context = array_pop($withStack);' . "\n"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Compiles the 'with' argument. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param Twig_Compiler $compiler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param mixed $argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function compileArgument($compiler, $argument) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         if (empty($argument['name'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $compiler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 ->write('(array) ') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 ->subcompile($argument['value']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $compiler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 ->write('array(') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 ->repr($argument['name']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 ->raw(' => ') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 ->subcompile($argument['value']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 ->raw(')'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 129 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |  |