mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 10:24:26 +02:00
add support for object registration
This commit is contained in:
6
NEWS
6
NEWS
@@ -1,6 +1,6 @@
|
||||
- add debug template to secure dir (Ferdinand Beyer, Monte)
|
||||
- add template_dir to secure_dir by default (Ferdinand
|
||||
Beyer, Monte)
|
||||
- add support for object registration (Monte)
|
||||
- add debug template to secure dir, add template_dir
|
||||
to secure_dir by default (Ferdinand Beyer, Monte)
|
||||
- added support for object method access (Monte)
|
||||
- fixed bug with directories named '0' (Frank Bauer, Monte)
|
||||
- add javascript parameter to escape modifier (Monte)
|
||||
|
@@ -334,6 +334,26 @@ class Smarty
|
||||
unset($this->_plugins['function'][$function]);
|
||||
}
|
||||
|
||||
/*======================================================================*\
|
||||
Function: register_object
|
||||
Purpose: Registers object to be used in templates
|
||||
\*======================================================================*/
|
||||
function register_object($object, &$object_impl)
|
||||
{
|
||||
$this->_plugins['object'][$object] =
|
||||
array(&$object_impl, null, null, false);
|
||||
}
|
||||
|
||||
/*======================================================================*\
|
||||
function: unregister_object
|
||||
Purpose: Unregisters object
|
||||
\*======================================================================*/
|
||||
function unregister_object($object)
|
||||
{
|
||||
unset($this->_plugins['object'][$object]);
|
||||
}
|
||||
|
||||
|
||||
/*======================================================================*\
|
||||
Function: register_block
|
||||
Purpose: Registers block function to be used in templates
|
||||
@@ -614,10 +634,12 @@ class Smarty
|
||||
function fetch($_smarty_tpl_file, $_smarty_cache_id = null, $_smarty_compile_id = null, $_smarty_display = false)
|
||||
{
|
||||
$_smarty_old_error_level = $this->debugging ? error_reporting() : error_reporting(error_reporting() & ~E_NOTICE);
|
||||
|
||||
if($this->security && !in_array($this->template_dir, $this->secure_dir)) {
|
||||
// add template_dir to secure_dir array
|
||||
array_unshift($this->secure_dir, $this->template_dir);
|
||||
}
|
||||
|
||||
if (!$this->debugging && $this->debugging_ctrl == 'URL'
|
||||
&& strstr($GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING'], $this->_smarty_debug_id)) {
|
||||
// enable debugging from URL
|
||||
@@ -1884,8 +1906,11 @@ function _run_insert_handler($args)
|
||||
*/
|
||||
if (isset($plugin)) {
|
||||
if (!$plugin[3]) {
|
||||
if (!function_exists($plugin[0])) {
|
||||
// no line number, see if it is valid
|
||||
if ($type == 'function' && !function_exists($plugin[0])) {
|
||||
$this->_trigger_plugin_error("$type '$name' is not implemented", $tpl_file, $tpl_line, __FILE__, __LINE__);
|
||||
} elseif ($type == 'object' && !is_object($plugin[0])) {
|
||||
$this->_trigger_plugin_error("$type '$name' is not an object", $tpl_file, $tpl_line, __FILE__, __LINE__);
|
||||
} else {
|
||||
$plugin[1] = $tpl_file;
|
||||
$plugin[2] = $tpl_line;
|
||||
|
@@ -145,6 +145,10 @@ class Smarty_Compiler extends Smarty {
|
||||
// _foo_bar
|
||||
$this->_func_regexp = '[a-zA-Z_]\w*';
|
||||
|
||||
// matches valid registered object:
|
||||
// foo.bar
|
||||
$this->_reg_obj_regexp = '[a-zA-Z_]\w*\.[a-zA-Z_]\w*';
|
||||
|
||||
// matches valid parameter values:
|
||||
// true
|
||||
// $foo
|
||||
@@ -348,7 +352,8 @@ class Smarty_Compiler extends Smarty {
|
||||
return '';
|
||||
|
||||
/* Split tag into two three parts: command, command modifiers and the arguments. */
|
||||
if(! preg_match('/^(?:(' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*))
|
||||
if(! preg_match('/^(?:(' . $this->_obj_call_regexp . '|' . $this->_var_regexp
|
||||
. '|' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*))
|
||||
(?:\s+(.*))?$
|
||||
/xs', $template_tag, $match)) {
|
||||
$this->_syntax_error("unrecognized tag: $template_tag", E_USER_ERROR, __FILE__, __LINE__);
|
||||
@@ -358,10 +363,10 @@ class Smarty_Compiler extends Smarty {
|
||||
$tag_modifier = isset($match[2]) ? $match[2] : null;
|
||||
$tag_args = isset($match[3]) ? $match[3] : null;
|
||||
|
||||
/* If the tag name is not a function, we process it. */
|
||||
if (!preg_match('!^\/?' . $this->_func_regexp . '$!', $tag_command)) {
|
||||
$_tag_attrs = $this->_parse_attrs($tag_args);
|
||||
$return = $this->_parse_var_props($tag_command . $tag_modifier, $_tag_attrs);
|
||||
|
||||
/* If the tag name is a variable or object, we process it. */
|
||||
if (preg_match('!^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '$!', $tag_command)) {
|
||||
$return = $this->_parse_var_props($tag_command . $tag_modifier, $this->_parse_attrs($tag_args));
|
||||
if(isset($_tag_attrs['assign'])) {
|
||||
return "<?php \$this->assign('" . $this->_dequote($_tag_attrs['assign']) . "', $return ); ?>\n";
|
||||
} else {
|
||||
@@ -369,6 +374,11 @@ class Smarty_Compiler extends Smarty {
|
||||
}
|
||||
}
|
||||
|
||||
/* If the tag name is a registered object, we process it. */
|
||||
if (preg_match('!^' . $this->_reg_obj_regexp . '$!', $tag_command)) {
|
||||
return $this->_compile_registered_object_tag($tag_command, $this->_parse_attrs($tag_args), $tag_modifier);
|
||||
}
|
||||
|
||||
switch ($tag_command) {
|
||||
case 'include':
|
||||
return $this->_compile_include_tag($tag_args);
|
||||
@@ -631,6 +641,51 @@ class Smarty_Compiler extends Smarty {
|
||||
return '<?php echo ' . $return . " ; ?>\n";
|
||||
}
|
||||
|
||||
/*======================================================================*\
|
||||
Function: _compile_registered_object_tag
|
||||
Purpose: compile a registered object tag
|
||||
\*======================================================================*/
|
||||
function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier)
|
||||
{
|
||||
list($object, $obj_comp) = explode('.', $tag_command);
|
||||
$this->_add_plugin('object', $object);
|
||||
|
||||
$arg_list = array();
|
||||
if(count($attrs)) {
|
||||
$_assign_var = false;
|
||||
foreach ($attrs as $arg_name => $arg_value) {
|
||||
if($arg_name == 'assign') {
|
||||
$_assign_var = $arg_value;
|
||||
continue;
|
||||
}
|
||||
if (is_bool($arg_value))
|
||||
$arg_value = $arg_value ? 'true' : 'false';
|
||||
$arg_list[] = "'$arg_name' => $arg_value";
|
||||
}
|
||||
}
|
||||
|
||||
if(!$this->_plugins['object'][$object][0]) {
|
||||
$this->_trigger_plugin_error("Smarty error: Registered '$object' is not an object");
|
||||
} elseif(method_exists($this->_plugins['object'][$object][0], $obj_comp)) {
|
||||
// method
|
||||
$return = "\$this->_plugins['object']['$object'][0]->$obj_comp(array(".implode(',', (array)$arg_list)."), \$this)";
|
||||
} else {
|
||||
// property
|
||||
$return = "\$this->_plugins['object']['$object'][0]->$obj_comp";
|
||||
}
|
||||
|
||||
if($tag_modifier != '') {
|
||||
$this->_parse_modifiers($return, $tag_modifier);
|
||||
}
|
||||
|
||||
if($_assign_var) {
|
||||
return "<?php \$this->assign('" . $this->_dequote($_assign_var) ."', $return); ?>\n";
|
||||
} else {
|
||||
return '<?php echo ' . $return . " ; ?>\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*======================================================================*\
|
||||
Function: _compile_insert_tag
|
||||
|
@@ -334,6 +334,26 @@ class Smarty
|
||||
unset($this->_plugins['function'][$function]);
|
||||
}
|
||||
|
||||
/*======================================================================*\
|
||||
Function: register_object
|
||||
Purpose: Registers object to be used in templates
|
||||
\*======================================================================*/
|
||||
function register_object($object, &$object_impl)
|
||||
{
|
||||
$this->_plugins['object'][$object] =
|
||||
array(&$object_impl, null, null, false);
|
||||
}
|
||||
|
||||
/*======================================================================*\
|
||||
function: unregister_object
|
||||
Purpose: Unregisters object
|
||||
\*======================================================================*/
|
||||
function unregister_object($object)
|
||||
{
|
||||
unset($this->_plugins['object'][$object]);
|
||||
}
|
||||
|
||||
|
||||
/*======================================================================*\
|
||||
Function: register_block
|
||||
Purpose: Registers block function to be used in templates
|
||||
@@ -614,10 +634,12 @@ class Smarty
|
||||
function fetch($_smarty_tpl_file, $_smarty_cache_id = null, $_smarty_compile_id = null, $_smarty_display = false)
|
||||
{
|
||||
$_smarty_old_error_level = $this->debugging ? error_reporting() : error_reporting(error_reporting() & ~E_NOTICE);
|
||||
|
||||
if($this->security && !in_array($this->template_dir, $this->secure_dir)) {
|
||||
// add template_dir to secure_dir array
|
||||
array_unshift($this->secure_dir, $this->template_dir);
|
||||
}
|
||||
|
||||
if (!$this->debugging && $this->debugging_ctrl == 'URL'
|
||||
&& strstr($GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING'], $this->_smarty_debug_id)) {
|
||||
// enable debugging from URL
|
||||
@@ -1884,8 +1906,11 @@ function _run_insert_handler($args)
|
||||
*/
|
||||
if (isset($plugin)) {
|
||||
if (!$plugin[3]) {
|
||||
if (!function_exists($plugin[0])) {
|
||||
// no line number, see if it is valid
|
||||
if ($type == 'function' && !function_exists($plugin[0])) {
|
||||
$this->_trigger_plugin_error("$type '$name' is not implemented", $tpl_file, $tpl_line, __FILE__, __LINE__);
|
||||
} elseif ($type == 'object' && !is_object($plugin[0])) {
|
||||
$this->_trigger_plugin_error("$type '$name' is not an object", $tpl_file, $tpl_line, __FILE__, __LINE__);
|
||||
} else {
|
||||
$plugin[1] = $tpl_file;
|
||||
$plugin[2] = $tpl_line;
|
||||
|
@@ -145,6 +145,10 @@ class Smarty_Compiler extends Smarty {
|
||||
// _foo_bar
|
||||
$this->_func_regexp = '[a-zA-Z_]\w*';
|
||||
|
||||
// matches valid registered object:
|
||||
// foo.bar
|
||||
$this->_reg_obj_regexp = '[a-zA-Z_]\w*\.[a-zA-Z_]\w*';
|
||||
|
||||
// matches valid parameter values:
|
||||
// true
|
||||
// $foo
|
||||
@@ -348,7 +352,8 @@ class Smarty_Compiler extends Smarty {
|
||||
return '';
|
||||
|
||||
/* Split tag into two three parts: command, command modifiers and the arguments. */
|
||||
if(! preg_match('/^(?:(' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*))
|
||||
if(! preg_match('/^(?:(' . $this->_obj_call_regexp . '|' . $this->_var_regexp
|
||||
. '|' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*))
|
||||
(?:\s+(.*))?$
|
||||
/xs', $template_tag, $match)) {
|
||||
$this->_syntax_error("unrecognized tag: $template_tag", E_USER_ERROR, __FILE__, __LINE__);
|
||||
@@ -358,10 +363,10 @@ class Smarty_Compiler extends Smarty {
|
||||
$tag_modifier = isset($match[2]) ? $match[2] : null;
|
||||
$tag_args = isset($match[3]) ? $match[3] : null;
|
||||
|
||||
/* If the tag name is not a function, we process it. */
|
||||
if (!preg_match('!^\/?' . $this->_func_regexp . '$!', $tag_command)) {
|
||||
$_tag_attrs = $this->_parse_attrs($tag_args);
|
||||
$return = $this->_parse_var_props($tag_command . $tag_modifier, $_tag_attrs);
|
||||
|
||||
/* If the tag name is a variable or object, we process it. */
|
||||
if (preg_match('!^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '$!', $tag_command)) {
|
||||
$return = $this->_parse_var_props($tag_command . $tag_modifier, $this->_parse_attrs($tag_args));
|
||||
if(isset($_tag_attrs['assign'])) {
|
||||
return "<?php \$this->assign('" . $this->_dequote($_tag_attrs['assign']) . "', $return ); ?>\n";
|
||||
} else {
|
||||
@@ -369,6 +374,11 @@ class Smarty_Compiler extends Smarty {
|
||||
}
|
||||
}
|
||||
|
||||
/* If the tag name is a registered object, we process it. */
|
||||
if (preg_match('!^' . $this->_reg_obj_regexp . '$!', $tag_command)) {
|
||||
return $this->_compile_registered_object_tag($tag_command, $this->_parse_attrs($tag_args), $tag_modifier);
|
||||
}
|
||||
|
||||
switch ($tag_command) {
|
||||
case 'include':
|
||||
return $this->_compile_include_tag($tag_args);
|
||||
@@ -631,6 +641,51 @@ class Smarty_Compiler extends Smarty {
|
||||
return '<?php echo ' . $return . " ; ?>\n";
|
||||
}
|
||||
|
||||
/*======================================================================*\
|
||||
Function: _compile_registered_object_tag
|
||||
Purpose: compile a registered object tag
|
||||
\*======================================================================*/
|
||||
function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier)
|
||||
{
|
||||
list($object, $obj_comp) = explode('.', $tag_command);
|
||||
$this->_add_plugin('object', $object);
|
||||
|
||||
$arg_list = array();
|
||||
if(count($attrs)) {
|
||||
$_assign_var = false;
|
||||
foreach ($attrs as $arg_name => $arg_value) {
|
||||
if($arg_name == 'assign') {
|
||||
$_assign_var = $arg_value;
|
||||
continue;
|
||||
}
|
||||
if (is_bool($arg_value))
|
||||
$arg_value = $arg_value ? 'true' : 'false';
|
||||
$arg_list[] = "'$arg_name' => $arg_value";
|
||||
}
|
||||
}
|
||||
|
||||
if(!$this->_plugins['object'][$object][0]) {
|
||||
$this->_trigger_plugin_error("Smarty error: Registered '$object' is not an object");
|
||||
} elseif(method_exists($this->_plugins['object'][$object][0], $obj_comp)) {
|
||||
// method
|
||||
$return = "\$this->_plugins['object']['$object'][0]->$obj_comp(array(".implode(',', (array)$arg_list)."), \$this)";
|
||||
} else {
|
||||
// property
|
||||
$return = "\$this->_plugins['object']['$object'][0]->$obj_comp";
|
||||
}
|
||||
|
||||
if($tag_modifier != '') {
|
||||
$this->_parse_modifiers($return, $tag_modifier);
|
||||
}
|
||||
|
||||
if($_assign_var) {
|
||||
return "<?php \$this->assign('" . $this->_dequote($_assign_var) ."', $return); ?>\n";
|
||||
} else {
|
||||
return '<?php echo ' . $return . " ; ?>\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*======================================================================*\
|
||||
Function: _compile_insert_tag
|
||||
|
Reference in New Issue
Block a user