You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_runtime_make_nocache.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * {make_nocache} Runtime Methods save(), store()
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Uwe Tews
  8. *
  9. */
  10. class 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. * Store variable value saved while rendering compiled template in cached template context
  36. *
  37. * @param \Smarty_Internal_Template $tpl
  38. * @param string $var variable name
  39. * @param array $properties
  40. */
  41. public function store(Smarty_Internal_Template $tpl, $var, $properties)
  42. {
  43. // do not overwrite existing nocache variables
  44. if (!isset($tpl->tpl_vars[ $var ]) || !$tpl->tpl_vars[ $var ]->nocache) {
  45. $newVar = new Smarty_Variable();
  46. unset($properties[ 'nocache' ]);
  47. foreach ($properties as $k => $v) {
  48. $newVar->$k = $v;
  49. }
  50. $tpl->tpl_vars[ $var ] = $newVar;
  51. }
  52. }
  53. }