1: <?php
2: /**
3: * Smarty Internal Plugin Compile Block Plugin
4: * Compiles code for the execution of block plugin
5: *
6: * @package Smarty
7: * @subpackage Compiler
8: * @author Uwe Tews
9: */
10:
11: /**
12: * Smarty Internal Plugin Compile Block Plugin Class
13: *
14: * @package Smarty
15: * @subpackage Compiler
16: */
17: class Smarty_Internal_Compile_Private_Block_Plugin extends Smarty_Internal_CompileBase
18: {
19: /**
20: * Attribute definition: Overwrites base class.
21: *
22: * @var array
23: * @see Smarty_Internal_CompileBase
24: */
25: public $optional_attributes = array('_any');
26:
27: /**
28: * nesting level
29: *
30: * @var int
31: */
32: public $nesting = 0;
33:
34: /**
35: * Compiles code for the execution of block plugin
36: *
37: * @param array $args array with attributes from parser
38: * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
39: * @param array $parameter array with compilation parameter
40: * @param string $tag name of block plugin
41: * @param string $function PHP function name
42: *
43: * @return string compiled code
44: * @throws \SmartyCompilerException
45: * @throws \SmartyException
46: */
47: public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter, $tag, $function = null)
48: {
49: if (!isset($tag[ 5 ]) || substr($tag, -5) !== 'close') {
50: // opening tag of block plugin
51: // check and get attributes
52: $_attr = $this->getAttributes($compiler, $args);
53: $this->nesting++;
54: unset($_attr[ 'nocache' ]);
55: list($callback, $_paramsArray, $callable) = $this->setup($compiler, $_attr, $tag, $function);
56: $_params = 'array(' . implode(',', $_paramsArray) . ')';
57: // compile code
58: $output = "<?php ";
59: if (is_array($callback)) {
60: $output .= "\$_block_plugin{$this->nesting} = isset({$callback[0]}) ? {$callback[0]} : null;\n";
61: $callback = "\$_block_plugin{$this->nesting}{$callback[1]}";
62: }
63: if (isset($callable)) {
64: $output .= "if (!is_callable({$callable})) {\nthrow new SmartyException('block tag \'{$tag}\' not callable or registered');\n}\n";
65: }
66: $output .= "\$_smarty_tpl->smarty->_cache['_tag_stack'][] = array('{$tag}', {$_params});\n";
67: $output .= "\$_block_repeat=true;\necho {$callback}({$_params}, null, \$_smarty_tpl, \$_block_repeat);\nwhile (\$_block_repeat) {\nob_start();?>";
68: $this->openTag($compiler, $tag, array($_params, $compiler->nocache, $callback));
69: // maybe nocache because of nocache variables or nocache plugin
70: $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
71: } else {
72: // must endblock be nocache?
73: if ($compiler->nocache) {
74: $compiler->tag_nocache = true;
75: }
76: // closing tag of block plugin, restore nocache
77: list($_params, $compiler->nocache, $callback) = $this->closeTag($compiler, substr($tag, 0, -5));
78: // compile code
79: if (!isset($parameter[ 'modifier_list' ])) {
80: $mod_pre = $mod_post = $mod_content = '';
81: $mod_content2 = 'ob_get_clean()';
82: } else {
83: $mod_content2 = "\$_block_content{$this->nesting}";
84: $mod_content = "\$_block_content{$this->nesting} = ob_get_clean();\n";
85: $mod_pre = "ob_start();\n";
86: $mod_post = 'echo ' . $compiler->compileTag(
87: 'private_modifier',
88: array(),
89: array(
90: 'modifierlist' => $parameter[ 'modifier_list' ],
91: 'value' => 'ob_get_clean()'
92: )
93: ) . ";\n";
94: }
95: $output =
96: "<?php {$mod_content}\$_block_repeat=false;\n{$mod_pre}echo {$callback}({$_params}, {$mod_content2}, \$_smarty_tpl, \$_block_repeat);\n{$mod_post}}\n";
97: $output .= 'array_pop($_smarty_tpl->smarty->_cache[\'_tag_stack\']);?>';
98: }
99: return $output;
100: }
101:
102: /**
103: * Setup callback and parameter array
104: *
105: * @param \Smarty_Internal_TemplateCompilerBase $compiler
106: * @param array $_attr attributes
107: * @param string $tag
108: * @param string $function
109: *
110: * @return array
111: */
112: public function setup(Smarty_Internal_TemplateCompilerBase $compiler, $_attr, $tag, $function)
113: {
114: $_paramsArray = array();
115: foreach ($_attr as $_key => $_value) {
116: if (is_int($_key)) {
117: $_paramsArray[] = "$_key=>$_value";
118: } else {
119: $_paramsArray[] = "'$_key'=>$_value";
120: }
121: }
122: return array($function, $_paramsArray, null);
123: }
124: }
125: