Rewrote global static global_tpl_vars to getters/setters on Smarty class, fixing several test cases. Added a ::getValue() method to Variable.

This commit is contained in:
Simon Wisselink
2023-01-09 09:55:22 +01:00
parent c5de83b09f
commit 34d5d6d024
10 changed files with 80 additions and 136 deletions

View File

@@ -155,12 +155,7 @@ abstract class Data
public function assignGlobal($varName, $value = null, $nocache = false)
{
if ($varName !== '') {
Smarty::$global_tpl_vars[ $varName ] = new \Smarty\Variable($value, $nocache);
$ptr = $this;
while ($ptr->_isTplObj()) {
$ptr->tpl_vars[ $varName ] = clone Smarty::$global_tpl_vars[ $varName ];
$ptr = $ptr->parent;
}
$this->_getSmartyObj()->setGlobalVariable($varName, new \Smarty\Variable($value, $nocache));
}
return $this;
}
@@ -256,8 +251,8 @@ abstract class Data
$_ptr = null;
}
}
if ($searchParents && isset(Smarty::$global_tpl_vars)) {
foreach (Smarty::$global_tpl_vars as $key => $var) {
if ($searchParents) {
foreach ($this->_getSmartyObj()->getAllGlobalTemplateVars() as $key => $var) {
if (!array_key_exists($key, $_result)) {
$_result[ $key ] = $var->value;
}
@@ -298,9 +293,9 @@ abstract class Data
$_ptr = null;
}
}
if (isset(Smarty::$global_tpl_vars[ $varName ])) {
if ($this->_getSmartyObj()->getGlobalVariable($varName)) {
// found it, return it
return Smarty::$global_tpl_vars[ $varName ];
return $this->_getSmartyObj()->getGlobalVariable($varName);
}
if ($errorEnable && $this->_getSmartyObj()->error_unassigned) {
// force a notice
@@ -542,14 +537,14 @@ abstract class Data
public function getGlobal($varName = null)
{
if (isset($varName)) {
if (isset(Smarty::$global_tpl_vars[ $varName ])) {
return Smarty::$global_tpl_vars[ $varName ]->value;
if ($this->_getSmartyObj()->getGlobalVariable($varName)) {
return $this->_getSmartyObj()->getGlobalVariable($varName)->getValue();
} else {
return '';
}
} else {
$_result = array();
foreach (Smarty::$global_tpl_vars as $key => $var) {
$_result = [];
foreach ($this->_getSmartyObj()->getAllGlobalTemplateVars() as $key => $var) {
$_result[ $key ] = $var->value;
}
return $_result;

View File

@@ -308,7 +308,7 @@ class Debug extends Data
}
$config_vars = array_merge($parent->config_vars, $config_vars);
} else {
foreach (\Smarty\Smarty::$global_tpl_vars as $key => $var) {
foreach ($this->_getSmartyObj()->getAllGlobalTemplateVars() as $key => $var) {
if (!array_key_exists($key, $tpl_vars)) {
foreach ($var as $varkey => $varvalue) {
if ($varkey === 'value') {

View File

@@ -96,7 +96,7 @@ class Smarty extends \Smarty\TemplateBase
/**
* assigned global tpl vars
*/
public static $global_tpl_vars = array();
private $global_tpl_vars = [];
/**
* The character set to adhere to (defaults to "UTF-8")
@@ -563,15 +563,11 @@ class Smarty extends \Smarty\TemplateBase
*/
public function __construct()
{
$this->_clearTemplateCache();
parent::__construct();
if (is_callable('mb_internal_encoding')) {
mb_internal_encoding(\Smarty\Smarty::$_CHARSET);
}
$this->start_time = microtime(true);
if (isset($_SERVER[ 'SCRIPT_NAME' ])) {
\Smarty\Smarty::$global_tpl_vars[ 'SCRIPT_NAME' ] = new \Smarty\Variable($_SERVER[ 'SCRIPT_NAME' ]);
}
// Check if we're running on Windows
\Smarty\Smarty::$_IS_WINDOWS = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
// let PCRE (preg_*) treat strings as ISO-8859-1 if we're not dealing with UTF-8
@@ -996,7 +992,7 @@ class Smarty extends \Smarty\TemplateBase
* @param mixed $cache_id cache id to be used with this template
* @param mixed $compile_id compile id to be used with this template
* @param object $parent next higher level of Smarty variables
* @param boolean $do_clone flag is Smarty object shall be cloned
* @param boolean $do_clone flag is template object shall be cloned
*
* @return \Smarty\Template template object
* @throws \Smarty\Exception
@@ -1017,22 +1013,10 @@ class Smarty extends \Smarty\TemplateBase
$this->_normalizeTemplateConfig(false);
}
$_templateId = $this->_getTemplateId($template, $cache_id, $compile_id);
if ($this->caching && isset(\Smarty\Template::$isCacheTplObj[ $_templateId ])) {
$tpl = $do_clone ? clone \Smarty\Template::$isCacheTplObj[ $_templateId ] :
\Smarty\Template::$isCacheTplObj[ $_templateId ];
$tpl->inheritance = null;
$tpl->tpl_vars = $tpl->config_vars = array();
} elseif (!$do_clone && isset(\Smarty\Template::$tplObjCache[ $_templateId ])) {
$tpl = clone \Smarty\Template::$tplObjCache[ $_templateId ];
$tpl->inheritance = null;
$tpl->tpl_vars = $tpl->config_vars = array();
} else {
$tpl = new \Smarty\Template($template, $this, null, $cache_id, $compile_id, null, null);
$tpl->templateId = $_templateId;
}
if ($do_clone) {
$tpl->smarty = clone $tpl->smarty;
}
$tpl->parent = $parent ? $parent : $this;
// fill data if present
if (!empty($data) && is_array($data)) {
@@ -1132,15 +1116,6 @@ class Smarty extends \Smarty\TemplateBase
return $realpath !== false ? $parts[ 'root' ] . $path : str_ireplace(getcwd(), '.', $parts[ 'root' ] . $path);
}
/**
* Empty template objects cache
*/
public function _clearTemplateCache()
{
\Smarty\Template::$isCacheTplObj = array();
\Smarty\Template::$tplObjCache = array();
}
/**
* @param boolean $use_sub_dirs
*/
@@ -1386,7 +1361,6 @@ class Smarty extends \Smarty\TemplateBase
$compile_id = null,
$exp_time = null
) {
$this->_clearTemplateCache();
return $this->getCacheResource()->clear($this, $template_name, $cache_id, $compile_id, $exp_time);
}
@@ -1403,7 +1377,6 @@ class Smarty extends \Smarty\TemplateBase
*/
public function clearAllCache($exp_time = null)
{
$this->_clearTemplateCache();
return $this->getCacheResource()->clearAll($this, $exp_time);
}
@@ -1422,8 +1395,6 @@ class Smarty extends \Smarty\TemplateBase
*/
public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
{
// clear template objects cache
$this->_clearTemplateCache();
$_compile_dir = $this->getCompileDir();
if ($_compile_dir === '/') { //We should never want to delete this!
return 0;
@@ -1629,7 +1600,6 @@ class Smarty extends \Smarty\TemplateBase
}
// free memory
unset($_tpl);
$_smarty->_clearTemplateCache();
if ($max_errors !== null && $_error_count === $max_errors) {
echo "\ntoo many errors\n";
exit(1);
@@ -2250,5 +2220,36 @@ class Smarty extends \Smarty\TemplateBase
$this->cacheResource = $cacheResource;
}
/**
* Sets a global variable, available in all templates
*
* @param string $varName
* @param Variable $param
*
* @return void
*/
public function setGlobalVariable(string $varName, Variable $param) {
$this->global_tpl_vars[$varName] = $param;
}
/**
* Returns all global variables
*
* @return array
*/
public function getAllGlobalTemplateVars() {
return $this->global_tpl_vars;
}
/**
* Returns a single global variable, or null if not found.
* @param string $varName
*
* @return Variable|null
*/
public function getGlobalVariable(string $varName): ?Variable {
return $this->global_tpl_vars[$varName] ?? null;
}
}

View File

@@ -27,20 +27,6 @@ use Smarty\Template\Config;
#[\AllowDynamicProperties]
class Template extends TemplateBase {
/**
* Template object cache
*
* @var Template[]
*/
public static $tplObjCache = [];
/**
* Template object cache for Smarty::isCached() === true
*
* @var Template[]
*/
public static $isCacheTplObj = [];
/**
* Sub template Info Cache
* - index name
@@ -282,26 +268,7 @@ class Template extends TemplateBase {
$_templateId = $smarty->_getTemplateId($template, $cache_id, $compile_id, $caching, $tpl);
// recursive call ?
if ((isset($tpl->templateId) ? $tpl->templateId : $tpl->_getTemplateId()) !== $_templateId) {
// already in template cache?
if (isset(self::$tplObjCache[$_templateId])) {
// copy data from cached object
$cachedTpl = &self::$tplObjCache[$_templateId];
$tpl->templateId = $cachedTpl->templateId;
$tpl->template_resource = $cachedTpl->template_resource;
$tpl->cache_id = $cachedTpl->cache_id;
$tpl->compile_id = $cachedTpl->compile_id;
$tpl->source = $cachedTpl->source;
if (isset($cachedTpl->compiled)) {
$tpl->compiled = $cachedTpl->compiled;
} else {
unset($tpl->compiled);
}
if ($caching !== 9999 && isset($cachedTpl->cached)) {
$tpl->cached = $cachedTpl->cached;
} else {
unset($tpl->cached);
}
} else {
$tpl->templateId = $_templateId;
$tpl->template_resource = $template;
$tpl->cache_id = $cache_id;
@@ -321,7 +288,6 @@ class Template extends TemplateBase {
if ($caching !== 9999) {
unset($tpl->cached);
}
}
} else {
// on recursive calls force caching
$forceTplCache = true;
@@ -330,15 +296,7 @@ class Template extends TemplateBase {
$tpl->cache_lifetime = $cache_lifetime;
// set template scope
$tpl->scope = $scope;
if (!isset(self::$tplObjCache[$tpl->templateId]) && !$tpl->source->handler->recompiled) {
// check if template object should be cached
if ($forceTplCache || (isset(self::$subTplInfo[$tpl->template_resource])
&& self::$subTplInfo[$tpl->template_resource] > 1)
|| ($tpl->_isSubTpl() && isset(self::$tplObjCache[$tpl->parent->templateId]))
) {
self::$tplObjCache[$tpl->templateId] = $tpl;
}
}
if (!empty($data)) {
// set up variable values
foreach ($data as $_key => $_val) {
@@ -810,7 +768,7 @@ class Template extends TemplateBase {
$mergedScope = $tagScope | $this->scope;
if ($mergedScope) {
if ($mergedScope & \Smarty\Smarty::SCOPE_GLOBAL && $varName) {
\Smarty\Smarty::$global_tpl_vars[$varName] = $this->tpl_vars[$varName];
$this->_getSmartyObj()->setGlobalVariable($varName, $this->tpl_vars[$varName]);
}
// update scopes
foreach ($this->_getAffectedScopes($mergedScope) as $ptr) {

View File

@@ -194,7 +194,6 @@ abstract class TemplateBase extends Data {
$template->loadCached();
}
$result = $template->cached->isCached($template);
Template::$isCacheTplObj[$template->_getTemplateId()] = $template;
} else {
return false;
}
@@ -204,21 +203,15 @@ abstract class TemplateBase extends Data {
$savedConfigVars = $template->config_vars;
}
ob_start();
$template->_mergeVars();
if (!empty(\Smarty\Smarty::$global_tpl_vars)) {
$template->tpl_vars = array_merge(\Smarty\Smarty::$global_tpl_vars, $template->tpl_vars);
}
$template->tpl_vars = array_merge($this->_getSmartyObj()->getAllGlobalTemplateVars(), $template->tpl_vars);
$result = $template->render(false, $function);
$template->_cleanUp();
if ($saveVars) {
$template->tpl_vars = $savedTplVars;
$template->config_vars = $savedConfigVars;
} else {
if (!$function && !isset(Template::$tplObjCache[$template->templateId])) {
$template->parent = null;
$template->tpl_vars = $template->config_vars = [];
Template::$tplObjCache[$template->templateId] = $template;
}
}
}

View File

@@ -38,6 +38,10 @@ class Variable
$this->nocache = $nocache;
}
public function getValue() {
return $this->value;
}
/**
* <<magic>> String conversion
*

View File

@@ -42,7 +42,6 @@ class AssignGlobalTest extends PHPUnit_Smarty
$this->smarty->assignGlobal('foo', array('foo' => 'bar', 'foo2' => 'bar2'));
$a1 = array('foo' => array('foo' => 'bar', 'foo2' => 'bar2'));
$a2 = $this->smarty->getGlobal();
unset($a2['SCRIPT_NAME']);
$this->assertTrue($a1 === $a2);
}

View File

@@ -8,15 +8,11 @@
/**
* class for clearing all assigned variables tests
*
*
*
*
*/
class ClearAllAssignTest extends PHPUnit_Smarty
{
protected $_data = null;
protected $_tpl = null;
private $_data = null;
private $_tpl = null;
public function setUp(): void
{

View File

@@ -575,7 +575,6 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty
$result = $this->smarty->fetch($tpl);
$this->assertStringContainsString('(grand|b)content grand b(grand|/b)(child|b)content child b(child|/b)(parent|b)content parent b(parent|/b)',
$result);
$this->smarty->_clearTemplateCache();
$this->smarty->assign('parent', 'parent2');
$this->smarty->assign('child', 'child2', true);
$this->smarty->assign('grand', 'grand2', true);
@@ -630,7 +629,6 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty
$result = $this->smarty->fetch($tpl);
$this->assertStringContainsString('(grand|b)content grand4 b(grand|/b)(child|b)content child4 b(child|/b)(parent|b)content parent4 b(parent|/b)',
$result);
$this->smarty->_clearTemplateCache();
$this->smarty->assign('parent', 'parent5');
$this->smarty->assign('child', 'child5', true);
$this->smarty->assign('grand', 'grand5', true);

View File

@@ -13,11 +13,11 @@ use Smarty\Template;
* Smarty {checkvar}
*
* @param array $params parameter array
* @param object $template template object
* @param Template $template template object
*
* @return string
*/
function smarty_function_checkvar($params, $template)
function smarty_function_checkvar($params, \Smarty\Template $template)
{
$output = '';
$types = array('template', 'data', 'smarty', 'global');
@@ -52,8 +52,8 @@ function smarty_function_checkvar($params, $template)
}
if (in_array('global', $types)) {
$output .= "#global:\${$var} =";
$output .= isset(\Smarty\Smarty::$global_tpl_vars[ $var ]) ?
preg_replace('/\s/', '', var_export(\Smarty\Smarty::$global_tpl_vars[ $var ]->value, true)) : '>unassigned<';
$output .= $template->_getSmartyObj()->getGlobalVariable($var) ?
preg_replace('/\s/', '', var_export($template->_getSmartyObj()->getGlobalVariable($var)->getValue(), true)) : '>unassigned<';
}
return $output;
}