Compiler.php
13.1 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?PHP
/**
* Compiler for patTemplate
*
* $Id: Compiler.php,v 1.4 2004/05/14 16:49:34 schst Exp $
*
* WARNING: This is still experimental!
*
* @package patTemplate
* @subpackage Compiler
* @author Stephan Schmidt <schst@php.net>
*/
/**
* Compiler for patTemplate
*
* $Id: Compiler.php,v 1.4 2004/05/14 16:49:34 schst Exp $
*
* WARNING: This is still experimental!
*
* @package patTemplate
* @subpackage Compiler
* @author Stephan Schmidt <schst@php.net>
*
* @todo implement all template types
* @todo implement variable modifiers
* @todo implement getParsedTemplate
* @todo check for existing compiled template
*/
class patTemplate_Compiler extends patTemplate
{
/**
* list of all templates that already have been compiled
*
* @access private
* @var array()
*/
var $_compiledTemplates = array();
/**
* file pointer to the compiled template
*
* @access private
* @var resource
*/
var $_fp;
/**
* constructor
*
* Creates a new patTemplate Compiler
*
* @access public
* @param string type of the templates, either 'html' or 'tex'
*/
function patTemplate_Compiler( $type = 'html' )
{
$GLOBALS['patTemplate_Compiler'] = &$this;
patTemplate::patTemplate( $type );
}
/**
* compile the currently loaded templates
*
* @access public
* @param string name of the input (filename, shm segment, etc.)
*/
function compile( $compileName = null )
{
$this->_varRegexp = '/'.$this->_startTag.'([^a-z:]+)'.$this->_endTag.'/U';
$this->_depRegexp = '/'.$this->_startTag.'TMPL:([^a-z:]+)'.$this->_endTag.'/U';
$compileFolder = $this->getOption( 'compileFolder' );
$compileFile = sprintf( '%s/%s', $compileFolder, $compileName );
$this->_fp = fopen( $compileFile, 'w' );
$this->_addToCode( '<?PHP' );
$this->_addToCode( '/**' );
$this->_addToCode( ' * compiled patTemplate file' );
$this->_addToCode( ' *' );
$this->_addToCode( ' * compiled on '. date( 'Y-m-d H:i:s' ) );
$this->_addToCode( ' */' );
$this->_addToCode( 'class compiledTemplate {' );
foreach( $this->_templates as $template => $spec )
{
$this->compileTemplate( $template );
}
$this->_addToCode( '}' );
$this->_addToCode( '?>' );
fclose( $this->_fp );
include_once $compileFile;
return true;
}
/**
* compile a template
*
* @access public
* @param string name of the template
*/
function compileTemplate( $template )
{
$name = strtolower( $template );
if( !isset( $this->_templates[$template] ) )
{
return patErrorManager::raiseWarning(
PATTEMPLATE_WARNING_NO_TEMPLATE,
"Template '$name' does not exist."
);
}
/**
* check, if the template has been loaded
* and load it if necessary.
*/
if( $this->_templates[$template]['loaded'] !== true )
{
if( $this->_templates[$template]['attributes']['parse'] == 'on' )
{
$result = $this->readTemplatesFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template );
}
else
{
$result = $this->loadTemplateFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], $template );
}
if( patErrorManager::isError( $result ) )
{
return $result;
}
}
$this->_addToCode( '' );
$this->_addToCode( '/**' );
$this->_addToCode( ' * Compiled version of '.$template );
$this->_addToCode( ' *' );
$this->_addToCode( ' * Template type is '.$this->_templates[$template]['attributes']['type'] );
$this->_addToCode( ' */' );
/**
* start the output
*/
$this->_addToCode( 'function '.$template.'()' );
$this->_addToCode( '{' );
$this->_addToCode( '$this->_prepareCompiledTemplate( "'.$template.'" );', 1 );
$this->_addToCode( '$this->prepareTemplate( "'.$template.'" );', 1 );
/**
* attributes
*/
$this->_addToCode( '$this->_templates["'.$template.'"]["attributes"] = unserialize( \''.serialize($this->_templates[$template]['attributes']).'\' );', 1, 'Read the attributes' );
/**
* copyVars
*/
$this->_addToCode( '$this->_templates["'.$template.'"]["copyVars"] = unserialize( \''.serialize($this->_templates[$template]['copyVars']).'\' );', 1, 'Read the copyVars' );
/**
* check visibility
*/
$this->_addToCode( 'if( $this->_templates["'.$template.'"]["attributes"]["visibility"] != "hidden" ) {', 1, 'Check, whether template is hidden' );
/**
* autoloop the template
*/
$this->_addToCode( '$this->_templates["'.$template.'"]["iteration"] = 0;', 2, 'Reset the iteration' );
$this->_addToCode( '$loop = count( $this->_vars["'.$template.'"]["rows"] );', 2, 'Get the amount of loops' );
$this->_addToCode( '$loop = max( $loop, 1 );', 2 );
$this->_addToCode( '$this->_templates["'.$template.'"]["loop"] = $loop;', 2 );
$this->_addToCode( 'for( $i = 0; $i < $loop; $i++ ) {', 2, 'Traverse all variables.' );
/**
* fetch the variables
*/
$this->_addToCode( 'unset( $this->_templates["'.$template.'"]["vars"] );', 3 );
$this->_addToCode( '$this->_fetchVariables("'.$template.'");', 3 );
/**
* different templates have to be compiled differently
*/
switch( $this->_templates[$template]['attributes']['type'] )
{
/**
* modulo template
*/
case 'modulo':
$this->_compileModuloTemplate( $template );
break;
/**
* simple condition template
*/
case 'simplecondition':
$this->_compileSimpleConditionTemplate( $template );
break;
/**
* condition template
*/
case 'condition':
$this->_compileConditionTemplate( $template );
break;
/**
* standard template
*/
default:
$this->_compileStandardTemplate( $template );
break;
}
$this->_addToCode( '$this->_templates["'.$template.'"]["iteration"]++;', 3 );
$this->_addToCode( '}', 2 );
$this->_addToCode( '}', 1 );
$this->_addToCode( '}' );
/**
* remember this template
*/
array_push( $this->_compiledTemplates, $template );
}
/**
* compile a standard template
*
* @access private
* @param string name of the template
*/
function _compileStandardTemplate( $template )
{
$content = $this->_templateToPHP( $this->_templates[$template]['content'], $template );
$this->_addToCode( $content );
return true;
}
/**
* compile a modulo template
*
* A modulo template will be compiled into a switch/case
* statement.
*
* @access private
* @param string name of the template
* @todo check special conditions (__first, __last, __default)
*/
function _compileModuloTemplate( $template )
{
$this->_compileBuiltinConditions( $template );
$this->_addToCode( 'if( !$_displayed ) {', 3, 'Builtin condition has been displayed?' );
/**
* build switch statement
*/
$this->_addToCode( 'switch( ( $this->_templates["'.$template.'"]["iteration"] + 1 ) % '.$this->_templates[$template]['attributes']['modulo'].' ) {', 4 );
foreach( $this->_templates[$template]['subtemplates'] as $condition => $spec )
{
$this->_addToCode( 'case "'.$condition.'":', 5 );
$content = $this->_templateToPHP( $spec['data'], $template );
$this->_addToCode( $content );
$this->_addToCode( 'break;', 6 );
}
$this->_addToCode( '}', 4 );
$this->_addToCode( '}', 3 );
return true;
}
/**
* compile a simpleCondition template
*
* A simpleCondition template will be compiled into an 'if'
* statement.
*
* @access private
* @param string name of the template
*/
function _compileSimpleConditionTemplate( $template )
{
$conditions = array();
foreach( $this->_templates[$template]['attributes']['requiredvars'] as $var )
{
array_push( $conditions, 'isset( $this->_templates["'.$template.'"]["vars"]["'.$var.'"] )' );
}
/**
* build switch statement
*/
$this->_addToCode( 'if( '.implode( ' && ', $conditions ).' ) {', 3, 'Check for required variables' );
$content = $this->_templateToPHP( $this->_templates[$template]['content'], $template );
$this->_addToCode( $content );
$this->_addToCode( '}', 3 );
return true;
}
/**
* compile a condition template
*
* A condition template will be compiled into an 'switch/case'
* statement.
*
* @access private
* @param string name of the template
*/
function _compileConditionTemplate( $template )
{
/**
* __first, __last
*/
$this->_compileBuiltinConditions( $template );
$this->_addToCode( 'if( !$_displayed ) {', 3, 'Builtin condition has been displayed?' );
/**
* build switch statement
*/
$this->_addToCode( 'switch( $this->_templates["'.$template.'"]["vars"]["'.$this->_templates[$template]["attributes"]["conditionvar"].'"] ) {', 4 );
foreach( $this->_templates[$template]['subtemplates'] as $condition => $spec )
{
if( $condition == '__default' )
{
$this->_addToCode( 'default:', 5 );
}
else
{
$this->_addToCode( 'case "'.$condition.'":', 5 );
}
$content = $this->_templateToPHP( $spec['data'], $template );
$this->_addToCode( $content );
$this->_addToCode( 'break;', 6 );
}
$this->_addToCode( '}', 4 );
$this->_addToCode( '}', 3 );
return true;
}
/**
* compile built-in conditions
*
* This will create the neccessary PHP code for:
* - __first
* - __last
*
* @access private
* @param string template name
*/
function _compileBuiltinConditions( $template )
{
$this->_addToCode( '$_displayed = false;', 3 );
if( isset( $this->_templates[$template]['subtemplates']['__first'] ) )
{
$this->_addToCode( 'if( $this->_templates["'.$template.'"]["iteration"] == 0 ) {', 3, 'Check for first entry' );
$content = $this->_templateToPHP( $this->_templates[$template]['subtemplates']['__first']['data'], $template );
$this->_addToCode( $content );
$this->_addToCode( '$_displayed = true;', 4 );
$this->_addToCode( '}', 3 );
}
if( isset( $this->_templates[$template]['subtemplates']['__last'] ) )
{
$this->_addToCode( 'if( $this->_templates["'.$template.'"]["iteration"] == ($this->_templates["'.$template.'"]["loop"]-1) ) {', 3, 'Check for last entry' );
$content = $this->_templateToPHP( $this->_templates[$template]['subtemplates']['__last']['data'], $template );
$this->_addToCode( $content );
$this->_addToCode( '$_displayed = true;', 4 );
$this->_addToCode( '}', 3 );
}
}
/**
* build PHP code from a template
*
* This will replace the variables in a template with
* PHP Code.
*
* @access private
* @param string template content
* @param string name of the template
* @return string PHP code
*/
function _templateToPHP( $content, $template )
{
$content = preg_replace( $this->_varRegexp, '<?PHP echo $this->_getVar( "'.$template.'", "$1"); ?>', $content );
$content = preg_replace( $this->_depRegexp, '<?PHP compiledTemplate::$1(); ?>', $content );
$content = '?>'.$content.'<?PHP';
return $content;
}
/**
* display the compiled template
*
* This is a replacement for patTemplate::displayParsedTemplate.
*
* @access public
* @param string name of the template to display
*/
function displayParsedTemplate( $name = null )
{
if( is_null( $name ) )
$name = $this->_root;
$name = strtolower( $name );
if( !is_callable( 'compiledTemplate', $name ) )
{
die( 'Unknown template' );
}
compiledTemplate::$name();
}
/**
* add a line to the compiled code
*
* @access public
* @param string line to add
* @param integer indentation
* @return void
*/
function _addToCode( $line, $indent = 0, $comment = null )
{
if( !is_null( $comment ) )
{
fputs( $this->_fp, "\n" );
if( $indent > 0 )
fputs( $this->_fp, str_repeat( "\t", $indent ) );
fputs( $this->_fp, "/* $comment */\n" );
}
if( $indent > 0 )
fputs( $this->_fp, str_repeat( "\t", $indent ) );
fputs( $this->_fp, $line."\n" );
}
/**
* function, used by the compiler to get a value of a variable
*
* Checks, whether the value is locally or globally set
*
* @access private
* @param string template
* @param string variable name
*
* @todo check for 'unusedvars' attribute
*/
function _getVar( $template, $varname )
{
if( isset( $this->_templates[$template]['vars'][$varname] ) )
return $this->_templates[$template]['vars'][$varname];
if( isset( $this->_globals[$this->_startTag.$varname.$this->_endTag] ) )
return $this->_globals[$this->_startTag.$varname.$this->_endTag];
return '';
}
/**
* prepare a template for the compiler
*
* @access private
* @param string template name
*/
function _prepareCompiledTemplate( $template )
{
$this->_templates[$template] = array(
'attributes' => array(),
'copyVars' => array(),
);
}
}
?>