formatting RainTPL class

This commit is contained in:
El RIDO 2015-09-05 01:55:19 +02:00
parent 3edeefd799
commit 28776ac178

View file

@ -154,7 +154,7 @@ class RainTPL{
* *
* @var array the file * @var array the file
*/ */
protected static $config_name_sum = array(); protected static $config_name_sum = array();
// ------------------------- // -------------------------
@ -207,41 +207,41 @@ class RainTPL{
} }
// Cache is off and, return_string is false // Cache is off and, return_string is false
// Rain just echo the template // Rain just echo the template
if( !$this->cache && !$return_string ){ if( !$this->cache && !$return_string ){
extract( $this->var ); extract( $this->var );
include $this->tpl['compiled_filename']; include $this->tpl['compiled_filename'];
unset( $this->tpl ); unset( $this->tpl );
} }
// cache or return_string are enabled // cache or return_string are enabled
// rain get the output buffer to save the output in the cache or to return it as string // rain get the output buffer to save the output in the cache or to return it as string
else{ else{
//---------------------- //----------------------
// get the output buffer // get the output buffer
//---------------------- //----------------------
ob_start(); ob_start();
extract( $this->var ); extract( $this->var );
include $this->tpl['compiled_filename']; include $this->tpl['compiled_filename'];
$raintpl_contents = ob_get_clean(); $raintpl_contents = ob_get_clean();
//---------------------- //----------------------
// save the output in the cache // save the output in the cache
if( $this->cache ) if( $this->cache )
file_put_contents( $this->tpl['cache_filename'], "<?php if(!class_exists('raintpl')){exit;}?>" . $raintpl_contents ); file_put_contents( $this->tpl['cache_filename'], "<?php if(!class_exists('raintpl')){exit;}?>" . $raintpl_contents );
// free memory // free memory
unset( $this->tpl ); unset( $this->tpl );
// return or print the template // return or print the template
if( $return_string ) return $raintpl_contents; else echo $raintpl_contents; if( $return_string ) return $raintpl_contents; else echo $raintpl_contents;
} }
} }
@ -258,15 +258,15 @@ class RainTPL{
*/ */
public function cache( $tpl_name, $expire_time = self::CACHE_EXPIRE_TIME, $cache_id = '' ){ public function cache( $tpl_name, $expire_time = self::CACHE_EXPIRE_TIME, $cache_id = '' ){
// set the cache_id // set the cache_id
$this->cache_id = $cache_id; $this->cache_id = $cache_id;
if( !$this->check_template( $tpl_name ) && file_exists( $this->tpl['cache_filename'] ) && ( time() - filemtime( $this->tpl['cache_filename'] ) < $expire_time ) ) if( !$this->check_template( $tpl_name ) && file_exists( $this->tpl['cache_filename'] ) && ( time() - filemtime( $this->tpl['cache_filename'] ) < $expire_time ) )
return substr( file_get_contents( $this->tpl['cache_filename'] ), 43 ); return substr( file_get_contents( $this->tpl['cache_filename'] ), 43 );
else{ else{
//delete the cache of the selected template //delete the cache of the selected template
if (file_exists($this->tpl['cache_filename'])) if (file_exists($this->tpl['cache_filename']))
unlink($this->tpl['cache_filename'] ); unlink($this->tpl['cache_filename'] );
$this->cache = true; $this->cache = true;
} }
} }
@ -287,8 +287,8 @@ class RainTPL{
self::configure( $key, $value ); self::configure( $key, $value );
else if( property_exists( __CLASS__, $setting ) ){ else if( property_exists( __CLASS__, $setting ) ){
self::$$setting = $value; self::$$setting = $value;
self::$config_name_sum[ $setting ] = $value; // take trace of all config self::$config_name_sum[ $setting ] = $value; // take trace of all config
} }
} }
@ -305,13 +305,13 @@ class RainTPL{
if( !isset($this->tpl['checked']) ){ if( !isset($this->tpl['checked']) ){
$tpl_basename = basename( $tpl_name ); // template basename $tpl_basename = basename( $tpl_name ); // template basename
$tpl_basedir = strpos($tpl_name,"/") ? dirname($tpl_name) . '/' : null; // template basedirectory $tpl_basedir = strpos($tpl_name,"/") ? dirname($tpl_name) . '/' : null; // template basedirectory
$tpl_dir = PATH . self::$tpl_dir . $tpl_basedir; // template directory $tpl_dir = PATH . self::$tpl_dir . $tpl_basedir; // template directory
$this->tpl['tpl_filename'] = $tpl_dir . $tpl_basename . '.' . self::$tpl_ext; // template filename $this->tpl['tpl_filename'] = $tpl_dir . $tpl_basename . '.' . self::$tpl_ext; // template filename
$temp_compiled_filename = PATH . self::$cache_dir . $tpl_basename . "." . md5( $tpl_dir . serialize(self::$config_name_sum)); $temp_compiled_filename = PATH . self::$cache_dir . $tpl_basename . "." . md5( $tpl_dir . serialize(self::$config_name_sum));
$this->tpl['compiled_filename'] = $temp_compiled_filename . '.rtpl.php'; // cache filename $this->tpl['compiled_filename'] = $temp_compiled_filename . '.rtpl.php'; // cache filename
$this->tpl['cache_filename'] = $temp_compiled_filename . '.s_' . $this->cache_id . '.rtpl.php'; // static cache filename $this->tpl['cache_filename'] = $temp_compiled_filename . '.s_' . $this->cache_id . '.rtpl.php'; // static cache filename
// if the template doesn't exsist throw an error // if the template doesn't exsist throw an error
if( self::$check_template_update && !file_exists( $this->tpl['tpl_filename'] ) ){ if( self::$check_template_update && !file_exists( $this->tpl['tpl_filename'] ) ){
@ -329,6 +329,7 @@ class RainTPL{
} }
/** /**
* execute stripslaches() on the xml block. Invoqued by preg_replace_callback function below * execute stripslaches() on the xml block. Invoqued by preg_replace_callback function below
* *
@ -337,9 +338,11 @@ class RainTPL{
* @return string * @return string
*/ */
protected function xml_reSubstitution($capture) { protected function xml_reSubstitution($capture) {
return "<?php echo '<?xml ".stripslashes($capture[1])." ?>'; ?>"; return "<?php echo '<?xml ".stripslashes($capture[1])." ?>'; ?>";
} }
/** /**
* Compile and write the compiled template file * Compile and write the compiled template file
* *
@ -398,20 +401,20 @@ class RainTPL{
protected function compileTemplate( $template_code, $tpl_basedir ){ protected function compileTemplate( $template_code, $tpl_basedir ){
//tag list //tag list
$tag_regexp = array( 'loop' => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})', $tag_regexp = array( 'loop' => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})',
'loop_close' => '(\{\/loop\})', 'loop_close' => '(\{\/loop\})',
'if' => '(\{if(?: condition){0,1}="[^"]*"\})', 'if' => '(\{if(?: condition){0,1}="[^"]*"\})',
'elseif' => '(\{elseif(?: condition){0,1}="[^"]*"\})', 'elseif' => '(\{elseif(?: condition){0,1}="[^"]*"\})',
'else' => '(\{else\})', 'else' => '(\{else\})',
'if_close' => '(\{\/if\})', 'if_close' => '(\{\/if\})',
'function' => '(\{function="[^"]*"\})', 'function' => '(\{function="[^"]*"\})',
'noparse' => '(\{noparse\})', 'noparse' => '(\{noparse\})',
'noparse_close'=> '(\{\/noparse\})', 'noparse_close'=> '(\{\/noparse\})',
'ignore' => '(\{ignore\}|\{\*)', 'ignore' => '(\{ignore\}|\{\*)',
'ignore_close' => '(\{\/ignore\}|\*\})', 'ignore_close' => '(\{\/ignore\}|\*\})',
'include' => '(\{include="[^"]*"(?: cache="[^"]*")?\})', 'include' => '(\{include="[^"]*"(?: cache="[^"]*")?\})',
'template_info'=> '(\{\$template_info\})', 'template_info'=> '(\{\$template_info\})',
'function' => '(\{function="(\w*?)(?:.*?)"\})' 'function' => '(\{function="(\w*?)(?:.*?)"\})'
); );
$tag_regexp = "/" . join( "|", $tag_regexp ) . "/"; $tag_regexp = "/" . join( "|", $tag_regexp ) . "/";
@ -444,35 +447,35 @@ class RainTPL{
//variables initialization //variables initialization
$compiled_code = $open_if = $comment_is_open = $ignore_is_open = null; $compiled_code = $open_if = $comment_is_open = $ignore_is_open = null;
$loop_level = 0; $loop_level = 0;
//read all parsed code //read all parsed code
while( $html = array_shift( $parsed_code ) ){ while( $html = array_shift( $parsed_code ) ){
//close ignore tag //close ignore tag
if( !$comment_is_open && ( strpos( $html, '{/ignore}' ) !== FALSE || strpos( $html, '*}' ) !== FALSE ) ) if( !$comment_is_open && ( strpos( $html, '{/ignore}' ) !== FALSE || strpos( $html, '*}' ) !== FALSE ) )
$ignore_is_open = false; $ignore_is_open = false;
//code between tag ignore id deleted //code between tag ignore id deleted
elseif( $ignore_is_open ){ elseif( $ignore_is_open ){
//ignore the code //ignore the code
} }
//close no parse tag //close no parse tag
elseif( strpos( $html, '{/noparse}' ) !== FALSE ) elseif( strpos( $html, '{/noparse}' ) !== FALSE )
$comment_is_open = false; $comment_is_open = false;
//code between tag noparse is not compiled //code between tag noparse is not compiled
elseif( $comment_is_open ) elseif( $comment_is_open )
$compiled_code .= $html; $compiled_code .= $html;
//ignore //ignore
elseif( strpos( $html, '{ignore}' ) !== FALSE || strpos( $html, '{*' ) !== FALSE ) elseif( strpos( $html, '{ignore}' ) !== FALSE || strpos( $html, '{*' ) !== FALSE )
$ignore_is_open = true; $ignore_is_open = true;
//noparse //noparse
elseif( strpos( $html, '{noparse}' ) !== FALSE ) elseif( strpos( $html, '{noparse}' ) !== FALSE )
$comment_is_open = true; $comment_is_open = true;
//include tag //include tag
elseif( preg_match( '/\{include="([^"]*)"(?: cache="([^"]*)"){0,1}\}/', $html, $code ) ){ elseif( preg_match( '/\{include="([^"]*)"(?: cache="([^"]*)"){0,1}\}/', $html, $code ) ){
@ -509,19 +512,19 @@ class RainTPL{
} }
//loop //loop
elseif( preg_match( '/\{loop(?: name){0,1}="\${0,1}([^"]*)"\}/', $html, $code ) ){ elseif( preg_match( '/\{loop(?: name){0,1}="\${0,1}([^"]*)"\}/', $html, $code ) ){
//increase the loop counter //increase the loop counter
$loop_level++; $loop_level++;
//replace the variable in the loop //replace the variable in the loop
$var = $this->var_replace( '$' . $code[ 1 ], $tag_left_delimiter=null, $tag_right_delimiter=null, $php_left_delimiter=null, $php_right_delimiter=null, $loop_level-1 ); $var = $this->var_replace( '$' . $code[ 1 ], $tag_left_delimiter=null, $tag_right_delimiter=null, $php_left_delimiter=null, $php_right_delimiter=null, $loop_level-1 );
//loop variables //loop variables
$counter = "\$counter$loop_level"; // count iteration $counter = "\$counter$loop_level"; // count iteration
$key = "\$key$loop_level"; // key $key = "\$key$loop_level"; // key
$value = "\$value$loop_level"; // value $value = "\$value$loop_level"; // value
//loop code //loop code
$compiled_code .= "<?php $counter=-1; if( isset($var) && is_array($var) && sizeof($var) ) foreach( $var as $key => $value ){ $counter++; ?>"; $compiled_code .= "<?php $counter=-1; if( isset($var) && is_array($var) && sizeof($var) ) foreach( $var as $key => $value ){ $counter++; ?>";
@ -653,6 +656,7 @@ class RainTPL{
} }
/** /**
* Reduce a path * Reduce a path
* *
@ -728,8 +732,6 @@ class RainTPL{
/** /**
* replace constants * replace constants
* *
@ -812,28 +814,28 @@ class RainTPL{
//if there's a function //if there's a function
if( $function_var ){ if( $function_var ){
// check if there's a function or a static method and separate, function by parameters // check if there's a function or a static method and separate, function by parameters
$function_var = str_replace("::", "@double_dot@", $function_var ); $function_var = str_replace("::", "@double_dot@", $function_var );
// get the position of the first : // get the position of the first :
if( $dot_position = strpos( $function_var, ":" ) ){ if( $dot_position = strpos( $function_var, ":" ) ){
// get the function and the parameters // get the function and the parameters
$function = substr( $function_var, 0, $dot_position ); $function = substr( $function_var, 0, $dot_position );
$params = substr( $function_var, $dot_position+1 ); $params = substr( $function_var, $dot_position+1 );
} }
else{ else{
//get the function //get the function
$function = str_replace( "@double_dot@", "::", $function_var ); $function = str_replace( "@double_dot@", "::", $function_var );
$params = null; $params = null;
} }
// replace back the @double_dot@ with :: // replace back the @double_dot@ with ::
$function = str_replace( "@double_dot@", "::", $function ); $function = str_replace( "@double_dot@", "::", $function );
$params = str_replace( "@double_dot@", "::", $params ); $params = str_replace( "@double_dot@", "::", $params );
} }
@ -880,105 +882,105 @@ class RainTPL{
//all variables //all variables
if( preg_match_all( '/' . $tag_left_delimiter . '\$(\w+(?:\.\${0,1}[A-Za-z0-9_]+)*(?:(?:\[\${0,1}[A-Za-z0-9_]+\])|(?:\-\>\${0,1}[A-Za-z0-9_]+))*)(.*?)' . $tag_right_delimiter . '/', $html, $matches ) ){ if( preg_match_all( '/' . $tag_left_delimiter . '\$(\w+(?:\.\${0,1}[A-Za-z0-9_]+)*(?:(?:\[\${0,1}[A-Za-z0-9_]+\])|(?:\-\>\${0,1}[A-Za-z0-9_]+))*)(.*?)' . $tag_right_delimiter . '/', $html, $matches ) ){
for( $parsed=array(), $i=0, $n=count($matches[0]); $i<$n; $i++ ) for( $parsed=array(), $i=0, $n=count($matches[0]); $i<$n; $i++ )
$parsed[$matches[0][$i]] = array('var'=>$matches[1][$i],'extra_var'=>$matches[2][$i]); $parsed[$matches[0][$i]] = array('var'=>$matches[1][$i],'extra_var'=>$matches[2][$i]);
foreach( $parsed as $tag => $array ){ foreach( $parsed as $tag => $array ){
//variable name ex: news.title //variable name ex: news.title
$var = $array['var']; $var = $array['var'];
//function and parameters associate to the variable ex: substr:0,100 //function and parameters associate to the variable ex: substr:0,100
$extra_var = $array['extra_var']; $extra_var = $array['extra_var'];
// check if there's any function disabled by black_list // check if there's any function disabled by black_list
$this->function_check( $tag ); $this->function_check( $tag );
$extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level ); $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level );
// check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value
$is_init_variable = preg_match( "/^[a-z_A-Z\.\[\](\-\>)]*=[^=]*$/", $extra_var ); $is_init_variable = preg_match( "/^[a-z_A-Z\.\[\](\-\>)]*=[^=]*$/", $extra_var );
//function associate to variable //function associate to variable
$function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null; $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null;
//variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title) //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title)
$temp = preg_split( "/\.|\[|\-\>/", $var ); $temp = preg_split( "/\.|\[|\-\>/", $var );
//variable name //variable name
$var_name = $temp[ 0 ]; $var_name = $temp[ 0 ];
//variable path //variable path
$variable_path = substr( $var, strlen( $var_name ) ); $variable_path = substr( $var, strlen( $var_name ) );
//parentesis transform [ e ] in [" e in "] //parentesis transform [ e ] in [" e in "]
$variable_path = str_replace( '[', '["', $variable_path ); $variable_path = str_replace( '[', '["', $variable_path );
$variable_path = str_replace( ']', '"]', $variable_path ); $variable_path = str_replace( ']', '"]', $variable_path );
//transform .$variable in ["$variable"] and .variable in ["variable"] //transform .$variable in ["$variable"] and .variable in ["variable"]
$variable_path = preg_replace('/\.(\${0,1}\w+)/', '["\\1"]', $variable_path ); $variable_path = preg_replace('/\.(\${0,1}\w+)/', '["\\1"]', $variable_path );
// if is an assignment also assign the variable to $this->var['value'] // if is an assignment also assign the variable to $this->var['value']
if( $is_init_variable ) if( $is_init_variable )
$extra_var = "=\$this->var['{$var_name}']{$variable_path}" . $extra_var; $extra_var = "=\$this->var['{$var_name}']{$variable_path}" . $extra_var;
//if there's a function //if there's a function
if( $function_var ){ if( $function_var ){
// check if there's a function or a static method and separate, function by parameters // check if there's a function or a static method and separate, function by parameters
$function_var = str_replace("::", "@double_dot@", $function_var ); $function_var = str_replace("::", "@double_dot@", $function_var );
// get the position of the first : // get the position of the first :
if( $dot_position = strpos( $function_var, ":" ) ){ if( $dot_position = strpos( $function_var, ":" ) ){
// get the function and the parameters // get the function and the parameters
$function = substr( $function_var, 0, $dot_position ); $function = substr( $function_var, 0, $dot_position );
$params = substr( $function_var, $dot_position+1 ); $params = substr( $function_var, $dot_position+1 );
} }
else{ else{
//get the function //get the function
$function = str_replace( "@double_dot@", "::", $function_var ); $function = str_replace( "@double_dot@", "::", $function_var );
$params = null; $params = null;
} }
// replace back the @double_dot@ with :: // replace back the @double_dot@ with ::
$function = str_replace( "@double_dot@", "::", $function ); $function = str_replace( "@double_dot@", "::", $function );
$params = str_replace( "@double_dot@", "::", $params ); $params = str_replace( "@double_dot@", "::", $params );
} }
else else
$function = $params = null; $function = $params = null;
//if it is inside a loop //if it is inside a loop
if( $loop_level ){ if( $loop_level ){
//verify the variable name //verify the variable name
if( $var_name == 'key' ) if( $var_name == 'key' )
$php_var = '$key' . $loop_level; $php_var = '$key' . $loop_level;
elseif( $var_name == 'value' ) elseif( $var_name == 'value' )
$php_var = '$value' . $loop_level . $variable_path; $php_var = '$value' . $loop_level . $variable_path;
elseif( $var_name == 'counter' ) elseif( $var_name == 'counter' )
$php_var = '$counter' . $loop_level; $php_var = '$counter' . $loop_level;
else else
$php_var = '$' . $var_name . $variable_path; $php_var = '$' . $var_name . $variable_path;
}else }else
$php_var = '$' . $var_name . $variable_path; $php_var = '$' . $var_name . $variable_path;
// compile the variable for php // compile the variable for php
if( isset( $function ) ) if( isset( $function ) )
$php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter; $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter;
else else
$php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter; $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter;
$html = str_replace( $tag, $php_var, $html ); $html = str_replace( $tag, $php_var, $html );
} }
} }
return $html; return $html;
} }
@ -1015,6 +1017,8 @@ class RainTPL{
} }
/** /**
* Prints debug info about exception or passes it further if debug is disabled. * Prints debug info about exception or passes it further if debug is disabled.
* *
@ -1053,6 +1057,7 @@ class RainTPL{
} }
/** /**
* Basic Rain tpl exception. * Basic Rain tpl exception.
*/ */
@ -1085,6 +1090,8 @@ class RainTpl_Exception extends Exception{
} }
} }
/** /**
* Exception thrown when template file does not exists. * Exception thrown when template file does not exists.
*/ */