1<?php
2
3/**
4 * {make_nocache} Runtime Methods save(), store()
5 *
6 * @package    Smarty
7 * @subpackage PluginsInternal
8 * @author     Uwe Tews
9 */
10class Smarty_Internal_Runtime_Make_Nocache
11{
12    /**
13     * Save current variable value while rendering compiled template and inject nocache code to
14     * assign variable value in cahed template
15     *
16     * @param \Smarty_Internal_Template $tpl
17     * @param string                    $var variable name
18     *
19     * @throws \SmartyException
20     */
21    public function save(Smarty_Internal_Template $tpl, $var)
22    {
23        if (isset($tpl->tpl_vars[ $var ])) {
24            $export =
25                preg_replace('/^Smarty_Variable::__set_state[(]|[)]$/', '', var_export($tpl->tpl_vars[ $var ], true));
26            if (preg_match('/(\w+)::__set_state/', $export, $match)) {
27                throw new SmartyException("{make_nocache \${$var}} in template '{$tpl->source->name}': variable does contain object '{$match[1]}' not implementing method '__set_state'");
28            }
29            echo "/*%%SmartyNocache:{$tpl->compiled->nocache_hash}%%*/<?php " .
30                 addcslashes("\$_smarty_tpl->smarty->ext->_make_nocache->store(\$_smarty_tpl, '{$var}', ", '\\') .
31                 $export . ");?>\n/*/%%SmartyNocache:{$tpl->compiled->nocache_hash}%%*/";
32        }
33    }
34
35    /**
36     * Store variable value saved while rendering compiled template in cached template context
37     *
38     * @param \Smarty_Internal_Template $tpl
39     * @param string                    $var variable name
40     * @param array                     $properties
41     */
42    public function store(Smarty_Internal_Template $tpl, $var, $properties)
43    {
44        // do not overwrite existing nocache variables
45        if (!isset($tpl->tpl_vars[ $var ]) || !$tpl->tpl_vars[ $var ]->nocache) {
46            $newVar = new Smarty_Variable();
47            unset($properties[ 'nocache' ]);
48            foreach ($properties as $k => $v) {
49                $newVar->$k = $v;
50            }
51            $tpl->tpl_vars[ $var ] = $newVar;
52        }
53    }
54}
55