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"), date("Y-m-d H:i:s"),
str_replace('*/', '* /', $this->template->source->filepath) str_replace('*/', '* /', $this->template->source->filepath)
); );
$code = '<?php $_smarty_tpl->assignConfigVars(' . $code = '<?php $_smarty_tpl->parent->assignConfigVars(' .
var_export($this->config_data, true) . '); ?>'; var_export($this->config_data, true) . ', $_smarty_tpl->getValue("sections")); ?>';
return $template_header . $this->template->createCodeFrame($code); return $template_header . $this->template->createCodeFrame($code);
} }

View File

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

View File

@@ -693,9 +693,9 @@ class Template extends TemplateBase {
/** /**
* @inheritdoc * @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 ] = $this->compiled->file_dependency[ $confObj->source->uid ] =
array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type); array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type);

View File

@@ -14,13 +14,6 @@ use Smarty\Exception;
*/ */
class Config extends Source { 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 * Flag that source is a config file
* *