К новейшей редакции | Содержимое файла | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
304 | alex-w | 1 | <?php |
2 | |||
3 | /** |
||
4 | * Smarty Internal Plugin Resource Extend |
||
5 | * |
||
6 | * Implements the file system as resource for Smarty which does extend a chain of template files templates |
||
7 | * |
||
8 | * @package Smarty |
||
9 | * @subpackage TemplateResources |
||
10 | * @author Uwe Tews |
||
11 | */ |
||
12 | /** |
||
13 | * Smarty Internal Plugin Resource Extend |
||
14 | */ |
||
15 | class Smarty_Internal_Resource_Extend { |
||
16 | public function __construct($smarty) |
||
17 | { |
||
18 | $this->smarty = $smarty; |
||
19 | } |
||
20 | // classes used for compiling Smarty templates from file resource |
||
21 | public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler'; |
||
22 | public $template_lexer_class = 'Smarty_Internal_Templatelexer'; |
||
23 | public $template_parser_class = 'Smarty_Internal_Templateparser'; |
||
24 | |||
25 | /** |
||
26 | * Return flag if template source is existing |
||
27 | * |
||
28 | * @param object $template template object |
||
29 | * @return boolean result |
||
30 | */ |
||
31 | public function isExisting($template) |
||
32 | { |
||
33 | if ($template->getTemplateFilepath() === false) { |
||
34 | return false; |
||
35 | } else { |
||
36 | return true; |
||
37 | } |
||
38 | } |
||
39 | /** |
||
40 | * Get filepath to template source |
||
41 | * |
||
42 | * @param object $template template object |
||
43 | * @return string filepath to template source file |
||
44 | */ |
||
45 | public function getTemplateFilepath($template) |
||
46 | { |
||
47 | $_files = explode('|', $template->resource_name); |
||
48 | $_filepath = $template->buildTemplateFilepath ($_files[count($_files)-1]); |
||
49 | if ($_filepath !== false) { |
||
50 | if ($template->security) { |
||
51 | $template->smarty->security_handler->isTrustedResourceDir($_filepath); |
||
52 | } |
||
53 | } |
||
54 | return $_filepath; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Get timestamp to template source |
||
59 | * |
||
60 | * @param object $template template object |
||
61 | * @return integer timestamp of template source file |
||
62 | */ |
||
63 | public function getTemplateTimestamp($template) |
||
64 | { |
||
65 | return filemtime($template->getTemplateFilepath()); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Read template source from file |
||
70 | * |
||
71 | * @param object $template template object |
||
72 | * @return string content of template source file |
||
73 | */ |
||
74 | public function getTemplateSource($template) |
||
75 | { |
||
76 | $this->template = $template; |
||
77 | $_files = explode('|', $template->resource_name); |
||
78 | $_files = array_reverse($_files); |
||
79 | foreach ($_files as $_file) { |
||
80 | $_filepath = $template->buildTemplateFilepath ($_file); |
||
81 | // read template file |
||
82 | if ($_filepath === false) { |
||
83 | throw new Exception("Unable to load template \"file : {$_file}\""); |
||
84 | } |
||
85 | if ($_file != $_files[0]) { |
||
86 | $template->properties['file_dependency']['F' . abs(crc32($_filepath))] = array($_filepath, filemtime($_filepath)); |
||
87 | } |
||
88 | $_content = file_get_contents($_filepath); |
||
89 | if ($_file != $_files[count($_files)-1]) { |
||
90 | if (preg_match_all('/(' . $this->smarty->left_delimiter . 'block(.+?)' . $this->smarty->right_delimiter . ')/', $_content, $_open, PREG_OFFSET_CAPTURE) != |
||
91 | preg_match_all('/(' . $this->smarty->left_delimiter . '\/block(.*?)' . $this->smarty->right_delimiter . ')/', $_content, $_close, PREG_OFFSET_CAPTURE)) { |
||
92 | $this->smarty->trigger_error(" unmatched {block} {/block} pairs"); |
||
93 | } |
||
94 | $_block_count = count($_open[0]); |
||
95 | for ($_i = 0; $_i < $_block_count; $_i++) { |
||
96 | $_block_content = str_replace($this->smarty->left_delimiter . '$smarty.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%', |
||
97 | substr($_content, $_open[0][$_i][1] + strlen($_open[0][$_i][0]), $_close[0][$_i][1] - $_open[0][$_i][1] - strlen($_open[0][$_i][0]))); |
||
98 | $this->saveBlockData($_block_content, $_open[0][$_i][0]); |
||
99 | } |
||
100 | } else { |
||
101 | $template->template_source = $_content; |
||
102 | return true; |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | protected function saveBlockData($block_content, $block_tag) |
||
107 | { |
||
108 | if (0 == preg_match('/(.?)(name=)([^ ]*)/', $block_tag, $_match)) { |
||
109 | $this->smarty->trigger_error("\"" . $block_tag . "\" missing name attribute"); |
||
110 | } else { |
||
111 | // compile block content |
||
112 | $_tpl = $this->smarty->createTemplate('string:' . $block_content); |
||
113 | $_tpl->template_filepath = $this->template->getTemplateFilepath(); |
||
114 | $_tpl->suppressHeader = true; |
||
115 | $_compiled_content = $_tpl->getCompiledTemplate(); |
||
116 | unset($_tpl); |
||
117 | $_name = trim($_match[3], "\"'}"); |
||
118 | |||
119 | if (isset($this->template->block_data[$_name])) { |
||
120 | if (strpos($this->template->block_data[$_name]['compiled'], '%%%%SMARTY_PARENT%%%%') !== false) { |
||
121 | $this->template->block_data[$_name]['compiled'] = |
||
122 | str_replace('%%%%SMARTY_PARENT%%%%', $_compiled_content, $this->template->block_data[$_name]['compiled']); |
||
123 | } elseif ($this->template->block_data[$_name]['mode'] == 'prepend') { |
||
124 | $this->template->block_data[$_name]['compiled'] .= $_compiled_content; |
||
125 | } elseif ($this->template->block_data[$_name]['mode'] == 'append') { |
||
126 | $this->template->block_data[$_name]['compiled'] = $_compiled_content . $this->template->block_data[$_name]['compiled']; |
||
127 | } |
||
128 | } else { |
||
129 | $this->template->block_data[$_name]['compiled'] = $_compiled_content; |
||
130 | } |
||
131 | if (preg_match('/(.?)(append=true)(.*)/', $block_tag, $_match) != 0) { |
||
132 | $this->template->block_data[$_name]['mode'] = 'append'; |
||
133 | } elseif (preg_match('/(.?)(prepend=true)(.*)/', $block_tag, $_match) != 0) { |
||
134 | $this->template->block_data[$_name]['mode'] = 'prepend'; |
||
135 | } else { |
||
136 | $this->template->block_data[$_name]['mode'] = 'replace'; |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Return flag that this resource uses the compiler |
||
143 | * |
||
144 | * @return boolean true |
||
145 | */ |
||
146 | public function usesCompiler() |
||
147 | { |
||
148 | // template has tags, uses compiler |
||
149 | return true; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Return flag that this is not evaluated |
||
154 | * |
||
155 | * @return boolean false |
||
156 | */ |
||
157 | public function isEvaluated() |
||
158 | { |
||
159 | // save the compiled file to disk, do not evaluate |
||
160 | return false; |
||
161 | } |
||
162 | /** |
||
163 | * Get filepath to compiled template |
||
164 | * |
||
165 | * @param object $template template object |
||
166 | * @return string return path to compiled template |
||
167 | */ |
||
168 | public function getCompiledFilepath($template) |
||
169 | { |
||
170 | $_files = explode('|', $template->resource_name); |
||
171 | $_filepath = (string)abs(crc32($template->resource_name)); |
||
172 | // if use_sub_dirs, break file into directories |
||
173 | if ($template->smarty->use_sub_dirs) { |
||
174 | $_filepath = substr($_filepath, 0, 3) . DS |
||
175 | . substr($_filepath, 0, 2) . DS |
||
176 | . substr($_filepath, 0, 1) . DS |
||
177 | . $_filepath; |
||
178 | } |
||
179 | $_compile_dir_sep = $template->smarty->use_sub_dirs ? DS : '^'; |
||
180 | if (isset($template->compile_id)) { |
||
181 | $_filepath = $template->compile_id . $_compile_dir_sep . $_filepath; |
||
182 | } |
||
183 | if ($template->caching) { |
||
184 | $_cache = '.cache'; |
||
185 | } else { |
||
186 | $_cache = ''; |
||
187 | } |
||
188 | $_compile_dir = $template->smarty->compile_dir; |
||
189 | if (substr($_compile_dir, -1) != DS) { |
||
190 | $_compile_dir .= DS; |
||
191 | } |
||
192 | return $_compile_dir . $_filepath . '.' . basename($_files[count($_files)-1]) . $_cache . $template->smarty->php_ext; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | ?> |