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