Fix configfile tests

This commit is contained in:
Simon Wisselink
2023-01-11 13:05:42 +01:00
parent 22ce23b47b
commit 1fe5050a04
4 changed files with 25 additions and 53 deletions

View File

@@ -140,8 +140,8 @@ class Configfile extends BaseCompiler {
date("Y-m-d H:i:s"),
str_replace('*/', '* /', $this->template->source->filepath)
);
$code = '<?php $_smarty_tpl->assignConfigVars(' .
var_export($this->config_data, true) . '); ?>';
$code = '<?php $_smarty_tpl->parent->assignConfigVars(' .
var_export($this->config_data, true) . ', $_smarty_tpl->getValue("sections")); ?>';
return $template_header . $this->template->createCodeFrame($code);
}

View File

@@ -2,6 +2,8 @@
namespace Smarty;
use Smarty\Template\Config;
/**
* Smarty Internal Plugin Data
* This file contains the basic properties and methods for holding config and template variables
@@ -247,23 +249,21 @@ abstract class Data
*
* @param array $new_config_vars
*/
public function assignConfigVars($new_config_vars) {
public function assignConfigVars($new_config_vars, array $sections = []) {
// copy global config vars
foreach ($new_config_vars['vars'] as $variable => $value) {
if ($this->smarty->config_overwrite || !isset($this->config_vars[$variable])) {
if ($this->_getSmartyObj()->config_overwrite || !isset($this->config_vars[$variable])) {
$this->config_vars[$variable] = $value;
} else {
$this->config_vars[$variable] = array_merge((array)$this->config_vars[$variable], (array)$value);
}
}
// scan sections
$sections = $this->source->config_sections;
if (!empty($sections)) {
foreach ((array)$sections as $tpl_section) {
foreach ($sections as $tpl_section) {
if (isset($new_config_vars['sections'][$tpl_section])) {
foreach ($new_config_vars['sections'][$tpl_section]['vars'] as $variable => $value) {
if ($this->smarty->config_overwrite || !isset($this->config_vars[$variable])) {
if ($this->_getSmartyObj()->config_overwrite || !isset($this->config_vars[$variable])) {
$this->config_vars[$variable] = $value;
} else {
$this->config_vars[$variable] = array_merge((array)$this->config_vars[$variable], (array)$value);
@@ -272,7 +272,6 @@ abstract class Data
}
}
}
}
/**
* Get Smarty object
@@ -390,43 +389,23 @@ abstract class Data
* @param string $config_file filename
* @param mixed $sections array of section names, single
* section or null
*
* @return $this
* @throws \Exception
*@api Smarty::configLoad()
* @link https://www.smarty.net/docs/en/api.config.load.tpl
*
*/
public function configLoad($config_file, $sections = null)
{
$this->_loadConfigfile($config_file, $sections);
return $this;
}
/**
* load a config file, optionally load just selected sections
*
* @param string $config_file filename
* @param mixed $sections array of section names, single
* section or null
* @returns Template
* @returns $this
* @throws \Exception
* @link https://www.smarty.net/docs/en/api.config.load.tpl
*
* @api Smarty::configLoad()
*/
protected function _loadConfigfile($config_file, $sections = null)
public function configLoad($config_file, $sections = null)
{
$smarty = $this->_getSmartyObj();
$confObj = new Template($config_file, $smarty, $this, null, null, null, null, true);
$confObj->caching = Smarty::CACHING_OFF;
$confObj->source->config_sections = $sections;
$confObj->compiled = \Smarty\Template\Compiled::load($confObj);
$confObj->compiled->render($confObj);
return $confObj;
$template = new Template($config_file, $smarty, $this, null, null, null, null, true);
$template->caching = Smarty::CACHING_OFF;
$template->assign('sections', (array) $sections ?? []);
// trigger a call to $this->assignConfigVars
$template->compiled = \Smarty\Template\Compiled::load($template);
$template->compiled->render($template);
return $this;
}
}

View File

@@ -693,9 +693,9 @@ class Template extends TemplateBase {
/**
* @inheritdoc
*/
protected function _loadConfigfile($config_file, $sections = null)
public function configLoad($config_file, $sections = null)
{
$confObj = parent::_loadConfigfile($config_file, $sections);
$confObj = parent::configLoad($config_file, $sections);
$this->compiled->file_dependency[ $confObj->source->uid ] =
array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type);

View File

@@ -14,13 +14,6 @@ use Smarty\Exception;
*/
class Config extends Source {
/**
* array of section names, single section or null
*
* @var null|string|array
*/
public $config_sections = null;
/**
* Flag that source is a config file
*