@@ -32,268 +32,23 @@ public function __construct()
32
32
$ this ->template_class = 'Smarty_Custom_Template ' ;
33
33
}
34
34
35
- /**
36
- * Delete compiled template file (lazy delete if resource_name is not specified)
37
- *
38
- * @param string $resource_name template name
39
- * @param string $compile_id compile id
40
- * @param int $exp_time expiration time
41
- *
42
- * @return int number of template files deleted
43
- */
44
- public function clearCompiledTemplate ($ resource_name = null , $ compile_id = null , $ exp_time = null )
45
- {
46
- if ($ resource_name == null ) {
47
- Db::getInstance ()->execute ('REPLACE INTO ` ' ._DB_PREFIX_ .'smarty_last_flush` (`type`, `last_flush`) VALUES ( \'compile \', FROM_UNIXTIME( ' .time ().')) ' );
48
- return 0 ;
49
- } else {
50
- return parent ::clearCompiledTemplate ($ resource_name , $ compile_id , $ exp_time );
51
- }
52
- }
53
-
54
- /**
55
- * Mark all template files to be regenerated
56
- *
57
- * @param int $exp_time expiration time
58
- * @param string $type resource type
59
- *
60
- * @return int number of cache files which needs to be updated
61
- */
62
- public function clearAllCache ($ exp_time = null , $ type = null )
63
- {
64
- Db::getInstance ()->execute ('REPLACE INTO ` ' ._DB_PREFIX_ .'smarty_last_flush` (`type`, `last_flush`) VALUES ( \'template \', FROM_UNIXTIME( ' .time ().')) ' );
65
- return $ this ->delete_from_lazy_cache (null , null , null );
66
- }
67
-
68
- /**
69
- * Mark file to be regenerated for a specific template
70
- *
71
- * @param string $template_name template name
72
- * @param string $cache_id cache id
73
- * @param string $compile_id compile id
74
- * @param int $exp_time expiration time
75
- * @param string $type resource type
76
- *
77
- * @return int number of cache files which needs to be updated
78
- */
79
- public function clearCache ($ template_name , $ cache_id = null , $ compile_id = null , $ exp_time = null , $ type = null )
80
- {
81
- return $ this ->delete_from_lazy_cache ($ template_name , $ cache_id , $ compile_id );
82
- }
83
-
84
- /**
85
- * Check the compile cache needs to be invalidated (multi front + local cache compatible)
86
- */
87
- public function check_compile_cache_invalidation ()
88
- {
89
- static $ last_flush = null ;
90
- if (!file_exists ($ this ->getCompileDir ().'last_flush ' )) {
91
- @touch ($ this ->getCompileDir ().'last_flush ' , time ());
92
- } elseif (defined ('_DB_PREFIX_ ' )) {
93
- if ($ last_flush === null ) {
94
- $ sql = 'SELECT UNIX_TIMESTAMP(last_flush) as last_flush FROM ` ' ._DB_PREFIX_ .'smarty_last_flush` WHERE type= \'compile \'' ;
95
- $ last_flush = Db::getInstance ()->getValue ($ sql , false );
96
- }
97
- if ((int )$ last_flush && @filemtime ($ this ->getCompileDir ().'last_flush ' ) < $ last_flush ) {
98
- @touch ($ this ->getCompileDir ().'last_flush ' , time ());
99
- parent ::clearCompiledTemplate ();
100
- }
101
- }
102
- }
103
-
104
35
/**
105
36
* {@inheritDoc}
106
37
*/
107
38
public function fetch ($ template = null , $ cache_id = null , $ compile_id = null , $ parent = null , $ display = false , $ merge_tpl_vars = true , $ no_output_filter = false )
108
39
{
109
- $ this ->check_compile_cache_invalidation ();
110
- return parent ::fetch ($ template , $ cache_id , $ compile_id , $ parent , $ display , $ merge_tpl_vars , $ no_output_filter );
111
- }
112
-
113
- /**
114
- * {@inheritDoc}
115
- */
116
- public function createTemplate ($ template , $ cache_id = null , $ compile_id = null , $ parent = null , $ do_clone = true )
117
- {
118
- $ this ->check_compile_cache_invalidation ();
119
- if ($ this ->caching ) {
120
- $ this ->check_template_invalidation ($ template , $ cache_id , $ compile_id );
121
- return parent ::createTemplate ($ template , $ cache_id , $ compile_id , $ parent , $ do_clone );
122
- } else {
123
- return parent ::createTemplate ($ template , $ cache_id , $ compile_id , $ parent , $ do_clone );
40
+ if (($ overrideTemplate = Hook::exec ('displayOverrideTemplate ' , array ('default_template ' => $ template , 'controller ' => Context::getContext ()->controller )))
41
+ && file_exists ($ overrideTemplate )
42
+ ) {
43
+ $ template = $ overrideTemplate ;
124
44
}
125
- }
126
-
127
- /**
128
- * Handle the lazy template cache invalidation
129
- *
130
- * @param string $template template name
131
- * @param string $cache_id cache id
132
- * @param string $compile_id compile id
133
- */
134
- public function check_template_invalidation ($ template , $ cache_id , $ compile_id )
135
- {
136
- static $ last_flush = null ;
137
- if (!file_exists ($ this ->getCacheDir ().'last_template_flush ' )) {
138
- @touch ($ this ->getCacheDir ().'last_template_flush ' , time ());
139
- } elseif (defined ('_DB_PREFIX_ ' )) {
140
- if ($ last_flush === null ) {
141
- $ sql = 'SELECT UNIX_TIMESTAMP(last_flush) as last_flush FROM ` ' ._DB_PREFIX_ .'smarty_last_flush` WHERE type= \'template \'' ;
142
- $ last_flush = Db::getInstance ()->getValue ($ sql , false );
143
- }
144
-
145
- if ((int )$ last_flush && @filemtime ($ this ->getCacheDir ().'last_template_flush ' ) < $ last_flush ) {
146
- @touch ($ this ->getCacheDir ().'last_template_flush ' , time ());
147
- parent ::clearAllCache ();
148
- } else {
149
- if ($ cache_id !== null && (is_object ($ cache_id ) || is_array ($ cache_id ))) {
150
- $ cache_id = null ;
151
- }
152
45
153
- if ($ this ->is_in_lazy_cache ($ template , $ cache_id , $ compile_id ) === false ) {
154
- // insert in cache before the effective cache creation to avoid nasty race condition
155
- $ this ->insert_in_lazy_cache ($ template , $ cache_id , $ compile_id );
156
- parent ::clearCache ($ template , $ cache_id , $ compile_id );
157
- }
158
- }
46
+ $ response = parent ::fetch ($ template , $ cache_id , $ compile_id , $ parent , $ display , $ merge_tpl_vars , $ no_output_filter );
47
+ if (isset ($ this ->display_comments ) && $ this ->display_comments ) {
48
+ $ response = "\n<!-- begin $ template --> \n" .$ response ."\n<!-- end $ template --> \n" ;
159
49
}
160
- }
161
-
162
- /**
163
- * Store the cache file path
164
- *
165
- * @param string $filepath cache file path
166
- * @param string $template template name
167
- * @param string $cache_id cache id
168
- * @param string $compile_id compile id
169
- */
170
- public function update_filepath ($ filepath , $ template , $ cache_id , $ compile_id )
171
- {
172
- $ template_md5 = md5 ($ template );
173
- $ sql = 'UPDATE ` ' ._DB_PREFIX_ .'smarty_lazy_cache`
174
- SET filepath= \'' .pSQL ($ filepath ).'\'
175
- WHERE `template_hash`= \'' .pSQL ($ template_md5 ).'\'' ;
176
50
177
- $ sql .= ' AND cache_id=" ' .pSQL ((string )$ cache_id ).'" ' ;
178
-
179
- if (strlen ($ compile_id ) > 32 ) {
180
- $ compile_id = md5 ($ compile_id );
181
- }
182
- $ sql .= ' AND compile_id=" ' .pSQL ((string )$ compile_id ).'" ' ;
183
- Db::getInstance ()->execute ($ sql , false );
184
- }
185
-
186
- /**
187
- * Check if the current template is stored in the lazy cache
188
- * Entry in the lazy cache = no need to regenerate the template
189
- *
190
- * @param string $template template name
191
- * @param string $cache_id cache id
192
- * @param string $compile_id compile id
193
- *
194
- * @return bool
195
- */
196
- public function is_in_lazy_cache ($ template , $ cache_id , $ compile_id )
197
- {
198
- static $ is_in_lazy_cache = array ();
199
- $ template_md5 = md5 ($ template );
200
-
201
- if (strlen ($ compile_id ) > 32 ) {
202
- $ compile_id = md5 ($ compile_id );
203
- }
204
-
205
- $ key = md5 ($ template_md5 .'- ' .$ cache_id .'- ' .$ compile_id );
206
-
207
- if (isset ($ is_in_lazy_cache [$ key ])) {
208
- return $ is_in_lazy_cache [$ key ];
209
- } else {
210
- $ sql = 'SELECT UNIX_TIMESTAMP(last_update) as last_update, filepath FROM ` ' ._DB_PREFIX_ .'smarty_lazy_cache`
211
- WHERE `template_hash`= \'' .pSQL ($ template_md5 ).'\'' ;
212
- $ sql .= ' AND cache_id=" ' .pSQL ((string )$ cache_id ).'" ' ;
213
- $ sql .= ' AND compile_id=" ' .pSQL ((string )$ compile_id ).'" ' ;
214
-
215
- $ result = Db::getInstance ()->getRow ($ sql , false );
216
- // If the filepath is not yet set, it means the cache update is in progress in another process.
217
- // In this case do not try to clear the cache again and tell to use the existing cache, if any
218
- if ($ result !== false && $ result ['filepath ' ] == '' ) {
219
- // If the cache update is stalled for more than 1min, something should be wrong,
220
- // remove the entry from the lazy cache
221
- if ($ result ['last_update ' ] < time () - 60 ) {
222
- $ this ->delete_from_lazy_cache ($ template , $ cache_id , $ compile_id );
223
- }
224
-
225
- $ return = true ;
226
- } else {
227
- if ($ result === false
228
- || @filemtime ($ this ->getCacheDir ().$ result ['filepath ' ]) < $ result ['last_update ' ]) {
229
- $ return = false ;
230
- } else {
231
- $ return = $ result ['filepath ' ];
232
- }
233
- }
234
- $ is_in_lazy_cache [$ key ] = $ return ;
235
- }
236
- return $ return ;
237
- }
238
-
239
- /**
240
- * Insert the current template in the lazy cache
241
- *
242
- * @param string $template template name
243
- * @param string $cache_id cache id
244
- * @param string $compile_id compile id
245
- *
246
- * @return bool
247
- */
248
- public function insert_in_lazy_cache ($ template , $ cache_id , $ compile_id )
249
- {
250
- $ template_md5 = md5 ($ template );
251
- $ sql = 'INSERT IGNORE INTO ` ' ._DB_PREFIX_ .'smarty_lazy_cache`
252
- (`template_hash`, `cache_id`, `compile_id`, `last_update`)
253
- VALUES ( \'' .pSQL ($ template_md5 ).'\'' ;
254
-
255
- $ sql .= '," ' .pSQL ((string )$ cache_id ).'" ' ;
256
-
257
- if (strlen ($ compile_id ) > 32 ) {
258
- $ compile_id = md5 ($ compile_id );
259
- }
260
- $ sql .= '," ' .pSQL ((string )$ compile_id ).'" ' ;
261
- $ sql .= ', FROM_UNIXTIME( ' .time ().')) ' ;
262
-
263
- return Db::getInstance ()->execute ($ sql , false );
264
- }
265
-
266
- /**
267
- * Delete the current template from the lazy cache or the whole cache if no template name is given
268
- *
269
- * @param string $template template name
270
- * @param string $cache_id cache id
271
- * @param string $compile_id compile id
272
- *
273
- * @return bool
274
- */
275
- public function delete_from_lazy_cache ($ template , $ cache_id , $ compile_id )
276
- {
277
- if (!$ template ) {
278
- return Db::getInstance ()->execute ('TRUNCATE TABLE ` ' ._DB_PREFIX_ .'smarty_lazy_cache` ' , false );
279
- }
280
-
281
- $ template_md5 = md5 ($ template );
282
- $ sql = 'DELETE FROM ` ' ._DB_PREFIX_ .'smarty_lazy_cache`
283
- WHERE template_hash= \'' .pSQL ($ template_md5 ).'\'' ;
284
-
285
- if ($ cache_id != null ) {
286
- $ sql .= ' AND cache_id LIKE " ' .pSQL ((string )$ cache_id ).'%" ' ;
287
- }
288
-
289
- if ($ compile_id != null ) {
290
- if (strlen ($ compile_id ) > 32 ) {
291
- $ compile_id = md5 ($ compile_id );
292
- }
293
- $ sql .= ' AND compile_id=" ' .pSQL ((string )$ compile_id ).'" ' ;
294
- }
295
- Db::getInstance ()->execute ($ sql , false );
296
- return Db::getInstance ()->Affected_Rows ();
51
+ return $ response ;
297
52
}
298
53
}
299
54
@@ -304,17 +59,25 @@ class Smarty_Custom_Template extends Smarty_Internal_Template
304
59
305
60
public function fetch ($ template = null , $ cache_id = null , $ compile_id = null , $ parent = null , $ display = false , $ merge_tpl_vars = true , $ no_output_filter = false )
306
61
{
307
- if ($ this ->smarty ->caching ) {
308
- $ tpl = parent ::fetch ($ template , $ cache_id , $ compile_id , $ parent , $ display , $ merge_tpl_vars , $ no_output_filter );
309
- if (property_exists ($ this , 'cached ' )) {
310
- $ filepath = str_replace ($ this ->smarty ->getCacheDir (), '' , $ this ->cached ->filepath );
311
- if ($ this ->smarty ->is_in_lazy_cache ($ this ->template_resource , $ this ->cache_id , $ this ->compile_id ) != $ filepath ) {
312
- $ this ->smarty ->update_filepath ($ filepath , $ this ->template_resource , $ this ->cache_id , $ this ->compile_id );
313
- }
314
- }
315
- return $ tpl ;
62
+ if (!is_null ($ template )) {
63
+ $ tpl = $ template ->template_resource ;
316
64
} else {
317
- return parent ::fetch ($ template , $ cache_id , $ compile_id , $ parent , $ display , $ merge_tpl_vars , $ no_output_filter );
65
+ $ tpl = $ this ->template_resource ;
66
+ }
67
+
68
+ if (($ templatePath = Hook::exec ('displayOverrideTemplate ' , array ('default_template ' => $ tpl , 'controller ' => Context::getContext ()->controller )))
69
+ && file_exists ($ templatePath )
70
+ ) {
71
+ $ template = Context::getContext ()->smarty ->createTemplate ($ templatePath );
72
+ $ tpl = $ template ->template_resource ;
318
73
}
74
+
75
+ $ response = parent ::fetch ($ template , $ cache_id , $ compile_id , $ parent , $ display , $ merge_tpl_vars , $ no_output_filter );
76
+ if (isset ($ this ->display_comments ) && $ this ->display_comments ) {
77
+ $ response = "\n<!-- begin $ tpl --> \n" .$ response ."\n<!-- end $ tpl --> \n" ;
78
+ }
79
+
80
+ return $ response ;
319
81
}
82
+
320
83
}
0 commit comments