1 | <?php |
---|
2 | /** |
---|
3 | * Smarty Internal Plugin Config |
---|
4 | * |
---|
5 | * @package Smarty |
---|
6 | * @subpackage Config |
---|
7 | * @author Uwe Tews |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Smarty Internal Plugin Config |
---|
12 | * |
---|
13 | * Main class for config variables |
---|
14 | * |
---|
15 | * @package Smarty |
---|
16 | * @subpackage Config |
---|
17 | * |
---|
18 | * @property Smarty_Config_Source $source |
---|
19 | * @property Smarty_Config_Compiled $compiled |
---|
20 | * @ignore |
---|
21 | */ |
---|
22 | class Smarty_Internal_Config { |
---|
23 | |
---|
24 | /** |
---|
25 | * Samrty instance |
---|
26 | * |
---|
27 | * @var Smarty object |
---|
28 | */ |
---|
29 | public $smarty = null; |
---|
30 | /** |
---|
31 | * Object of config var storage |
---|
32 | * |
---|
33 | * @var object |
---|
34 | */ |
---|
35 | public $data = null; |
---|
36 | /** |
---|
37 | * Config resource |
---|
38 | * @var string |
---|
39 | */ |
---|
40 | public $config_resource = null; |
---|
41 | /** |
---|
42 | * Compiled config file |
---|
43 | * |
---|
44 | * @var string |
---|
45 | */ |
---|
46 | public $compiled_config = null; |
---|
47 | /** |
---|
48 | * filepath of compiled config file |
---|
49 | * |
---|
50 | * @var string |
---|
51 | */ |
---|
52 | public $compiled_filepath = null; |
---|
53 | /** |
---|
54 | * Filemtime of compiled config Filemtime |
---|
55 | * |
---|
56 | * @var int |
---|
57 | */ |
---|
58 | public $compiled_timestamp = null; |
---|
59 | /** |
---|
60 | * flag if compiled config file is invalid and must be (re)compiled |
---|
61 | * @var bool |
---|
62 | */ |
---|
63 | public $mustCompile = null; |
---|
64 | /** |
---|
65 | * Config file compiler object |
---|
66 | * |
---|
67 | * @var Smarty_Internal_Config_File_Compiler object |
---|
68 | */ |
---|
69 | public $compiler_object = null; |
---|
70 | |
---|
71 | /** |
---|
72 | * Constructor of config file object |
---|
73 | * |
---|
74 | * @param string $config_resource config file resource name |
---|
75 | * @param Smarty $smarty Smarty instance |
---|
76 | * @param object $data object for config vars storage |
---|
77 | */ |
---|
78 | public function __construct($config_resource, $smarty, $data = null) |
---|
79 | { |
---|
80 | $this->data = $data; |
---|
81 | $this->smarty = $smarty; |
---|
82 | $this->config_resource = $config_resource; |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Returns the compiled filepath |
---|
87 | * |
---|
88 | * @return string the compiled filepath |
---|
89 | */ |
---|
90 | public function getCompiledFilepath() |
---|
91 | { |
---|
92 | return $this->compiled_filepath === null ? |
---|
93 | ($this->compiled_filepath = $this->buildCompiledFilepath()) : |
---|
94 | $this->compiled_filepath; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Get file path. |
---|
99 | * |
---|
100 | * @return string |
---|
101 | */ |
---|
102 | public function buildCompiledFilepath() |
---|
103 | { |
---|
104 | $_compile_id = isset($this->smarty->compile_id) ? preg_replace('![^\w\|]+!', '_', $this->smarty->compile_id) : null; |
---|
105 | $_flag = (int) $this->smarty->config_read_hidden + (int) $this->smarty->config_booleanize * 2 |
---|
106 | + (int) $this->smarty->config_overwrite * 4; |
---|
107 | $_filepath = sha1($this->source->name . $_flag); |
---|
108 | // if use_sub_dirs, break file into directories |
---|
109 | if ($this->smarty->use_sub_dirs) { |
---|
110 | $_filepath = substr($_filepath, 0, 2) . DS |
---|
111 | . substr($_filepath, 2, 2) . DS |
---|
112 | . substr($_filepath, 4, 2) . DS |
---|
113 | . $_filepath; |
---|
114 | } |
---|
115 | $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^'; |
---|
116 | if (isset($_compile_id)) { |
---|
117 | $_filepath = $_compile_id . $_compile_dir_sep . $_filepath; |
---|
118 | } |
---|
119 | $_compile_dir = $this->smarty->getCompileDir(); |
---|
120 | return $_compile_dir . $_filepath . '.' . basename($this->source->name) . '.config' . '.php'; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Returns the timpestamp of the compiled file |
---|
125 | * |
---|
126 | * @return integer the file timestamp |
---|
127 | */ |
---|
128 | public function getCompiledTimestamp() |
---|
129 | { |
---|
130 | return $this->compiled_timestamp === null |
---|
131 | ? ($this->compiled_timestamp = (file_exists($this->getCompiledFilepath())) ? filemtime($this->getCompiledFilepath()) : false) |
---|
132 | : $this->compiled_timestamp; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Returns if the current config file must be compiled |
---|
137 | * |
---|
138 | * It does compare the timestamps of config source and the compiled config and checks the force compile configuration |
---|
139 | * |
---|
140 | * @return boolean true if the file must be compiled |
---|
141 | */ |
---|
142 | public function mustCompile() |
---|
143 | { |
---|
144 | return $this->mustCompile === null ? |
---|
145 | $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp () === false || $this->smarty->compile_check && $this->getCompiledTimestamp () < $this->source->timestamp): |
---|
146 | $this->mustCompile; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Returns the compiled config file |
---|
151 | * |
---|
152 | * It checks if the config file must be compiled or just read the compiled version |
---|
153 | * |
---|
154 | * @return string the compiled config file |
---|
155 | */ |
---|
156 | public function getCompiledConfig() |
---|
157 | { |
---|
158 | if ($this->compiled_config === null) { |
---|
159 | // see if template needs compiling. |
---|
160 | if ($this->mustCompile()) { |
---|
161 | $this->compileConfigSource(); |
---|
162 | } else { |
---|
163 | $this->compiled_config = file_get_contents($this->getCompiledFilepath()); |
---|
164 | } |
---|
165 | } |
---|
166 | return $this->compiled_config; |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Compiles the config files |
---|
171 | * |
---|
172 | * @throws Exception |
---|
173 | */ |
---|
174 | public function compileConfigSource() |
---|
175 | { |
---|
176 | // compile template |
---|
177 | if (!is_object($this->compiler_object)) { |
---|
178 | // load compiler |
---|
179 | $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty); |
---|
180 | } |
---|
181 | // compile locking |
---|
182 | if ($this->smarty->compile_locking) { |
---|
183 | if ($saved_timestamp = $this->getCompiledTimestamp()) { |
---|
184 | touch($this->getCompiledFilepath()); |
---|
185 | } |
---|
186 | } |
---|
187 | // call compiler |
---|
188 | try { |
---|
189 | $this->compiler_object->compileSource($this); |
---|
190 | } catch (Exception $e) { |
---|
191 | // restore old timestamp in case of error |
---|
192 | if ($this->smarty->compile_locking && $saved_timestamp) { |
---|
193 | touch($this->getCompiledFilepath(), $saved_timestamp); |
---|
194 | } |
---|
195 | throw $e; |
---|
196 | } |
---|
197 | // compiling succeded |
---|
198 | // write compiled template |
---|
199 | Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * load config variables |
---|
204 | * |
---|
205 | * @param mixed $sections array of section names, single section or null |
---|
206 | * @param object $scope global,parent or local |
---|
207 | */ |
---|
208 | public function loadConfigVars($sections = null, $scope = 'local') |
---|
209 | { |
---|
210 | if ($this->data instanceof Smarty_Internal_Template) { |
---|
211 | $this->data->properties['file_dependency'][sha1($this->source->filepath)] = array($this->source->filepath, $this->source->timestamp, 'file'); |
---|
212 | } |
---|
213 | if ($this->mustCompile()) { |
---|
214 | $this->compileConfigSource(); |
---|
215 | } |
---|
216 | // pointer to scope |
---|
217 | if ($scope == 'local') { |
---|
218 | $scope_ptr = $this->data; |
---|
219 | } elseif ($scope == 'parent') { |
---|
220 | if (isset($this->data->parent)) { |
---|
221 | $scope_ptr = $this->data->parent; |
---|
222 | } else { |
---|
223 | $scope_ptr = $this->data; |
---|
224 | } |
---|
225 | } elseif ($scope == 'root' || $scope == 'global') { |
---|
226 | $scope_ptr = $this->data; |
---|
227 | while (isset($scope_ptr->parent)) { |
---|
228 | $scope_ptr = $scope_ptr->parent; |
---|
229 | } |
---|
230 | } |
---|
231 | $_config_vars = array(); |
---|
232 | include($this->getCompiledFilepath()); |
---|
233 | // copy global config vars |
---|
234 | foreach ($_config_vars['vars'] as $variable => $value) { |
---|
235 | if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) { |
---|
236 | $scope_ptr->config_vars[$variable] = $value; |
---|
237 | } else { |
---|
238 | $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value); |
---|
239 | } |
---|
240 | } |
---|
241 | // scan sections |
---|
242 | if (!empty($sections)) { |
---|
243 | $sections = array_flip((array) $sections); |
---|
244 | foreach ($_config_vars['sections'] as $this_section => $dummy) { |
---|
245 | if (isset($sections[$this_section])) { |
---|
246 | foreach ($_config_vars['sections'][$this_section]['vars'] as $variable => $value) { |
---|
247 | if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) { |
---|
248 | $scope_ptr->config_vars[$variable] = $value; |
---|
249 | } else { |
---|
250 | $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value); |
---|
251 | } |
---|
252 | } |
---|
253 | } |
---|
254 | } |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | * set Smarty property in template context |
---|
260 | * |
---|
261 | * @param string $property_name property name |
---|
262 | * @param mixed $value value |
---|
263 | * @throws SmartyException if $property_name is not valid |
---|
264 | */ |
---|
265 | public function __set($property_name, $value) |
---|
266 | { |
---|
267 | switch ($property_name) { |
---|
268 | case 'source': |
---|
269 | case 'compiled': |
---|
270 | $this->$property_name = $value; |
---|
271 | return; |
---|
272 | } |
---|
273 | |
---|
274 | throw new SmartyException("invalid config property '$property_name'."); |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * get Smarty property in template context |
---|
279 | * |
---|
280 | * @param string $property_name property name |
---|
281 | * @throws SmartyException if $property_name is not valid |
---|
282 | */ |
---|
283 | public function __get($property_name) |
---|
284 | { |
---|
285 | switch ($property_name) { |
---|
286 | case 'source': |
---|
287 | if (empty($this->config_resource)) { |
---|
288 | throw new SmartyException("Unable to parse resource name \"{$this->config_resource}\""); |
---|
289 | } |
---|
290 | $this->source = Smarty_Resource::config($this); |
---|
291 | return $this->source; |
---|
292 | |
---|
293 | case 'compiled': |
---|
294 | $this->compiled = $this->source->getCompiled($this); |
---|
295 | return $this->compiled; |
---|
296 | } |
---|
297 | |
---|
298 | throw new SmartyException("config attribute '$property_name' does not exist."); |
---|
299 | } |
---|
300 | |
---|
301 | } |
---|
302 | |
---|
303 | ?> |
---|