added block-methods for registered objects

This commit is contained in:
messju
2003-06-08 01:07:39 +00:00
parent fa948918c7
commit 5b4b13a319
3 changed files with 57 additions and 21 deletions

1
NEWS
View File

@@ -1,3 +1,4 @@
- added block-methods for registered objects (Bharat Mediratta, messju)
- ignore one char resource names like c:foo.tpl (Monte) - ignore one char resource names like c:foo.tpl (Monte)
- added default_resource_type feature (Monte) - added default_resource_type feature (Monte)
- fix bug where config file starts with hidden section (boots, Monte) - fix bug where config file starts with hidden section (boots, Monte)

View File

@@ -721,13 +721,14 @@ class Smarty
* @param object &$object_impl the referenced PHP object to register * @param object &$object_impl the referenced PHP object to register
* @param null|array $allowed list of allowed methods (empty = all) * @param null|array $allowed list of allowed methods (empty = all)
* @param boolean $smarty_args smarty argument format, else traditional * @param boolean $smarty_args smarty argument format, else traditional
* @param null|array $block_functs list of methods that are block format
*/ */
function register_object($object, &$object_impl, $allowed = array(), $smarty_args = true) function register_object($object, &$object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
{ {
settype($allowed, 'array'); settype($allowed, 'array');
settype($smarty_args, 'boolean'); settype($smarty_args, 'boolean');
$this->_reg_objects[$object] = $this->_reg_objects[$object] =
array(&$object_impl, $allowed, $smarty_args); array(&$object_impl, $allowed, $smarty_args, $block_methods);
} }
/** /**

View File

@@ -379,7 +379,7 @@ class Smarty_Compiler extends Smarty {
/* Split tag into two three parts: command, command modifiers and the arguments. */ /* Split tag into two three parts: command, command modifiers and the arguments. */
if(! preg_match('/^(?:(' . $this->_obj_call_regexp . '|' . $this->_var_regexp if(! preg_match('/^(?:(' . $this->_obj_call_regexp . '|' . $this->_var_regexp
. '|' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*)) . '|\/?' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*))
(?:\s+(.*))?$ (?:\s+(.*))?$
/xs', $template_tag, $match)) { /xs', $template_tag, $match)) {
$this->_syntax_error("unrecognized tag: $template_tag", E_USER_ERROR, __FILE__, __LINE__); $this->_syntax_error("unrecognized tag: $template_tag", E_USER_ERROR, __FILE__, __LINE__);
@@ -401,7 +401,7 @@ class Smarty_Compiler extends Smarty {
} }
/* If the tag name is a registered object, we process it. */ /* If the tag name is a registered object, we process it. */
if (preg_match('!^' . $this->_reg_obj_regexp . '$!', $tag_command)) { if (preg_match('!^\/?' . $this->_reg_obj_regexp . '$!', $tag_command)) {
return $this->_compile_registered_object_tag($tag_command, $this->_parse_attrs($tag_args), $tag_modifier); return $this->_compile_registered_object_tag($tag_command, $this->_parse_attrs($tag_args), $tag_modifier);
} }
@@ -691,6 +691,13 @@ class Smarty_Compiler extends Smarty {
*/ */
function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier) function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier)
{ {
if ($tag_command{0} == '/') {
$start_tag = false;
$tag_command = substr($tag_command, 1);
} else {
$start_tag = true;
}
list($object, $obj_comp) = explode('->', $tag_command); list($object, $obj_comp) = explode('->', $tag_command);
$arg_list = array(); $arg_list = array();
@@ -708,37 +715,64 @@ class Smarty_Compiler extends Smarty {
} }
} }
if($this->_reg_objects[$object][2]) {
// smarty object argument format
$args = "array(".implode(',', (array)$arg_list)."), \$this";
} else {
// traditional argument format
$args = implode(',', array_values($attrs));
if (empty($args)) {
$args = 'null';
}
}
$prefix = '';
$postfix = '';
if(!is_object($this->_reg_objects[$object][0])) { if(!is_object($this->_reg_objects[$object][0])) {
$this->_trigger_fatal_error("registered '$object' is not an object"); $this->_trigger_fatal_error("registered '$object' is not an object");
} elseif(!empty($this->_reg_objects[$object][1]) && !in_array($obj_comp, $this->_reg_objects[$object][1])) { } elseif(!empty($this->_reg_objects[$object][1]) && !in_array($obj_comp, $this->_reg_objects[$object][1])) {
$this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'"); $this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'");
} elseif(method_exists($this->_reg_objects[$object][0], $obj_comp)) { } elseif(method_exists($this->_reg_objects[$object][0], $obj_comp)) {
// method // method
if($this->_reg_objects[$object][2]) { if(in_array($obj_comp, $this->_reg_objects[$object][3])) {
// smarty object argument format // block method
$return = "\$this->_reg_objects['$object'][0]->$obj_comp(array(".implode(',', (array)$arg_list)."), \$this)"; if ($start_tag) {
$prefix = "\$this->_tag_stack[] = array('$obj_comp', $args); ";
$prefix .= "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], null, \$this, \$_block_repeat=true); ";
$prefix .= "while (\$_block_repeat) { ob_start();";
$return = null;
$postfix = '';
} else { } else {
// traditional argument format $prefix = "\$this->_obj_block_content = ob_get_contents(); ob_end_clean(); ";
$return = "\$this->_reg_objects['$object'][0]->$obj_comp(".implode(',', array_values($attrs)).")"; $return = "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$this->_obj_block_content, \$this, \$_block_repeat=false)";
$postfix = "} array_pop(\$this->_tag_stack);";
}
} else {
// non-block method
$return = "\$this->_reg_objects['$object'][0]->$obj_comp($args)";
} }
} else { } else {
// property // property
$return = "\$this->_reg_objects['$object'][0]->$obj_comp"; $return = "\$this->_reg_objects['$object'][0]->$obj_comp";
} }
if($tag_modifier != '') { if($return != null) {
$this->_parse_modifiers($return, $tag_modifier); if($tag_modifier != '') {
} $this->_parse_modifiers($return, $tag_modifier);
}
if($_assign_var) { if(!empty($_assign_var)) {
return "<?php \$this->assign('" . $this->_dequote($_assign_var) ."', $return); ?>\n"; $output = "\$this->assign('" . $this->_dequote($_assign_var) ."', $return);";
} else { } else {
return '<?php echo ' . $return . "; ?>\n"; $output = 'echo ' . $return . ';';
} }
} else {
$output = '';
}
return '<?php ' . $prefix . $output . $postfix . "?>\n";
} }
/** /**
* Compile {insert ...} tag * Compile {insert ...} tag
* *