File: logic/default.inc.php

Recommend this page to a friend!
  Classes of wim niemans  >  Sirenade PHP Template Compiler  >  logic/default.inc.php  >  Download  
File: logic/default.inc.php
Role: Configuration script
Content type: text/plain
Description: configuration logic
Class: Sirenade PHP Template Compiler
Compile templates to PHP and caches the results
Author: By
Last change: revamped: better performance, and docs added
Date: 2 months ago
Size: 5,436 bytes
 

Contents

Class file image Download
<?php
/**
 * @author wim niemans, Rotterdam, Bonn
 * @license EUPL
 */

/**
 * Basicly, a logic line consists of a head and a body: <head> \s+ <body>
 * The type of logic is identified by <head>, and <body> can be nill (!endif}), a var (myVar of {myVar}, an expression.
 * Most logic is analysed by below functions, that receive the expression (<body>). Some logic just return a PHP statement.
 * ASP logic, however, is treated differently, because it forms a coding style.
 */

return

[
'simple' => // syntax: {!<head> \s+ <body> } or {!<head> \s+ <body> !}
               // $expr : <var> | <scalar> | <var> \s <scalar> | <nill>
  
['if' => function($expr) { $q = '\''; return 'if (!empty($this->renderVar(' .$q.$expr.$q. '))) :';
                                   
array_push($this->logicStack, 'if'); },
   
'elseif' => function($expr) { $q = '\''; return 'elseif (!empty($this->renderVar(' .$q.$expr.$q. '))) ;'; },
   
'while' => function($expr) { $q = '\''; return 'while (!empty($this->renderVar(' .$q.$expr.$q. '))) :';
                                   
array_push($this->logicStack, 'while'); },
   
'loop' => function($expr) { $q = '\''; return 'while ($this->loopCondition(' .$q.$expr.$q. ')) :';
                                   
array_push($this->logicStack, 'while'); },
   
'else' => 'else :',
   
'endif' => function($expr) { return 'endif;'; array_pop($this->logicStack); },
   
'endwhile' => function($expr) { return 'endwhile;'; array_pop($this->logicStack); },
   
'endloop' => function($expr) { return 'endwhile;'; array_pop($this->logicStack); },
   
'break' => function($expr) { return 'break ' . $expr .';'; },
   
'continue' => function($expr) { return 'continue ' . $expr .';'; },
   
'echo' => function($expr) { return 'echo ' . $expr .';'; },
   
'inc' => function($expr) { $q = '\''; return '$this->incrementVar(' .$q.$expr.$q. ');'; },
   
'dec' => function($expr) { $q = '\''; return '$this->decrementVar(' .$q.$expr.$q. ');'; },
   
'set' => function($expr) { $q = '\''; $pieces = explode(' ', $expr, 2);
                                               return
'$this->setVar(' .$q.$pieces[0].$q.', '
                                                                            
. $pieces[1].');'; },
   
'unset' => function($expr) { $q = '\''; return '$this->unsetVar(' .$q.$expr.$q. ');'; },
   ],
'advanced' => // syntay: {!!<head> \s+ <body> !!}
               // $expr : <condition> | <var> \s <scalar> | <var>
  
['if' => function($expr) { $q = '\''; return 'if (' . $expr . ') :'; },
   
'elseif' => function($expr) { $q = '\''; return 'elseif (' . $expr . ') ;'; },
   
'while' => function($expr) { $q = '\''; return 'while (' . $expr . ') :'; },
   
'set' => function($expr) { $q = '\''; $pieces = explode(' ', $expr, 2);
                                               return
'$this->setVar(' .$q.$pieces[0].$q.', '
                                                                        
. $pieces[1] .');'; },
   
'unset' => function($expr) { $q = '\''; return '$this->unsetVar(' .$q.$expr.$q .');'; },
   ],
'scope' => // syntax: {$<head> \s+ <body> } or {@<head> \s+ <body @}
               // $expr : the complete string!!
                                    // syntax: $var p1,'p2',..
  
['global' => function($expr) { $q = '\''; $pieces = explode(' ', $expr, 2);
                                               return
'sprintf($this->renderGlobalVar('.$q.$pieces[0].$q.')'
                                                                                 
.', '. $pieces[1] .');'; },
                                   
// syntax; var method p1,'p2',..
   
'object' => function($expr) { $q = '\''; $pieces = explode(' ', $expr, 3);
                                               return
'$this->renderObject('.$q.$pieces[0].$q.')->'.$pieces[1].'('.$pieces[2].');'; },
   ],
'ASP' => // experimental: not implemented in release 1 !!
                  // for var = scalar step scalar to scalar
                  // for(.*?)\s+(.*?)\s+step\s+(.*?)\s+to\s(.*?)
                  // => for (\1 = \2; \1 += \3; \1 >= \4)
  
['for' => function($expr) { $regex = '~(.*?)=(.*?)\s+STEP\s+(.*?)\s+TO\s(.*?)~i';
                                   
$repl = 'for (\1 = \2; \1 >= \4; \1 += \3;)';
                                   
array_push($this->logicStack, 'for');
                                    return
preg_replace($expr, $regex, $repl); },
                 
// EACH var IN var
                  // EACH\s+(.*?)\s+IN\s+{.*?)
                  // => foreach (\2 as index => \1)
   
'each' => function($expr) { $regex = '~ ~';
                                   
$repl = 'foreach (\2 as $index => \1) :';
                                   
array_push($this->logicStack, 'foreach');
                                    return
preg_replace($expr, $regex, $repl); },
   
'select' => function($expr) { $pieces = explode(' ', $expr, 2);
                                   
array_push($this->logicStack, 'switch');
                                    return
'switch ('. $pieces[1] .') :'; },
   
'case' => function($expr) { return 'case ' .$expr. ' :';},
   ],
   
'next' => 'continue;',
   
'end' => function($expr) { return 'end' . array_pop($this->logicStack); },
'default' => function($line) { return $line; },
];
 
?>


For more information send a message to info at phpclasses dot org.