forked from zikula/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twigify_lib.php
292 lines (238 loc) · 8.26 KB
/
twigify_lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
class Twiggifier
{
private $template;
private $matches;
public function __construct($template, $matches)
{
$this->template = $template;
$this->matches = $matches;
}
public function stripString($string)
{
foreach ($this->matches[0] as $key => $match) {
$match = str_replace($string, '', $match);
$this->template = str_replace($this->matches[0][$key], $match, $this->template);
}
}
public function replaceString($search, $replace)
{
foreach ($this->matches[0] as $key => $match) {
$match = str_replace($search, $replace, $match);
$this->template = str_replace($this->matches[0][$key], $match, $this->template);
}
}
private function comment($match, $key)
{
$string = "{# $match #}";
$this->template = str_replace($this->matches[0][$key], $string, $this->template);
}
// public function __call($method, $args)
// {
// $this->comment($args[0], $args[1]);
// }
public function convertAjaxheader($match, $key)
{
$this->comment($match, $key);
}
public function convertMath($match, $key)
{
$this->comment($match, $key);
}
public function convertButton($match, $key)
{
$this->comment($match, $key);
}
public function convertImg($match, $key)
{
$this->comment($match, $key);
}
public function convertIcon($match, $key)
{
$this->comment($match, $key);
}
public function convertInclude($match, $key)
{
preg_match('/^include file=(?:"{0,1}|\'{0,1})(.+?)(?:"{0,1}|\'{0,1})\s{0,}$/', $match, $matches);
$fileName = str_replace('"', '', str_replace("'", '', $matches[1]));
$string = "{% include 'module:controller:$fileName' %}{# should be in the form 'module:controller:template.html.twig' #}";
$this->template = str_replace($this->matches[0][$key], $string, $this->template);
}
public function convertForeach($match, $key)
{
preg_match('/^foreach (.+?)\s{0,}$/', $match, $matches);
$name = '';
if (preg_match('/name=(?:"{0,1}|\'{0,1})(.+?)(?:"{0,1}|\'{0,1})\s|$/', $matches[1], $nameMatches)) {
$name = isset($nameMatches[1]) ? $nameMatches[1] : $nameMatches[0];
}
$from = ''; // data
if (preg_match('/from=(?:"{0,1}|\'{0,1})(.+?)(?:"{0,1}|\'{0,1})(?:\s|$)/', $matches[1], $fromMatches)) {
$from = $fromMatches[1];
}
$item = ''; // value
if (preg_match('/item=(?:"{0,1}|\'{0,1})(.+?)(?:"{0,1}|\'{0,1})(?:\s|$)/', $matches[1], $itemMatches)) {
$item = $itemMatches[1];
}
$skey = ''; // key
if (preg_match('/key=(?:"{0,1}|\'{0,1})(.+?)(?:"{0,1}|\'{0,1})(?:\s|$)/', $matches[1], $skeyMatches)) {
$skey = $skeyMatches[1];
}
if ($skey) {
$string = "{% for $skey, $item in $from %}";
} elseif ($name) {
$string = "{% for $name, $item in $from %}"; // todo - check this, is 'name' really 'key'?
} else {
$string = "{% for $item in $from %}";
}
$string = str_replace("'", '', $string);
$this->template = str_replace($this->matches[0][$key], $string, $this->template);
}
public function convertCycle($match, $key)
{
preg_match('/^cycle (.+?)\s{0,}$/', $match, $matches);
$values = ''; // value
if (preg_match('/values=(?:"{0,1}|\'{0,1})(.+?)(?:"{0,1}|\'{0,1})(?:\s|$)/', $matches[1], $itemMatches)) {
$values = $itemMatches[1];
}
$values = str_replace("'", '', $values);
$parts = explode(',', $values);
$string = '[';
foreach ($parts as $skey => $value) {
$string .= "$value";
if ($skey < count($parts)-1) {
$string .= ',';
}
}
$string = "{{ cycle($string]), i }}";
$this->template = str_replace($this->matches[0][$key], $string, $this->template);
}
public function convertIf($match, $key, $command = 'if')
{
// {if ....}
if (!preg_match('/^if (.+?)\s{0,}$/', $match, $matches)) {
return;
}
$content = '';
$array = explode(' ', $matches[1]);
foreach ($array as $element) {
switch ($element) {
case 'eq':
$content .= '==';
break;
case 'neq':
$content .= '!=';
break;
default:
$content .= $element;
break;
}
$content .= ' ';
}
$content = rtrim($content, ' ');
$string = "{% $command ".str_replace('$', '', $content)." %}";
$this->template = str_replace($this->matches[0][$key], $string, $this->template);
}
public function convertElseif($match, $key)
{
$this->convertIf($match, $key, 'elseif');
}
public function convertModurl($match, $key)
{
return; //todo
// {modurl ....}
if (!preg_match('/^modurl (.+?)\s{0,}$/', $match, $matches)) {
return;
}
$content = '';
$array = explode(' ', $matches[1]);
foreach ($array as $element) {
switch ($element) {
case 'modname':
$content .= '';
break;
case 'type':
$content .= '';
break;
case 'func':
$content .= '';
break;
default:
$content .= $element;
break;
}
$content .= ' ';
}
$content = rtrim($content, ' ');
$string = "{{ path('".str_replace('$', '', $content)."') }}";
$this->template = str_replace($this->matches[0][$key], $string, $this->template);
}
public function convertPagesetvar($match, $key, $command = 'pagesetvar')
{
preg_match('/^'.$command.' (.+?)\s{0,}$/', $match, $matches);
$name = '';
if (preg_match('/name=(?:"{0,1}|\'{0,1})(.+?)(?:"{0,1}|\'{0,1})\s|$/', $matches[1], $nameMatches)) {
$name = $nameMatches[1];
}
$value = '';
if (preg_match('/value=(?:"{0,1}|\'{0,1})(.+?)(?:"{0,1}|\'{0,1})(?:\s|$)/', $matches[1], $valueMatches)) {
$value = $valueMatches[1];
}
$string = "{{ $command('$name', '$value') }}";
$this->template = str_replace($this->matches[0][$key], $string, $this->template);
}
public function convertPageaddvar($match, $key)
{
$this->convertPagesetvar($match, $key, 'pageaddvar');
}
public function convertVariable($key)
{
$match = str_replace('$', '', $this->matches[1][$key]);
$match = "{{ $match }}";
$this->template = str_replace($this->matches[0][$key], $match, $this->template);
}
public function convertGt($match, $key)
{
if (preg_match('/^gt text="(.+?)"\s{0,}$/', $match, $matches)) {
// gt text=""
$string = "{{ __(\"{$matches[1]}\") }}";
$this->template = str_replace($this->matches[0][$key], $string, $this->template);
return;
} elseif (preg_match('/^gt text=\'(.+?)\'\s{0,}$/', $match, $matches)) {
// gt text=''
$string = "{{ __('{$matches[1]}') }}";
$this->template = str_replace($this->matches[0][$key], $string, $this->template);
return;
} elseif (preg_match('/^gt text="(.+?)"\s{1,}tag\d{1}="(.+?)"\s{0,}$/', $match, $matches)) {
// gt text="" tag0=...
// todo
}
}
/**
* Resolves the smarty call
*
* @param $match
*/
public function resolve($match)
{
preg_match('/(.+?)(?:\s{1,}|$)/', $match, $matches);
return $matches[1];
}
public function setMatches($matches)
{
$this->matches = $matches;
return $this;
}
public function getMatches()
{
return $this->matches;
}
public function setTemplate($template)
{
$this->template = $template;
return $this;
}
public function getTemplate()
{
return $this->template;
}
}