1: <?php
2: /**
3: * Smarty Internal Plugin Templateparser Parse Tree
4: * These are classes to build parse tree in the template parser
5: *
6: * @package Smarty
7: * @subpackage Compiler
8: * @author Thue Kristensen
9: * @author Uwe Tews
10: */
11:
12: /**
13: * Template element
14: *
15: * @package Smarty
16: * @subpackage Compiler
17: * @ignore
18: */
19: class Smarty_Internal_ParseTree_Template extends Smarty_Internal_ParseTree
20: {
21: /**
22: * Array of template elements
23: *
24: * @var array
25: */
26: public $subtrees = array();
27:
28: /**
29: * Create root of parse tree for template elements
30: */
31: public function __construct()
32: {
33: }
34:
35: /**
36: * Append buffer to subtree
37: *
38: * @param \Smarty_Internal_Templateparser $parser
39: * @param Smarty_Internal_ParseTree $subtree
40: */
41: public function append_subtree(Smarty_Internal_Templateparser $parser, Smarty_Internal_ParseTree $subtree)
42: {
43: if (!empty($subtree->subtrees)) {
44: $this->subtrees = array_merge($this->subtrees, $subtree->subtrees);
45: } else {
46: if ($subtree->data !== '') {
47: $this->subtrees[] = $subtree;
48: }
49: }
50: }
51:
52: /**
53: * Append array to subtree
54: *
55: * @param \Smarty_Internal_Templateparser $parser
56: * @param \Smarty_Internal_ParseTree[] $array
57: */
58: public function append_array(Smarty_Internal_Templateparser $parser, $array = array())
59: {
60: if (!empty($array)) {
61: $this->subtrees = array_merge($this->subtrees, (array)$array);
62: }
63: }
64:
65: /**
66: * Prepend array to subtree
67: *
68: * @param \Smarty_Internal_Templateparser $parser
69: * @param \Smarty_Internal_ParseTree[] $array
70: */
71: public function prepend_array(Smarty_Internal_Templateparser $parser, $array = array())
72: {
73: if (!empty($array)) {
74: $this->subtrees = array_merge((array)$array, $this->subtrees);
75: }
76: }
77:
78: /**
79: * Sanitize and merge subtree buffers together
80: *
81: * @param \Smarty_Internal_Templateparser $parser
82: *
83: * @return string template code content
84: */
85: public function to_smarty_php(Smarty_Internal_Templateparser $parser)
86: {
87: $code = '';
88:
89: foreach ($this->getChunkedSubtrees() as $chunk) {
90: $text = '';
91: switch ($chunk['mode']) {
92: case 'textstripped':
93: foreach ($chunk['subtrees'] as $subtree) {
94: $text .= $subtree->to_smarty_php($parser);
95: }
96: $code .= preg_replace(
97: '/((<%)|(%>)|(<\?php)|(<\?)|(\?>)|(<\/?script))/',
98: "<?php echo '\$1'; ?>\n",
99: $parser->compiler->processText($text)
100: );
101: break;
102: case 'text':
103: foreach ($chunk['subtrees'] as $subtree) {
104: $text .= $subtree->to_smarty_php($parser);
105: }
106: $code .= preg_replace(
107: '/((<%)|(%>)|(<\?php)|(<\?)|(\?>)|(<\/?script))/',
108: "<?php echo '\$1'; ?>\n",
109: $text
110: );
111: break;
112: case 'tag':
113: foreach ($chunk['subtrees'] as $subtree) {
114: $text = $parser->compiler->appendCode($text, $subtree->to_smarty_php($parser));
115: }
116: $code .= $text;
117: break;
118: default:
119: foreach ($chunk['subtrees'] as $subtree) {
120: $text = $subtree->to_smarty_php($parser);
121: }
122: $code .= $text;
123:
124: }
125: }
126: return $code;
127: }
128:
129: private function getChunkedSubtrees() {
130: $chunks = array();
131: $currentMode = null;
132: $currentChunk = array();
133: for ($key = 0, $cnt = count($this->subtrees); $key < $cnt; $key++) {
134:
135: if ($this->subtrees[ $key ]->data === '' && in_array($currentMode, array('textstripped', 'text', 'tag'))) {
136: continue;
137: }
138:
139: if ($this->subtrees[ $key ] instanceof Smarty_Internal_ParseTree_Text
140: && $this->subtrees[ $key ]->isToBeStripped()) {
141: $newMode = 'textstripped';
142: } elseif ($this->subtrees[ $key ] instanceof Smarty_Internal_ParseTree_Text) {
143: $newMode = 'text';
144: } elseif ($this->subtrees[ $key ] instanceof Smarty_Internal_ParseTree_Tag) {
145: $newMode = 'tag';
146: } else {
147: $newMode = 'other';
148: }
149:
150: if ($newMode == $currentMode) {
151: $currentChunk[] = $this->subtrees[ $key ];
152: } else {
153: $chunks[] = array(
154: 'mode' => $currentMode,
155: 'subtrees' => $currentChunk
156: );
157: $currentMode = $newMode;
158: $currentChunk = array($this->subtrees[ $key ]);
159: }
160: }
161: if ($currentMode && $currentChunk) {
162: $chunks[] = array(
163: 'mode' => $currentMode,
164: 'subtrees' => $currentChunk
165: );
166: }
167: return $chunks;
168: }
169: }
170: