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_updatescope.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Runtime Extension updateScope
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Uwe Tews
  8. *
  9. **/
  10. class Smarty_Internal_Runtime_UpdateScope
  11. {
  12. /**
  13. * Update new assigned template or config variable in other effected scopes
  14. *
  15. * @param Smarty_Internal_Template $tpl data object
  16. * @param string|null $varName variable name
  17. * @param int $tagScope tag scope to which bubble up variable value
  18. *
  19. */
  20. public function _updateScope(Smarty_Internal_Template $tpl, $varName, $tagScope = 0)
  21. {
  22. if ($tagScope) {
  23. $this->_updateVarStack($tpl, $varName);
  24. $tagScope = $tagScope & ~Smarty::SCOPE_LOCAL;
  25. if (!$tpl->scope && !$tagScope) return;
  26. }
  27. $mergedScope = $tagScope | $tpl->scope;
  28. if ($mergedScope) {
  29. if ($mergedScope & Smarty::SCOPE_GLOBAL && $varName) {
  30. Smarty::$global_tpl_vars[ $varName ] = $tpl->tpl_vars[ $varName ];
  31. }
  32. // update scopes
  33. foreach ($this->_getAffectedScopes($tpl, $mergedScope) as $ptr) {
  34. $this->_updateVariableInOtherScope($ptr->tpl_vars, $tpl, $varName);
  35. if($tagScope && $ptr->_isTplObj() && isset($tpl->_cache[ 'varStack' ])) {
  36. $this->_updateVarStack($ptr, $varName); }
  37. }
  38. }
  39. }
  40. /**
  41. * Get array of objects which needs to be updated by given scope value
  42. *
  43. * @param Smarty_Internal_Template $tpl
  44. * @param int $mergedScope merged tag and template scope to which bubble up variable value
  45. *
  46. * @return array
  47. */
  48. public function _getAffectedScopes(Smarty_Internal_Template $tpl, $mergedScope)
  49. {
  50. $_stack = array();
  51. $ptr = $tpl->parent;
  52. if ($mergedScope && isset($ptr) && $ptr->_isTplObj()) {
  53. $_stack[] = $ptr;
  54. $mergedScope = $mergedScope & ~Smarty::SCOPE_PARENT;
  55. if (!$mergedScope) {
  56. // only parent was set, we are done
  57. return $_stack;
  58. }
  59. $ptr = $ptr->parent;
  60. }
  61. while (isset($ptr) && $ptr->_isTplObj()) {
  62. $_stack[] = $ptr;
  63. $ptr = $ptr->parent;
  64. }
  65. if ($mergedScope & Smarty::SCOPE_SMARTY) {
  66. if (isset($tpl->smarty)) {
  67. $_stack[] = $tpl->smarty;
  68. }
  69. } elseif ($mergedScope & Smarty::SCOPE_ROOT) {
  70. while (isset($ptr)) {
  71. if (!$ptr->_isTplObj()) {
  72. $_stack[] = $ptr;
  73. break;
  74. }
  75. $ptr = $ptr->parent;
  76. }
  77. }
  78. return $_stack;
  79. }
  80. /**
  81. * Update variable in other scope
  82. *
  83. * @param array $tpl_vars template variable array
  84. * @param \Smarty_Internal_Template $from
  85. * @param string $varName variable name
  86. */
  87. public function _updateVariableInOtherScope(&$tpl_vars, Smarty_Internal_Template $from, $varName)
  88. {
  89. if (!isset($tpl_vars[ $varName ])) {
  90. $tpl_vars[ $varName ] = clone $from->tpl_vars[ $varName ];
  91. } else {
  92. $tpl_vars[ $varName ] = clone $tpl_vars[ $varName ];
  93. $tpl_vars[ $varName ]->value = $from->tpl_vars[ $varName ]->value;
  94. }
  95. }
  96. /**
  97. * Update variable in template local variable stack
  98. *
  99. * @param \Smarty_Internal_Template $tpl
  100. * @param string|null $varName variable name or null for config variables
  101. */
  102. public function _updateVarStack(Smarty_Internal_Template $tpl, $varName)
  103. {
  104. $i = 0;
  105. while (isset($tpl->_cache[ 'varStack' ][ $i ])) {
  106. $this->_updateVariableInOtherScope($tpl->_cache[ 'varStack' ][ $i ][ 'tpl' ], $tpl, $varName);
  107. $i ++;
  108. }
  109. }
  110. }