00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 class PHPTAL_Generator
00024 {
00025 var
$_temp_id = 0;
00026 var
$_str_buffer =
"";
00027 var
$_tab =
"";
00028 var
$_tab_save =
"";
00029 var
$_code =
"";
00030 var
$_closed =
false;
00031 var
$_fname;
00032 var
$_funcName;
00033
00034 var
$_macros = array();
00035 var
$_stacks = array();
00036 var
$_current_macro =
false;
00037
00038 var
$_gettext_required =
false;
00039
00040 var
$_headers =
false;
00041
00042 function
PHPTAL_Generator($fname, $func_name)
00043 {
00044 $this->_fname = $fname;
00045 $this->_funcName = $func_name;
00046 $this->
appendln('<?php');
00047 $this->
doComment(str_pad(
"", 78,
"-"));
00048 $this->
doComment(
"This code was generate by PHPTAL based on the template source file :");
00049 $this->
doComment(
"$fname");
00050 $this->
doComment(str_pad(
"", 78,
"-"));
00051 $this->
appendln();
00052 $this->
appendln('function ', $func_name, '(&$__tpl__)');
00053 $this->
appendln(
'{');
00054 $this->
tabInc();
00055 $this->
appendln('$__ctx__ =& $__tpl__->getContext();');
00056 $this->
appendln('$__out__ =
new PHPTAL_OutputControl($__ctx__, $__tpl__->getEncoding());');
00057 $this->
appendln('$__ctx__->set(
"repeat", array());');
00058 }
00059
00060 function
setHeaders($h)
00061 {
00062 $this->_headers = $h;
00063
if ($this->_headers) {
00064 $this->
doHeadersPrint();
00065 }
00066 }
00067
00068 function
requireGettext()
00069 {
00070 $this->_gettext_required =
true;
00071 }
00072
00073 function
getCode()
00074 {
00075
if (!$this->_closed) {
00076 $this->
_flushOutput();
00077 $this->
appendln('
return $__out__->toString();');
00078 $this->
endBlock();
00079
00080 foreach ($this->_macros as $
name=>$code) {
00081 $this->_code .= $code;
00082 }
00083
if ($this->_gettext_required) {
00084 $this->_code = preg_replace('/^<\?php/sm',
00085 '<?php require_once
"' . PT_IP . '/GetText.php";',
00086 $this->_code,
00087 1);
00088 }
00089 $this->
append('?>');
00090 $this->_closed =
true;
00091 }
00092
return $this->_code;
00093 }
00094
00095 function
execute($code)
00096 {
00097 $this->
_flushOutput();
00098 $this->
appendln(trim($code),
";");
00099 }
00100
00101 function
doComment($str)
00102 {
00103 $this->
_flushOutput();
00104 $this->
appendln('
00105 }
00106
00107 function
setSource($tagName, $line)
00108 {
00109 $this->doComment('TAG ' . $tagName . ' AT LINE ' . $line);
00110 $this->
appendln('$_src_tag =
"'. $tagName. '"; ', '$_src_line = '. $line.
';');
00111 }
00112
00113
00114 function
doDo()
00115 {
00116 $this->
_flushOutput();
00117 $this->
appendln(
"do {");
00118 $this->
tabInc();
00119 }
00120
00121 function
doEndDoWhile($condition)
00122 {
00123 $this->
tabDec();
00124 $this->
appendln(
"} while($condition);");
00125 }
00126
00127 function
doWhile($condition)
00128 {
00129 $this->
_flushOutput();
00130 $this->
appendln(
"while ($condition) {");
00131 $this->
tabInc();
00132 }
00133
00134
00135 function
doIf($condition)
00136 {
00137 $this->
_flushOutput();
00138 $this->
appendln(
"if ($condition) {");
00139 $this->
tabInc();
00140 }
00141
00142 function
doElseIf($condition)
00143 {
00144 $this->_str_buffer =
"";
00145 $this->
endBlock();
00146 $this->
appendln(
"else if ($condition) {");
00147 $this->
tabInc();
00148 }
00149
00150 function
doElse()
00151 {
00152 $this->
endBlock();
00153 $this->
appendln(
"else {");
00154 $this->
tabInc();
00155 }
00156
00157
00158 function
doMacroDeclare($name)
00159 {
00160 $this->
_flushOutput();
00161 $this->_stacks[] = $this->_code;
00162 $this->_code =
"";
00163 $this->_current_macro = $name;
00164 $this->_tab_save = $this->_tab;
00165 $this->_tab =
"";
00166 $this->
appendln('function ', $this->_funcName,
'_',$
name, '(&$__tpl__)');
00167 $this->
appendln(
'{');
00168 $this->
tabInc();
00169 $this->
appendln('$__ctx__ =& $__tpl__->getContext();');
00170 $this->
appendln('$__out__ =
new PHPTAL_OutputControl($__ctx__, $__tpl__->getEncoding());');
00171
if ($this->_headers) {
00172 $this->
doHeadersPrint();
00173 }
00174 }
00175
00176 function
doHeadersPrint()
00177 {
00178 $this->
_flushOutput();
00179 $this->doIf('! $__tpl__->_headers');
00180 $str = str_replace(
"'",
"\\'", $this->_headers);
00181 $str =
"'" . $str .
"'";
00182 $str =
PHPTAL_ES_path_in_string($str,
"'");
00183 $this->
appendln('$__tpl__->_headers = ' . $str .
';');
00184 $this->endBlock();
00185 }
00186
00187 function
doMacroEnd()
00188 {
00189 $this->
_flushOutput();
00190 $this->
appendln('
return $__out__->toString();');
00191 $this->
endBlock();
00192 $this->_macros[$this->_current_macro] = $this->_code;
00193 $this->_code = array_pop($this->_stacks);
00194 $this->_current_macro =
false;
00195 $this->_tab = $this->_tab_save;
00196 }
00197
00198 function
doAffectResult($dest, $code)
00199 {
00200
if ($dest[0] !=
'$') { $dest =
'$'.$dest; }
00201
00202 $this->
appendln(
"$dest = $code;");
00203 }
00204
00205
00206 function
doPrintString()
00207 {
00208
$args = func_get_args();
00209 $this->_str_buffer .= join(
"", $args);
00210
00211 }
00212
00213 function
doPrintVar($var, $structure=
false)
00214 {
00215 $this->
_flushOutput();
00216
if ($var[0] !=
'$') {
00217 $var =
'$' . $var;
00218 }
00219
if ($structure) {
00220 $this->
appendln('$__out__->writeStructure(', $var, ');');
00221 }
else {
00222 $this->
appendln('$__out__->write(', $var, ');');
00223 }
00224 }
00225
00226 function
doPrintRes($code, $structure=
false)
00227 {
00228 $this->
_flushOutput();
00229
if ($structure) {
00230 $this->
appendln('$__out__->writeStructure(', $code, ');');
00231 }
else {
00232 $this->
appendln('$__out__->write(', $code, ');');
00233 }
00234 }
00235
00236 function
doPrintContext($path, $structure=
false)
00237 {
00238 $code = sprintf('$__ctx__->get(\'%s\
')', $
path);
00239 $this->doPrintRes($code, $structure);
00240 }
00241
00242
00243 function
doOBStart()
00244 {
00245 $this->
_flushOutput();
00246 $this->
appendln('$__out__->pushBuffer();');
00247 }
00248
00249 function
doOBEnd($dest)
00250 {
00251 $this->
_flushOutput();
00252
00253 $this->
appendln($dest, ' =& $__out__->popBuffer();');
00254 }
00255
00256 function
doOBClean()
00257 {
00258 $this->
_flushOutput();
00259 $this->
appendln('$__out__->popBuffer();');
00260 }
00261
00262 function
doOBPrint()
00263 {
00264 $this->
_flushOutput();
00265 $this->
appendln('$__out__->writeStructure($__out__->popBuffer());');
00266 }
00267
00268 function
doOBEndInContext($dest)
00269 {
00270 $this->
doContextSet($dest, '$__out__->popBuffer()');
00271 }
00272
00273
00274 function
doReference($dest, $source)
00275 {
00276 $this->
_flushOutput();
00277
if ($dest[0] !=
'$') { $dest =
'$'.$dest; }
00278
if ($source[0] !=
'$') { $source =
'$'.$source; }
00279
00280 $this->
appendln(
"$dest =& $source;");
00281 }
00282
00283 function
doUnset($var)
00284 {
00285 $this->
appendln(
"unset($var);");
00286 }
00287
00288
00289 function
newTemporaryVar()
00290 {
00291
return '$temp_' . $this->_temp_id++;
00292 }
00293
00294 function
releaseTemporaryVar($name)
00295 {
00296 $this->doUnset($
name);
00297 }
00298
00299
00300
00301 function
doContextSet($out, $code)
00302 {
00303 $this->
_flushOutput();
00304
if ($out[0] !=
'$')
00305 { $out =
'"'.$out.
'"'; }
00306
00307 $this->
appendln(
"\$__ctx__->setRef($out, $code);");
00308 }
00309
00310 function
doContextGet($out, $path)
00311 {
00312 $this->
_flushOutput();
00313
if ($out[0] !=
'$')
00314 { $out =
'$' . $out; }
00315
00316 $this->
appendln(
"$out =& \$__ctx__->get(\"$path\");");
00317 }
00318
00319 function
endBlock()
00320 {
00321 $this->
tabDec();
00322 $this->
appendln(
'}');
00323 }
00324
00325 function
tabInc()
00326 {
00327 $this->
_flushOutput();
00328 $this->_tab .=
" ";
00329 }
00330
00331 function
tabDec()
00332 {
00333 $this->
_flushOutput();
00334 $this->_tab = substr($this->_tab, 4);
00335 }
00336
00337 function
appendln()
00338 {
00339
$args = func_get_args();
00340 $str = join(
"", $args);
00341 $this->_code .= $this->_tab . $str .
"\n";
00342 }
00343
00344 function
append()
00345 {
00346
$args = func_get_args();
00347 $str = join(
"", $args);
00348 $this->_code .= $this->_tab . $str;
00349 }
00350
00351 function
_flushOutput()
00352 {
00353
if ($this->_str_buffer ==
"")
return;
00354 $this->_str_buffer = str_replace(
"'",
"\\'", $this->_str_buffer);
00355 $this->_str_buffer =
"'" . $this->_str_buffer .
"'";
00356 $this->_str_buffer =
PHPTAL_ES_path_in_string($this->_str_buffer,
"'");
00357 $this->
appendln('$__out__->writeStructure(', $this->_str_buffer, ');');
00358 $this->_str_buffer =
"";
00359 }
00360 }
00361
00362 ?>