Skip to content

Commit 8c240df

Browse files
authored
Merge pull request #1195 from vishal-singh-webkul/gli-2230_2
Added: The smarty template overriding feature.
2 parents 346061d + 8989394 commit 8c240df

File tree

70 files changed

+9537
-8808
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+9537
-8808
lines changed

classes/Smarty/SmartyCustom.php

+26-263
Original file line numberDiff line numberDiff line change
@@ -32,268 +32,23 @@ public function __construct()
3232
$this->template_class = 'Smarty_Custom_Template';
3333
}
3434

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-
10435
/**
10536
* {@inheritDoc}
10637
*/
10738
public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false)
10839
{
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;
12444
}
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-
}
15245

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";
15949
}
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).'\'';
17650

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;
29752
}
29853
}
29954

@@ -304,17 +59,25 @@ class Smarty_Custom_Template extends Smarty_Internal_Template
30459

30560
public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false)
30661
{
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;
31664
} 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;
31873
}
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;
31981
}
82+
32083
}

classes/Smarty/SmartyDev.php

+5-23
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,22 @@
2424
* International Registered Trademark & Property of PrestaShop SA
2525
*/
2626

27-
class SmartyDev extends Smarty
27+
class SmartyDev extends SmartyCustom
2828
{
29+
public $display_comments = true;
30+
2931
public function __construct()
3032
{
3133
parent::__construct();
3234
$this->template_class = 'Smarty_Dev_Template';
3335
}
3436

35-
/**
36-
* {@inheritDoc}
37-
*/
38-
public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false)
39-
{
40-
return "\n<!-- begin $template -->\n"
41-
.parent::fetch($template, $cache_id, $compile_id, $parent, $display, $merge_tpl_vars, $no_output_filter)
42-
."\n<!-- end $template -->\n";
43-
}
4437
}
4538

46-
class Smarty_Dev_Template extends Smarty_Internal_Template
39+
class Smarty_Dev_Template extends Smarty_Custom_Template
4740
{
4841
/** @var SmartyCustom|null */
4942
public $smarty = null;
43+
public $display_comments = true;
5044

51-
public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false)
52-
{
53-
if (!is_null($template)) {
54-
$tpl = $template->template_resource;
55-
} else {
56-
$tpl = $this->template_resource;
57-
}
58-
59-
return "\n<!-- begin $tpl -->\n"
60-
.parent::fetch($template, $cache_id, $compile_id, $parent, $display, $merge_tpl_vars, $no_output_filter)
61-
."\n<!-- end $tpl -->\n";
62-
}
6345
}

classes/controller/FrontController.php

+1-19
Original file line numberDiff line numberDiff line change
@@ -1624,28 +1624,10 @@ public function setTemplate($default_template)
16241624
if ($this->useMobileTheme()) {
16251625
$this->setMobileTemplate($default_template);
16261626
} else {
1627-
$template = $this->getOverrideTemplate();
1628-
if ($template) {
1629-
parent::setTemplate($template);
1630-
} else {
1631-
parent::setTemplate($default_template);
1632-
}
1627+
parent::setTemplate($default_template);
16331628
}
16341629
}
16351630

1636-
/**
1637-
* Returns an overridden template path (if any) for this controller.
1638-
* If not overridden, will return false. This method can be easily overriden in a
1639-
* specific controller.
1640-
*
1641-
* @since 1.5.0.13
1642-
* @return string|bool
1643-
*/
1644-
public function getOverrideTemplate()
1645-
{
1646-
return Hook::exec('DisplayOverrideTemplate', array('controller' => $this));
1647-
}
1648-
16491631
/**
16501632
* Checks if mobile theme is active and in use.
16511633
*

config/smarty.config.inc.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
} elseif (_PS_MODE_DEV_ && !defined('_PS_ADMIN_DIR_')) {
3535
$smarty = new SmartyDev();
3636
} else {
37-
$smarty = new Smarty();
37+
$smarty = new SmartyCustom();
3838
}
3939

4040
$smarty->setCompileDir(_PS_CACHE_DIR_.'smarty/compile');

0 commit comments

Comments
 (0)