1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MewesK\TwigSpreadsheetBundle\Twig\NodeVisitor; |
4
|
|
|
|
5
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\Node\BaseNode; |
6
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\Node\DocumentNode; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SyntaxCheckNodeVisitor. |
10
|
|
|
*/ |
11
|
|
|
class SyntaxCheckNodeVisitor extends \Twig_BaseNodeVisitor |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $path = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
public function getPriority() |
22
|
|
|
{ |
23
|
|
|
return 0; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
* |
29
|
|
|
* @throws \Twig_Error_Syntax |
30
|
|
|
*/ |
31
|
|
|
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) |
32
|
|
|
{ |
33
|
|
|
try { |
34
|
|
|
if ($node instanceof BaseNode) { |
35
|
|
|
$this->checkAllowedParents($node); |
36
|
|
|
} else { |
37
|
|
|
$this->checkAllowedChildren($node); |
38
|
|
|
} |
39
|
|
|
} catch (\Twig_Error_Syntax $e) { |
40
|
|
|
// reset path since throwing an error prevents doLeaveNode to be called |
41
|
|
|
$this->path = []; |
42
|
|
|
throw $e; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->path[] = $node !== null ? get_class($node) : null; |
46
|
|
|
|
47
|
|
|
return $node; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
protected function doLeaveNode(\Twig_Node $node, \ Twig_Environment $env) |
54
|
|
|
{ |
55
|
|
|
array_pop($this->path); |
56
|
|
|
|
57
|
|
|
return $node; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \Twig_Node $node |
62
|
|
|
* |
63
|
|
|
* @throws \Twig_Error_Syntax |
64
|
|
|
*/ |
65
|
|
|
private function checkAllowedChildren(\Twig_Node $node) |
66
|
|
|
{ |
67
|
|
|
$hasDocumentNode = false; |
68
|
|
|
$hasTextNodeBefore = false; |
69
|
|
|
$hasTextNodeAfter = false; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var \Twig_Node $currentNode |
73
|
|
|
*/ |
74
|
|
|
foreach ($node->getIterator() as $currentNode) { |
75
|
|
|
if ($currentNode instanceof \Twig_Node_Text) { |
76
|
|
|
if ($hasDocumentNode) { |
77
|
|
|
$hasTextNodeAfter = true; |
78
|
|
|
} else { |
79
|
|
|
$hasTextNodeBefore = true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($currentNode instanceof DocumentNode) { |
84
|
|
|
$hasDocumentNode = true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($hasDocumentNode) { |
89
|
|
|
if ($hasTextNodeBefore) { |
90
|
|
|
throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed before Node "%s".', \Twig_Node_Text::class, DocumentNode::class)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($hasTextNodeAfter) { |
94
|
|
|
throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed after Node "%s".', \Twig_Node_Text::class, DocumentNode::class)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param BaseNode $node |
101
|
|
|
* |
102
|
|
|
* @throws \Twig_Error_Syntax |
103
|
|
|
*/ |
104
|
|
|
private function checkAllowedParents(BaseNode $node) |
105
|
|
|
{ |
106
|
|
|
$parentName = null; |
107
|
|
|
|
108
|
|
|
// find first parent from this bundle |
109
|
|
|
foreach (array_reverse($this->path) as $className) { |
110
|
|
|
if (strpos($className, 'MewesK\\TwigSpreadsheetBundle\\Twig\\Node\\') === 0) { |
111
|
|
|
$parentName = $className; |
112
|
|
|
break; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// allow no parents (e.g. macros, includes) |
117
|
|
|
if ($parentName === null) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// check if parent is allowed |
122
|
|
|
foreach ($node->getAllowedParents() as $className) { |
123
|
|
|
if ($className === $parentName) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed inside of Node "%s".', get_class($node), $parentName)); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|