150 lines
4.0 KiB
PHP
150 lines
4.0 KiB
PHP
<?php
|
||
|
||
/*
|
||
* Smarty plugin
|
||
* -------------------------------------------------------------
|
||
* Type: function
|
||
* Name: eval
|
||
* Purpose: evaluate a template variable as a template
|
||
* -------------------------------------------------------------
|
||
*/
|
||
|
||
use KupShop\KupShopBundle\Context\LanguageContext;
|
||
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
||
|
||
function html_compress(&$html)
|
||
{
|
||
preg_match_all('!(<(?:code|pre).*>[^<]+</(?:code|pre)>)!', $html, $pre); // exclude pre or code tags
|
||
|
||
$html = preg_replace('!<(?:code|pre).*>[^<]+</(?:code|pre)>!', '#pre#', $html); // removing all pre or code tags
|
||
|
||
$html = preg_replace('#<!–[^\[].+–>#', '', $html); // removing HTML comments
|
||
|
||
$html = preg_replace('/[\r\n\t]+/', ' ', $html); // remove new lines, spaces, tabs
|
||
|
||
$html = preg_replace('/>[\s]+</', '><', $html); // remove new lines, spaces, tabs
|
||
|
||
$html = preg_replace('/[\s]+/', ' ', $html); // remove new lines, spaces, tabs
|
||
|
||
if (!empty($pre[0])) {
|
||
foreach ($pre[0] as $tag) {
|
||
$html = preg_replace('!#pre#!', $tag, $html, 1);
|
||
}
|
||
}// putting back pre|code tags
|
||
}
|
||
|
||
function smarty_function_include_cached($params, &$smarty)
|
||
{
|
||
$key = null;
|
||
$file = null;
|
||
$force = false;
|
||
$debug = false;
|
||
$skip_cache = false;
|
||
$get_data_function = null;
|
||
$last_updated = null;
|
||
|
||
extract($params);
|
||
|
||
if ($skip_cache) {
|
||
$debug = true;
|
||
}
|
||
|
||
if (empty($key)) {
|
||
trigger_error("include_cached: missing 'key' parameter");
|
||
|
||
return;
|
||
}
|
||
|
||
if (empty($file)) {
|
||
trigger_error("include_cached: missing 'file' parameter");
|
||
|
||
return;
|
||
}
|
||
|
||
$html = getCache($key);
|
||
|
||
if (is_array($html) && !empty($html['time_cached'])) {
|
||
if ($last_updated && $last_updated > $html['time_cached']) {
|
||
$html = false;
|
||
} else {
|
||
$html = $html['html'] ?? '';
|
||
}
|
||
}
|
||
|
||
if ($debug || !$html) {
|
||
ob_start();
|
||
$_smarty_tpl_vars = $smarty->tpl_vars;
|
||
|
||
if ($get_data_function) {
|
||
call_user_func_array($get_data_function, [&$params]);
|
||
}
|
||
|
||
echo $smarty->_subTemplateRender($file, $smarty->cache_id, $smarty->compile_id, 0, null, $params, 0, false);
|
||
|
||
$smarty->tpl_vars = $_smarty_tpl_vars;
|
||
|
||
unset($_smarty_tpl_vars);
|
||
|
||
$html = ob_get_contents();
|
||
|
||
if (!$debug) {
|
||
html_compress($html);
|
||
}
|
||
|
||
$cache = ['html' => $html, 'time_cached' => time()];
|
||
// Umožňujeme $get_data_function nastavit TTL, když třeba selže fetch, tak se zacachuje na krátko
|
||
setCache($key, $cache, $params['ttl'] ?? 7200);
|
||
|
||
ob_end_clean();
|
||
}
|
||
|
||
echo $html;
|
||
|
||
if ($force) {
|
||
$params['fromCache'] = true;
|
||
$_smarty_tpl_vars = $smarty->tpl_vars;
|
||
|
||
echo $smarty->_subTemplateRender($file, $smarty->cache_id, $smarty->compile_id, 0, null, $params, 0, false);
|
||
|
||
$smarty->tpl_vars = $_smarty_tpl_vars;
|
||
}
|
||
}
|
||
|
||
function smarty_function_include_cached_optional(&$params, &$smarty)
|
||
{
|
||
if (!empty($params['cache'])) {
|
||
// Use caching, generate key
|
||
$params['key'] = $params['cache_key'];
|
||
|
||
if (is_callable($params['cache_key'])) {
|
||
$params['key'] = $params['cache_key']($params);
|
||
}
|
||
|
||
if (is_array($params['key'])) {
|
||
$params['key'][] = ServiceContainer::getService(LanguageContext::class)->getActiveId();
|
||
$params['key'] = join('-', $params['key']);
|
||
}
|
||
|
||
if (empty($params['file'])) {
|
||
$params['file'] = $params['template'];
|
||
}
|
||
|
||
smarty_function_include_cached($params, $smarty);
|
||
|
||
return;
|
||
}
|
||
|
||
// Fallback without caching
|
||
if (!empty($params['get_data_function'])) {
|
||
call_user_func_array($params['get_data_function'], [&$params]);
|
||
}
|
||
|
||
$_smarty_tpl_vars = $smarty->tpl_vars;
|
||
|
||
echo $smarty->_subTemplateRender($params['template'], $smarty->cache_id, $smarty->compile_id, 0, null, $params, 0, false);
|
||
|
||
$smarty->tpl_vars = $_smarty_tpl_vars;
|
||
}
|
||
|
||
/* vim: set expandtab: */
|