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