| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace MewesK\TwigExcelBundle\Twig\TokenParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use MewesK\TwigExcelBundle\Twig\NodeHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Twig_Error_Syntax; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Twig_Node_Body; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Twig_Node_Expression_Constant; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Twig_Node_Macro; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Twig_Token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Twig_TokenParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Class XlsMacroTokenParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @package MewesK\TwigExcelBundle\Twig\TokenParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class XlsMacroTokenParser extends Twig_TokenParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Based on final class method Twig_TokenParser_Macro::parse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param Twig_Token $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @throws Twig_Error_Syntax | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     public function parse(Twig_Token $token) | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $lineno = $token->getLine(); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $stream = $this->parser->getStream(); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $arguments = $this->parser->getExpressionParser()->parseArguments(true, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         // fix macro context | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $arguments->setNode('phpExcel', new Twig_Node_Expression_Constant(null, null)); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $stream->expect(Twig_Token::BLOCK_END_TYPE); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $this->parser->pushLocalScope(); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             $value = $token->getValue(); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             if ($value !== $name) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                 throw new Twig_Error_Syntax(sprintf('Expected endxlsmacro for macro "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getFilename()); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $this->parser->popLocalScope(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $stream->expect(Twig_Token::BLOCK_END_TYPE); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         // remove all unwanted text nodes | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         NodeHelper::removeTextNodesRecursively($body, $this->parser); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         // fix the methodCalls inside the body | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         NodeHelper::fixMacroCallsRecursively($body); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $macro = new Twig_Node_Macro($name, new Twig_Node_Body(array($body)), $arguments, $lineno, $this->getTag()); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         // mark for syntax checks | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $macro->setAttribute('twigExcelBundle', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $this->parser->setMacro($name, $macro); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param Twig_Token $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function decideBlockEnd(Twig_Token $token) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $token->test('endxlsmacro'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function getTag() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return 'xlsmacro'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  |