issue 2662 coding convention

git-svn-id: http://xe-core.googlecode.com/svn/branches/maserati@12220 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ovclas 2012-11-14 09:59:39 +00:00
parent f9ca200edf
commit 15fa10dc39
11 changed files with 697 additions and 522 deletions

View file

@ -6,12 +6,10 @@
* @version 0.1
* @remarks It compiles template file by using regular expression into php
* code, and XE caches compiled code for further uses
**/
class TemplateHandler {
*/
class TemplateHandler
{
var $compiled_path = './files/cache/template_compiled/'; ///< path of compiled caches files
var $path = null; ///< target directory
var $filename = null; ///< target filename
var $file = null; ///< target file (fullpath)
@ -19,13 +17,12 @@ class TemplateHandler {
var $web_path = null; ///< tpl file web path
var $compiled_file = null; ///< tpl file web path
var $skipTags = null;
var $handler_mtime = 0;
/**
* constructor
* @return void
**/
*/
function TemplateHandler()
{
// TODO: replace this with static variable in PHP5
@ -39,12 +36,13 @@ class TemplateHandler {
/**
* returns TemplateHandler's singleton object
* @return TemplateHandler instance
**/
*/
function &getInstance()
{
static $oTemplate = null;
if(__DEBUG__==3 ) {
if(__DEBUG__==3 )
{
if(!isset($GLOBALS['__TemplateHandlerCalled__'])) $GLOBALS['__TemplateHandlerCalled__']=1;
else $GLOBALS['__TemplateHandlerCalled__']++;
}
@ -60,7 +58,7 @@ class TemplateHandler {
* @param string $tpl_filename
* @param string $tpl_file
* @return void
**/
*/
function init($tpl_path, $tpl_filename, $tpl_file='')
{
// verify arguments
@ -94,7 +92,8 @@ class TemplateHandler {
* @param string $tpl_file if specified use it as template file's full path
* @return string Returns compiled result in case of success, NULL otherwise
*/
function compile($tpl_path, $tpl_filename, $tpl_file='') {
function compile($tpl_path, $tpl_filename, $tpl_file='')
{
global $__templatehandler_root_tpl;
$buff = '';
@ -109,7 +108,8 @@ class TemplateHandler {
if(!$this->file || !file_exists($this->file)) return "Err : '{$this->file}' template file does not exists.";
// for backward compatibility
if(is_null($__templatehandler_root_tpl)) {
if(is_null($__templatehandler_root_tpl))
{
$__templatehandler_root_tpl = $this->file;
}
@ -120,16 +120,21 @@ class TemplateHandler {
$oCacheHandler = &CacheHandler::getInstance('template');
// get cached buff
if($oCacheHandler->isSupport()){
if($oCacheHandler->isSupport())
{
$cache_key = 'template:'.$this->file;
$buff = $oCacheHandler->get($cache_key, $latest_mtime);
} else {
if(is_readable($this->compiled_file) && filemtime($this->compiled_file)>$latest_mtime && filesize($this->compiled_file)) {
}
else
{
if(is_readable($this->compiled_file) && filemtime($this->compiled_file)>$latest_mtime && filesize($this->compiled_file))
{
$buff = 'file://'.$this->compiled_file;
}
}
if(!$buff) {
if(!$buff)
{
$buff = $this->parse();
if($oCacheHandler->isSupport()) $oCacheHandler->put($cache_key, $buff);
else FileHandler::writeFile($this->compiled_file, $buff);
@ -137,7 +142,8 @@ class TemplateHandler {
$output = $this->_fetch($buff);
if($__templatehandler_root_tpl == $this->file) {
if($__templatehandler_root_tpl == $this->file)
{
$__templatehandler_root_tpl = null;
}
@ -152,12 +158,14 @@ class TemplateHandler {
* @param string $tpl_path path of the directory containing target template file
* @param string $tpl_filename target template file's name
* @return string Returns compiled content in case of success or NULL in case of failure
**/
function compileDirect($tpl_path, $tpl_filename) {
*/
function compileDirect($tpl_path, $tpl_filename)
{
$this->init($tpl_path, $tpl_filename, null);
// if target file does not exist exit
if(!$this->file || !file_exists($this->file)) {
if(!$this->file || !file_exists($this->file))
{
Context::close();
exit("Cannot find the template file: '{$this->file}'");
}
@ -169,9 +177,11 @@ class TemplateHandler {
* parse syntax.
* @param string $buff template file
* @return string compiled result in case of success or NULL in case of error
**/
function parse($buff=null) {
if(is_null($buff)) {
*/
function parse($buff=null)
{
if(is_null($buff))
{
if(!is_readable($this->file)) return;
// read tpl file
@ -179,7 +189,8 @@ class TemplateHandler {
}
// HTML tags to skip
if(is_null($this->skipTags)) {
if(is_null($this->skipTags))
{
$this->skipTags = array('marquee');
}
@ -222,7 +233,7 @@ class TemplateHandler {
* 4. generate return url, return url use in server side validator
* @param array $matches
* @return string
**/
*/
function _compileFormAuthGeneration($matches)
{
@ -234,10 +245,13 @@ class TemplateHandler {
{
$matches[1] = preg_replace('/'.addcslashes($m[0], '?$').'/i', '', $matches[1]);
if (strpos($m[1],'@') !== false){
if (strpos($m[1],'@') !== false)
{
$path = str_replace('@', '', $m[1]);
$path = './files/ruleset/'.$path.'.xml';
}else if(strpos($m[1],'#') !== false){
}
else if(strpos($m[1],'#') !== false)
{
$fileName = str_replace('#', '', $m[1]);
$fileName = str_replace('<?php echo ', '', $fileName);
$fileName = str_replace(' ?>', '', $fileName);
@ -248,7 +262,9 @@ class TemplateHandler {
list($rulsetFile) = explode('.', $fileName);
$autoPath = $module_path.'/ruleset/'.$rulsetFile.'.xml';
$m[1] = $rulsetFile;
}else if(preg_match('@(?:^|\.?/)(modules/[\w-]+)@', $this->path, $mm)) {
}
else if(preg_match('@(?:^|\.?/)(modules/[\w-]+)@', $this->path, $mm))
{
$module_path = $mm[1];
$path = $module_path.'/ruleset/'.$m[1].'.xml';
}
@ -292,21 +308,26 @@ class TemplateHandler {
* fetch using ob_* function
* @param string $buff if buff is not null, eval it instead of including compiled template file
* @return string
**/
function _fetch($buff) {
*/
function _fetch($buff)
{
if(!$buff) return;
$__Context = &$GLOBALS['__Context__'];
$__Context->tpl_path = $this->path;
if($_SESSION['is_logged']) {
if($_SESSION['is_logged'])
{
$__Context->logged_info = Context::get('logged_info');
}
ob_start();
if(substr($buff, 0, 7) == 'file://') {
if(substr($buff, 0, 7) == 'file://')
{
include(substr($buff, 7));
} else {
}
else
{
$eval_str = "?>".$buff;
eval($eval_str);
}
@ -321,7 +342,7 @@ class TemplateHandler {
* @param array $match
*
* @return string changed result
**/
*/
function _replacePath($match)
{
//return origin conde when src value started '${'.
@ -353,7 +374,7 @@ class TemplateHandler {
* replace loop and cond template syntax
* @param string $buff
* @return string changed result
**/
*/
function _parseInline($buff)
{
if(preg_match_all('/<([a-zA-Z]+\d?)(?>(?!<[a-z]+\d?[\s>]).)*?(?:[ \|]cond| loop)="/s', $buff, $match) === false) return $buff;
@ -370,10 +391,12 @@ class TemplateHandler {
// list of self closing tags
$self_closing = array('area'=>1,'base'=>1,'basefont'=>1,'br'=>1,'hr'=>1,'input'=>1,'img'=>1,'link'=>1,'meta'=>1,'param'=>1,'frame'=>1,'col'=>1);
for($idx=1,$node_len=count($nodes); $idx < $node_len; $idx+=2) {
for($idx=1,$node_len=count($nodes); $idx < $node_len; $idx+=2)
{
if(!($node=$nodes[$idx])) continue;
if(preg_match_all('@\s(loop|cond)="([^"]+)"@', $node, $matches)) {
if(preg_match_all('@\s(loop|cond)="([^"]+)"@', $node, $matches))
{
// this tag
$tag = substr($node, 1, strpos($node, ' ')-1);
@ -381,45 +404,61 @@ class TemplateHandler {
$closing = 0;
// process opening tag
foreach($matches[1] as $n=>$stmt) {
foreach($matches[1] as $n=>$stmt)
{
$expr = $matches[2][$n];
$expr = $this->_replaceVar($expr);
$closing++;
switch($stmt) {
case 'cond':
$nodes[$idx-1] .= "<?php if({$expr}){ ?>";
break;
case 'loop':
if(!preg_match('@^(?:(.+?)=>(.+?)(?:,(.+?))?|(.*?;.*?;.*?)|(.+?)\s*=\s*(.+?))$@', $expr, $expr_m)) break;
if($expr_m[1]) {
$expr_m[1] = trim($expr_m[1]);
$expr_m[2] = trim($expr_m[2]);
if($expr_m[3]) $expr_m[2] .= '=>'.trim($expr_m[3]);
$nodes[$idx-1] .= "<?php if({$expr_m[1]}&&count({$expr_m[1]}))foreach({$expr_m[1]} as {$expr_m[2]}){ ?>";
}elseif($expr_m[4]) {
$nodes[$idx-1] .= "<?php for({$expr_m[4]}){ ?>";
}elseif($expr_m[5]) {
$nodes[$idx-1] .= "<?php while({$expr_m[5]}={$expr_m[6]}){ ?>";
}
break;
switch($stmt)
{
case 'cond':
$nodes[$idx-1] .= "<?php if({$expr}){ ?>";
break;
case 'loop':
if(!preg_match('@^(?:(.+?)=>(.+?)(?:,(.+?))?|(.*?;.*?;.*?)|(.+?)\s*=\s*(.+?))$@', $expr, $expr_m)) break;
if($expr_m[1])
{
$expr_m[1] = trim($expr_m[1]);
$expr_m[2] = trim($expr_m[2]);
if($expr_m[3]) $expr_m[2] .= '=>'.trim($expr_m[3]);
$nodes[$idx-1] .= "<?php if({$expr_m[1]}&&count({$expr_m[1]}))foreach({$expr_m[1]} as {$expr_m[2]}){ ?>";
}
elseif($expr_m[4])
{
$nodes[$idx-1] .= "<?php for({$expr_m[4]}){ ?>";
}
elseif($expr_m[5])
{
$nodes[$idx-1] .= "<?php while({$expr_m[5]}={$expr_m[6]}){ ?>";
}
break;
}
}
$node = preg_replace('@\s(loop|cond)="([^"]+)"@', '', $node);
// find closing tag
$close_php = '<?php '.str_repeat('}', $closing).' ?>';
if($node{1} == '!' || substr($node,-2,1) == '/' || isset($self_closing[$tag])) { // self closing tag
// self closing tag
if($node{1} == '!' || substr($node,-2,1) == '/' || isset($self_closing[$tag]))
{
$nodes[$idx+1] = $close_php.$nodes[$idx+1];
} else {
}
else
{
$depth = 1;
for($i=$idx+2; $i < $node_len; $i+=2) {
for($i=$idx+2; $i < $node_len; $i+=2)
{
$nd = $nodes[$i];
if(strpos($nd, $tag) === 1) {
if(strpos($nd, $tag) === 1)
{
$depth++;
} elseif(strpos($nd, '/'.$tag) === 1) {
}
elseif(strpos($nd, '/'.$tag) === 1)
{
$depth--;
if(!$depth) {
if(!$depth)
{
$nodes[$i-1] .= $nodes[$i].$close_php;
$nodes[$i] = '';
break;
@ -429,7 +468,8 @@ class TemplateHandler {
}
}
if(strpos($node, '|cond="') !== false) {
if(strpos($node, '|cond="') !== false)
{
$node = preg_replace('@(\s[-\w:]+(?:="[^"]+?")?)\|cond="(.+?)"@s', '<?php if($2){ ?>$1<?php } ?>', $node);
$node = $this->_replaceVar($node);
}
@ -447,7 +487,7 @@ class TemplateHandler {
* replace php code.
* @param array $m
* @return string changed result
**/
*/
function _parseResource($m)
{
// {@ ... } or {$var} or {func(...)}
@ -456,7 +496,8 @@ class TemplateHandler {
if(preg_match('@^(\w+)\(@', $m[1], $mm) && !function_exists($mm[1])) return $m[0];
$echo = 'echo ';
if($m[1]{0} == '@') {
if($m[1]{0} == '@')
{
$echo = '';
$m[1] = substr($m[1], 1);
}
@ -466,16 +507,22 @@ class TemplateHandler {
if($m[3])
{
$attr = array();
if($m[5]) {
if(preg_match_all('@,(\w+)="([^"]+)"@', $m[6], $mm)) {
foreach($mm[1] as $idx=>$name) {
if($m[5])
{
if(preg_match_all('@,(\w+)="([^"]+)"@', $m[6], $mm))
{
foreach($mm[1] as $idx=>$name)
{
$attr[$name] = $mm[2][$idx];
}
}
$attr['target'] = $m[5];
} else {
}
else
{
if(!preg_match_all('@ (\w+)="([^"]+)"@', $m[6], $mm)) return $m[0];
foreach($mm[1] as $idx=>$name) {
foreach($mm[1] as $idx=>$name)
{
$attr[$name] = $mm[2][$idx];
}
}
@ -492,14 +539,12 @@ class TemplateHandler {
if(!$fileDir) return '';
return "<?php \$__tpl=TemplateHandler::getInstance();echo \$__tpl->compile('{$fileDir}','{$pathinfo['basename']}') ?>";
// <!--%load_js_plugin-->
case 'load_js_plugin':
$plugin = $this->_replaceVar($m[5]);
if(strpos($plugin, '$__Context') === false) $plugin = "'{$plugin}'";
return "<?php Context::loadJavascriptPlugin({$plugin}); ?>";
// <load ...> or <unload ...> or <!--%import ...--> or <!--%unload ...-->
case 'import':
case 'load':
@ -509,9 +554,11 @@ class TemplateHandler {
$doUnload = ($m[3] === 'unload');
$isRemote = !!preg_match('@^https?://@i', $attr['target']);
if(!$isRemote) {
if(!$isRemote)
{
if(!preg_match('@^\.?/@',$attr['target'])) $attr['target'] = './'.$attr['target'];
if(substr($attr['target'], -5) == '/lang') {
if(substr($attr['target'], -5) == '/lang')
{
$pathinfo['dirname'] .= '/lang';
$pathinfo['basename'] = '';
$pathinfo['extension'] = 'xml';
@ -527,24 +574,33 @@ class TemplateHandler {
case 'xml':
if($isRemote || $doUnload) return '';
// language file?
if($pathinfo['basename'] == 'lang.xml' || substr($pathinfo['dirname'],-5) == '/lang') {
if($pathinfo['basename'] == 'lang.xml' || substr($pathinfo['dirname'],-5) == '/lang')
{
$result = "Context::loadLang('{$relativeDir}');";
} else {
}
else
{
$result = "require_once('./classes/xml/XmlJsFilter.class.php');\$__xmlFilter=new XmlJsFilter('{$relativeDir}','{$pathinfo['basename']}');\$__xmlFilter->compile();";
}
break;
case 'js':
if($doUnload) {
if($doUnload)
{
$result = "Context::unloadFile('{$attr['target']}','{$attr['targetie']}');";
} else {
}
else
{
$metafile = $attr['target'];
$result = "\$__tmp=array('{$attr['target']}','{$attr['type']}','{$attr['targetie']}','{$attr['index']}');Context::loadFile(\$__tmp,'{$attr['usecdn']}','{$attr['cdnprefix']}','{$attr['cdnversion']}');unset(\$__tmp);";
}
break;
case 'css':
if($doUnload) {
if($doUnload)
{
$result = "Context::unloadFile('{$attr['target']}','{$attr['targetie']}','{$attr['media']}');";
} else {
}
else
{
$metafile = $attr['target'];
$result = "\$__tmp=array('{$attr['target']}','{$attr['media']}','{$attr['targetie']}','{$attr['index']}');Context::loadFile(\$__tmp,'{$attr['usecdn']}','{$attr['cdnprefix']}','{$attr['cdnversion']}');unset(\$__tmp);";
}
@ -564,13 +620,17 @@ class TemplateHandler {
$m[7] = substr($m[7],1);
if(!$m[7]) return '<?php '.$this->_replaceVar($m[8]).'{ ?>'.$m[9];
if(!preg_match('/^(?:((?:end)?(?:if|switch|for(?:each)?|while)|end)|(else(?:if)?)|(break@)?(case|default)|(break))$/', $m[7], $mm)) return '';
if($mm[1]) {
if($mm[1])
{
if($mm[1]{0} == 'e') return '<?php } ?>'.$m[9];
$precheck = '';
if($mm[1] == 'switch') {
if($mm[1] == 'switch')
{
$m[9] = '';
} elseif($mm[1] == 'foreach') {
}
elseif($mm[1] == 'foreach')
{
$var = preg_replace('/^\s*\(\s*(.+?) .*$/', '$1', $m[8]);
$precheck = "if({$var}&&count({$var}))";
}
@ -581,7 +641,6 @@ class TemplateHandler {
if($mm[5]) return "<?php break; ?>";
return '';
}
return $m[0];
}
@ -589,7 +648,7 @@ class TemplateHandler {
* change relative path
* @param string $path
* @return string
**/
*/
function _getRelativeDir($path)
{
$_path = $path;
@ -598,13 +657,16 @@ class TemplateHandler {
if($path{0} != '/') $path = strtr(realpath($fileDir.'/'.$path),'\\','/');
// for backward compatibility
if(!$path) {
if(!$path)
{
$dirs = explode('/', $fileDir);
$paths = explode('/', $_path);
$idx = array_search($paths[0], $dirs);
if($idx !== false) {
while($dirs[$idx] && $dirs[$idx] === $paths[0]) {
if($idx !== false)
{
while($dirs[$idx] && $dirs[$idx] === $paths[0])
{
array_splice($dirs, $idx, 1);
array_shift($paths);
}
@ -621,11 +683,12 @@ class TemplateHandler {
* replace PHP variables of $ character
* @param string $php
* @return string $__Context->varname
**/
function _replaceVar($php) {
*/
function _replaceVar($php)
{
if(!strlen($php)) return '';
return preg_replace('@(?<!::|\\\\|(?<!eval\()\')\$([a-z]|_[a-z0-9])@i', '\$__Context->$1', $php);
}
}
/* End of File: TemplateHandler.class.php */
/* Location: ./classes/template/TemplateHandler.class.php */