1
|
|
|
<?php |
2
|
|
|
namespace Goetas\Twital; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* |
6
|
|
|
* @author Asmir Mustafic <[email protected]> |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class Compiler |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $lexerOptions; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* @var \DOMDocument |
20
|
|
|
*/ |
21
|
|
|
protected $document; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
* @var Twital |
26
|
|
|
*/ |
27
|
|
|
protected $twital; |
28
|
|
|
|
29
|
461 |
|
public function __construct(Twital $twital, array $lexerOptions = array()) |
30
|
|
|
{ |
31
|
461 |
|
$this->twital = $twital; |
32
|
|
|
|
33
|
461 |
|
$this->lexerOptions = array_merge(array( |
34
|
|
|
'tag_block' => array( |
35
|
461 |
|
'{%', |
36
|
|
|
'%}' |
37
|
461 |
|
), |
38
|
|
|
'tag_variable' => array( |
39
|
461 |
|
'{{', |
40
|
|
|
'}}' |
41
|
461 |
|
) |
42
|
461 |
|
), $lexerOptions); |
43
|
461 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* @return \DOMDocument |
48
|
|
|
*/ |
49
|
|
|
public function getDocument() |
50
|
|
|
{ |
51
|
|
|
return $this->document; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @param string $content |
57
|
|
|
* @return \DOMCDATASection |
58
|
|
|
*/ |
59
|
1 |
|
public function createPrintNode($content) |
60
|
|
|
{ |
61
|
1 |
|
$printPart = $this->getLexerOption('tag_variable'); |
62
|
|
|
|
63
|
1 |
|
return $this->document->createCDATASection("__[__{$printPart[0]} {$content} {$printPart[1]}__]__"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* @param string $content |
69
|
|
|
* @return \DOMCDATASection |
70
|
|
|
*/ |
71
|
101 |
|
public function createControlNode($content) |
72
|
|
|
{ |
73
|
101 |
|
$printPart = $this->getLexerOption('tag_block'); |
74
|
|
|
|
75
|
101 |
|
return $this->document->createCDATASection("__[__{$printPart[0]} " . $content . " {$printPart[1]}__]__"); |
76
|
|
|
} |
77
|
|
|
|
78
|
102 |
|
private function getLexerOption($name) |
79
|
|
|
{ |
80
|
102 |
|
return $this->lexerOptions[$name]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param \DOMDocument $doc |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
461 |
|
public function compile(\DOMDocument $doc) |
88
|
|
|
{ |
89
|
461 |
|
$this->document = $doc; |
90
|
461 |
|
$this->compileChilds($doc); |
91
|
459 |
|
} |
92
|
|
|
|
93
|
461 |
|
public function compileElement(\DOMElement $node) |
94
|
|
|
{ |
95
|
461 |
|
$nodes = $this->twital->getNodes(); |
96
|
461 |
|
if (isset($nodes[$node->namespaceURI][$node->localName])) { |
97
|
69 |
|
$nodes[$node->namespaceURI][$node->localName]->visit($node, $this); |
98
|
461 |
|
} elseif (isset($nodes[$node->namespaceURI]['__base__'])) { |
99
|
|
|
$nodes[$node->namespaceURI]['__base__']->visit($node, $this); |
100
|
|
|
} else { |
101
|
404 |
|
if ($node->namespaceURI === Twital::NS) { |
102
|
|
|
throw new Exception("Can't handle the {$node->namespaceURI}#{$node->localName} node at line " . $node->getLineNo()); |
103
|
|
|
} |
104
|
404 |
|
if ($this->compileAttributes($node)) { |
105
|
402 |
|
$this->compileChilds($node); |
106
|
402 |
|
} |
107
|
|
|
} |
108
|
459 |
|
} |
109
|
|
|
|
110
|
428 |
|
public function compileAttributes(\DOMNode $node) |
111
|
|
|
{ |
112
|
428 |
|
$attributes = $this->twital->getAttributes(); |
113
|
428 |
|
$continueNode = true; |
114
|
428 |
|
foreach (iterator_to_array($node->attributes) as $attr) { |
115
|
337 |
|
if (!$attr->ownerElement) { |
116
|
6 |
|
continue; |
117
|
337 |
|
} elseif (isset($attributes[$attr->namespaceURI][$attr->localName])) { |
118
|
50 |
|
$attPlugin = $attributes[$attr->namespaceURI][$attr->localName]; |
119
|
337 |
|
} elseif (isset($attributes[$attr->namespaceURI]['__base__'])) { |
120
|
3 |
|
$attPlugin = $attributes[$attr->namespaceURI]['__base__']; |
121
|
294 |
|
} elseif ($attr->namespaceURI === Twital::NS) { |
122
|
|
|
throw new Exception("Can't handle the {$attr->namespaceURI}#{$attr->localName} attribute on {$node->namespaceURI}#{$node->localName} node at line " . $attr->getLineNo()); |
123
|
|
|
} else { |
124
|
291 |
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
50 |
|
$return = $attPlugin->visit($attr, $this); |
128
|
48 |
|
if ($return !== null) { |
129
|
5 |
|
$continueNode = $continueNode && !($return & Attribute::STOP_NODE); |
130
|
5 |
|
if ($return & Attribute::STOP_ATTRIBUTE) { |
131
|
5 |
|
break; |
132
|
|
|
} |
133
|
|
|
} |
134
|
426 |
|
} |
135
|
|
|
|
136
|
426 |
|
return $continueNode; |
137
|
|
|
} |
138
|
|
|
|
139
|
461 |
|
public function compileChilds(\DOMNode $node) |
140
|
|
|
{ |
141
|
461 |
|
foreach (iterator_to_array($node->childNodes) as $child) { |
142
|
461 |
|
if ($child instanceof \DOMElement) { |
143
|
461 |
|
$this->compileElement($child); |
144
|
459 |
|
} |
145
|
459 |
|
} |
146
|
459 |
|
} |
147
|
|
|
} |
148
|
|
|
|