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; |
8
|
|
|
use Twig_Node_Block; |
9
|
|
|
use Twig_Node_BlockReference; |
10
|
|
|
use Twig_Node_Print; |
11
|
|
|
use Twig_Token; |
12
|
|
|
use Twig_TokenParser; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class XlsBlockTokenParser |
16
|
|
|
* |
17
|
|
|
* @package MewesK\TwigExcelBundle\Twig\TokenParser |
18
|
|
|
*/ |
19
|
|
|
class XlsBlockTokenParser extends Twig_TokenParser |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Based on final class method Twig_TokenParser_Block::parse |
23
|
|
|
* |
24
|
|
|
* @param Twig_Token $token |
25
|
|
|
* @return Twig_Node_BlockReference |
26
|
|
|
* @throws Twig_Error_Syntax |
27
|
|
|
*/ |
28
|
|
|
public function parse(Twig_Token $token) |
29
|
|
|
{ |
30
|
|
|
$lineno = $token->getLine(); |
31
|
|
|
$stream = $this->parser->getStream(); |
32
|
|
|
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); |
33
|
|
|
if ($this->parser->hasBlock($name)) { |
34
|
|
|
throw new Twig_Error_Syntax(sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getTemplateLine()), $stream->getCurrent()->getLine(), $stream->getSourceContext()); |
35
|
|
|
} |
36
|
|
|
$this->parser->setBlock($name, $block = new Twig_Node_Block($name, new Twig_Node(array()), $lineno)); |
37
|
|
|
$this->parser->pushLocalScope(); |
38
|
|
|
$this->parser->pushBlockStack($name); |
39
|
|
|
|
40
|
|
|
if ($stream->nextIf(Twig_Token::BLOCK_END_TYPE)) { |
41
|
|
|
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); |
42
|
|
|
if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) { |
43
|
|
|
$value = $token->getValue(); |
44
|
|
|
|
45
|
|
|
if ($value !== $name) { |
46
|
|
|
throw new Twig_Error_Syntax(sprintf('Expected endblock for block "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getSourceContext()); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} else { |
50
|
|
|
$body = new Twig_Node(array( |
51
|
|
|
new Twig_Node_Print($this->parser->getExpressionParser()->parseExpression(), $lineno), |
52
|
|
|
)); |
53
|
|
|
} |
54
|
|
|
$stream->expect(Twig_Token::BLOCK_END_TYPE); |
55
|
|
|
|
56
|
|
|
$block->setNode('body', $body); |
57
|
|
|
$this->parser->popBlockStack(); |
58
|
|
|
$this->parser->popLocalScope(); |
59
|
|
|
|
60
|
|
|
$blockReference = new Twig_Node_BlockReference($name, $lineno, $this->getTag()); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var Twig_Node_Block $block |
64
|
|
|
*/ |
65
|
|
|
$block = $this->parser->getBlock($blockReference->getAttribute('name')); |
66
|
|
|
|
67
|
|
|
// prepare block |
68
|
|
|
NodeHelper::removeTextNodesRecursively($block, $this->parser); |
69
|
|
|
NodeHelper::fixMacroCallsRecursively($block); |
70
|
|
|
|
71
|
|
|
// mark for syntax checks |
72
|
|
|
foreach ($block->getIterator() as $node) { |
73
|
|
|
if ($node instanceof Twig_Node_Block) { |
74
|
|
|
$node->setAttribute('twigExcelBundle', true); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $blockReference; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Twig_Token $token |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function decideBlockEnd(Twig_Token $token) |
86
|
|
|
{ |
87
|
|
|
return $token->test('endxlsblock'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getTag() |
94
|
|
|
{ |
95
|
|
|
return 'xlsblock'; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|