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
|
486 |
|
public function __construct(Twital $twital, array $lexerOptions = array()) |
30
|
|
|
{ |
31
|
486 |
|
$this->twital = $twital; |
32
|
|
|
|
33
|
486 |
|
$this->lexerOptions = array_merge(array( |
34
|
|
|
'tag_block' => array( |
35
|
486 |
|
'{%', |
36
|
|
|
'%}' |
37
|
486 |
|
), |
38
|
|
|
'tag_variable' => array( |
39
|
486 |
|
'{{', |
40
|
|
|
'}}' |
41
|
486 |
|
) |
42
|
486 |
|
), $lexerOptions); |
43
|
486 |
|
} |
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
|
7 |
|
public function createPrintNode($content) |
60
|
|
|
{ |
61
|
7 |
|
$printPart = $this->getLexerOption('tag_variable'); |
62
|
|
|
|
63
|
7 |
|
return $this->document->createCDATASection("__[__{$printPart[0]} {$content} {$printPart[1]}__]__"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* @param string $content |
69
|
|
|
* @return \DOMCDATASection |
70
|
|
|
*/ |
71
|
111 |
|
public function createControlNode($content) |
72
|
|
|
{ |
73
|
111 |
|
$printPart = $this->getLexerOption('tag_block'); |
74
|
|
|
|
75
|
111 |
|
return $this->document->createCDATASection("__[__{$printPart[0]} " . $content . " {$printPart[1]}__]__"); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param \DOMDocument $doc |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
486 |
|
public function compile(\DOMDocument $doc) |
83
|
|
|
{ |
84
|
486 |
|
$this->document = $doc; |
85
|
486 |
|
$this->compileChilds($doc); |
86
|
484 |
|
} |
87
|
|
|
|
88
|
483 |
|
public function compileElement(\DOMElement $node) |
89
|
|
|
{ |
90
|
483 |
|
$nodes = $this->twital->getNodes(); |
91
|
483 |
|
if (isset($nodes[$node->namespaceURI][$node->localName])) { |
92
|
81 |
|
$nodes[$node->namespaceURI][$node->localName]->visit($node, $this); |
93
|
483 |
|
} elseif (isset($nodes[$node->namespaceURI]['__base__'])) { |
94
|
|
|
$nodes[$node->namespaceURI]['__base__']->visit($node, $this); |
95
|
|
|
} else { |
96
|
426 |
|
if ($node->namespaceURI === Twital::NS) { |
97
|
|
|
throw new Exception("Can't handle the {$node->namespaceURI}#{$node->localName} node at line " . $node->getLineNo()); |
98
|
|
|
} |
99
|
426 |
|
if ($this->compileAttributes($node)) { |
100
|
421 |
|
$this->compileChilds($node); |
101
|
421 |
|
} |
102
|
|
|
} |
103
|
481 |
|
} |
104
|
|
|
|
105
|
450 |
|
public function compileAttributes(\DOMNode $node) |
106
|
|
|
{ |
107
|
450 |
|
$attributes = $this->twital->getAttributes(); |
108
|
450 |
|
$continueNode = true; |
109
|
450 |
|
foreach (iterator_to_array($node->attributes) as $attr) { |
110
|
356 |
|
if (!$attr->ownerElement) { |
111
|
6 |
|
continue; |
112
|
356 |
|
} elseif (isset($attributes[$attr->namespaceURI][$attr->localName])) { |
113
|
66 |
|
$attPlugin = $attributes[$attr->namespaceURI][$attr->localName]; |
114
|
356 |
|
} elseif (isset($attributes[$attr->namespaceURI]['__base__'])) { |
115
|
3 |
|
$attPlugin = $attributes[$attr->namespaceURI]['__base__']; |
116
|
300 |
|
} elseif ($attr->namespaceURI === Twital::NS) { |
117
|
|
|
throw new Exception("Can't handle the {$attr->namespaceURI}#{$attr->localName} attribute on {$node->namespaceURI}#{$node->localName} node at line " . $attr->getLineNo()); |
118
|
|
|
} else { |
119
|
297 |
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
66 |
|
$return = $attPlugin->visit($attr, $this); |
123
|
64 |
|
if ($return !== null) { |
124
|
8 |
|
$continueNode = $continueNode && !($return & Attribute::STOP_NODE); |
125
|
8 |
|
if ($return & Attribute::STOP_ATTRIBUTE) { |
126
|
5 |
|
break; |
127
|
|
|
} |
128
|
3 |
|
} |
129
|
448 |
|
} |
130
|
|
|
|
131
|
448 |
|
return $continueNode; |
132
|
|
|
} |
133
|
|
|
|
134
|
486 |
|
public function compileChilds(\DOMNode $node) |
135
|
|
|
{ |
136
|
486 |
|
foreach (iterator_to_array($node->childNodes) as $child) { |
137
|
483 |
|
if ($child instanceof \DOMElement) { |
138
|
483 |
|
$this->compileElement($child); |
139
|
481 |
|
} |
140
|
484 |
|
} |
141
|
484 |
|
} |
142
|
|
|
|
143
|
118 |
|
private function getLexerOption($name) |
144
|
|
|
{ |
145
|
118 |
|
return $this->lexerOptions[$name]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|