support foo->bar[index] syntax

This commit is contained in:
mohrt
2003-02-18 19:58:32 +00:00
parent 2d6d972bf5
commit 7bd3972319
5 changed files with 33 additions and 60 deletions

1
NEWS
View File

@@ -1,3 +1,4 @@
- support $foo->bar[index] syntax (Monte)
- add get_registered_object function (messju, Monte)
- treat unrecognized param attribute syntax as string (Monte)
- support $smarty.const.$foo syntax (messju, Monte)

View File

@@ -227,7 +227,8 @@ class Smarty
'IF_FUNCS' => array('array', 'list',
'isset', 'empty',
'count', 'sizeof',
'in_array', 'is_array'),
'in_array', 'is_array',
'true','false'),
'INCLUDE_ANY' => false,
'PHP_TAGS' => false,
'MODIFIER_FUNCS' => array('count'),

View File

@@ -139,7 +139,7 @@ class Smarty_Compiler extends Smarty {
// $foo->bar($foo, "foo")
// $foo->bar->foo()
// $foo->bar->foo->bar()
$this->_obj_ext_regexp = '\->(?:\w+|\$?' . $this->_dvar_guts_regexp . ')';
$this->_obj_ext_regexp = '\->(?:\$?' . $this->_dvar_guts_regexp . ')';
$this->_obj_params_regexp = '\((?:\w+|'
. $this->_var_regexp . '(?:\s*,\s*(?:(?:\w+|'
. $this->_var_regexp . ')))*)?\)';
@@ -1084,8 +1084,8 @@ class Smarty_Compiler extends Smarty {
/* Tokenize args for 'if' tag. */
preg_match_all('/(?>
' . $this->_obj_call_regexp . '(?:' . $this->_mod_regexp . '*) | # valid object call
' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*) | # var or quoted string
' . $this->_obj_call_regexp . '(?:' . $this->_mod_regexp . '*)? | # valid object call
' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)? | # var or quoted string
\-?\d+(?:\.\d+)?|\.\d+|!==|<=>|===|==|!=|<=|>=|\&\&|\|\||\(|\)|,|\!|\^|=|\&|\~|<|>|\||\%|\+|\-|\/|\*|\@ | # valid non-word token
\b\w+\b | # valid word token
\S+ # anything else
@@ -1212,11 +1212,8 @@ class Smarty_Compiler extends Smarty {
!in_array($token, $this->security_settings['IF_FUNCS'])) {
$this->_syntax_error("(secure mode) '$token' not allowed in if statement", E_USER_ERROR, __FILE__, __LINE__);
}
} elseif(preg_match('!^' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)$!', $token)) {
// variable
$token = $this->_parse_var_props($token);
} elseif(preg_match('!^' . $this->_obj_call_regexp . '(?:' . $this->_mod_regexp . '*)$!', $token)) {
// object
} elseif(preg_match('!^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)$!', $token)) {
// object or variable
$token = $this->_parse_var_props($token);
} elseif(is_numeric($token)) {
// number, skip it
@@ -1474,8 +1471,8 @@ class Smarty_Compiler extends Smarty {
$modifiers = empty($modifiers) ? $_default_mod_string : $_default_mod_string . '|' . $modifiers;
}
// get [foo] and .foo and ->foo() pieces
preg_match_all('!(?:^\w+)|(?:' . $this->_obj_ext_regexp . ')+(?:' . $this->_obj_params_regexp . ')?|(?:' . $this->_var_bracket_regexp . ')|\.\$?\w+!', $var_ref, $match);
// get [foo] and .foo and ->foo and (...) pieces
preg_match_all('!(?:^\w+)|' . $this->_obj_params_regexp . '|(?:' . $this->_var_bracket_regexp . ')|->\w+|\.\$?\w+|\S+!', $var_ref, $match);
$indexes = $match[0];
$var_name = array_shift($indexes);
@@ -1520,23 +1517,11 @@ class Smarty_Compiler extends Smarty {
$this->_syntax_error('call to internal object members is not allowed', E_USER_ERROR, __FILE__, __LINE__);
} elseif($this->security && substr($index,2,1) == '_') {
$this->_syntax_error('(secure) call to private object member is not allowed', E_USER_ERROR, __FILE__, __LINE__);
} else {
if(preg_match('!((?:' . $this->_obj_ext_regexp . ')+)(' . $this->_obj_params_regexp . ')?!', $index, $match)) {
if(!empty($match[2])) {
// parse object parameters
$index = str_replace($match[2], $this->_parse_parenth_args($match[2]), $index);
}
if(preg_match_all('!' . $this->_dvar_regexp . '!', $match[1], $_dvar_match)) {
// parse embedded variables
$_match_replace = $match[1];
foreach($_dvar_match[0] as $_curr_var) {
$_match_replace = str_replace($_curr_var, '{' . $this->_parse_var($_curr_var) . '}', $_match_replace);
}
$index = str_replace($match[1], $_match_replace, $index);
}
}
$output .= $index;
}
} elseif ($index{0} == '(') {
$index = $this->_parse_parenth_args($index);
$output .= $index;
} else {
$output .= $index;
}

View File

@@ -227,7 +227,8 @@ class Smarty
'IF_FUNCS' => array('array', 'list',
'isset', 'empty',
'count', 'sizeof',
'in_array', 'is_array'),
'in_array', 'is_array',
'true','false'),
'INCLUDE_ANY' => false,
'PHP_TAGS' => false,
'MODIFIER_FUNCS' => array('count'),

View File

@@ -139,7 +139,7 @@ class Smarty_Compiler extends Smarty {
// $foo->bar($foo, "foo")
// $foo->bar->foo()
// $foo->bar->foo->bar()
$this->_obj_ext_regexp = '\->(?:\w+|\$?' . $this->_dvar_guts_regexp . ')';
$this->_obj_ext_regexp = '\->(?:\$?' . $this->_dvar_guts_regexp . ')';
$this->_obj_params_regexp = '\((?:\w+|'
. $this->_var_regexp . '(?:\s*,\s*(?:(?:\w+|'
. $this->_var_regexp . ')))*)?\)';
@@ -1084,8 +1084,8 @@ class Smarty_Compiler extends Smarty {
/* Tokenize args for 'if' tag. */
preg_match_all('/(?>
' . $this->_obj_call_regexp . '(?:' . $this->_mod_regexp . '*) | # valid object call
' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*) | # var or quoted string
' . $this->_obj_call_regexp . '(?:' . $this->_mod_regexp . '*)? | # valid object call
' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)? | # var or quoted string
\-?\d+(?:\.\d+)?|\.\d+|!==|<=>|===|==|!=|<=|>=|\&\&|\|\||\(|\)|,|\!|\^|=|\&|\~|<|>|\||\%|\+|\-|\/|\*|\@ | # valid non-word token
\b\w+\b | # valid word token
\S+ # anything else
@@ -1212,11 +1212,8 @@ class Smarty_Compiler extends Smarty {
!in_array($token, $this->security_settings['IF_FUNCS'])) {
$this->_syntax_error("(secure mode) '$token' not allowed in if statement", E_USER_ERROR, __FILE__, __LINE__);
}
} elseif(preg_match('!^' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)$!', $token)) {
// variable
$token = $this->_parse_var_props($token);
} elseif(preg_match('!^' . $this->_obj_call_regexp . '(?:' . $this->_mod_regexp . '*)$!', $token)) {
// object
} elseif(preg_match('!^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . '*)$!', $token)) {
// object or variable
$token = $this->_parse_var_props($token);
} elseif(is_numeric($token)) {
// number, skip it
@@ -1474,8 +1471,8 @@ class Smarty_Compiler extends Smarty {
$modifiers = empty($modifiers) ? $_default_mod_string : $_default_mod_string . '|' . $modifiers;
}
// get [foo] and .foo and ->foo() pieces
preg_match_all('!(?:^\w+)|(?:' . $this->_obj_ext_regexp . ')+(?:' . $this->_obj_params_regexp . ')?|(?:' . $this->_var_bracket_regexp . ')|\.\$?\w+!', $var_ref, $match);
// get [foo] and .foo and ->foo and (...) pieces
preg_match_all('!(?:^\w+)|' . $this->_obj_params_regexp . '|(?:' . $this->_var_bracket_regexp . ')|->\w+|\.\$?\w+|\S+!', $var_ref, $match);
$indexes = $match[0];
$var_name = array_shift($indexes);
@@ -1520,23 +1517,11 @@ class Smarty_Compiler extends Smarty {
$this->_syntax_error('call to internal object members is not allowed', E_USER_ERROR, __FILE__, __LINE__);
} elseif($this->security && substr($index,2,1) == '_') {
$this->_syntax_error('(secure) call to private object member is not allowed', E_USER_ERROR, __FILE__, __LINE__);
} else {
if(preg_match('!((?:' . $this->_obj_ext_regexp . ')+)(' . $this->_obj_params_regexp . ')?!', $index, $match)) {
if(!empty($match[2])) {
// parse object parameters
$index = str_replace($match[2], $this->_parse_parenth_args($match[2]), $index);
}
if(preg_match_all('!' . $this->_dvar_regexp . '!', $match[1], $_dvar_match)) {
// parse embedded variables
$_match_replace = $match[1];
foreach($_dvar_match[0] as $_curr_var) {
$_match_replace = str_replace($_curr_var, '{' . $this->_parse_var($_curr_var) . '}', $_match_replace);
}
$index = str_replace($match[1], $_match_replace, $index);
}
}
$output .= $index;
}
} elseif ($index{0} == '(') {
$index = $this->_parse_parenth_args($index);
$output .= $index;
} else {
$output .= $index;
}