From 1fe5050a0433505d95282a7a89e0cb2173638d78 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Wed, 11 Jan 2023 13:05:42 +0100 Subject: [PATCH] Fix configfile tests --- src/Compiler/Configfile.php | 4 +-- src/Data.php | 63 +++++++++++++------------------------ src/Template.php | 4 +-- src/Template/Config.php | 7 ----- 4 files changed, 25 insertions(+), 53 deletions(-) diff --git a/src/Compiler/Configfile.php b/src/Compiler/Configfile.php index e016cb43..a2e7cf4f 100644 --- a/src/Compiler/Configfile.php +++ b/src/Compiler/Configfile.php @@ -140,8 +140,8 @@ class Configfile extends BaseCompiler { date("Y-m-d H:i:s"), str_replace('*/', '* /', $this->template->source->filepath) ); - $code = 'assignConfigVars(' . - var_export($this->config_data, true) . '); ?>'; + $code = 'parent->assignConfigVars(' . + var_export($this->config_data, true) . ', $_smarty_tpl->getValue("sections")); ?>'; return $template_header . $this->template->createCodeFrame($code); } diff --git a/src/Data.php b/src/Data.php index cf80fdcc..a43212e5 100644 --- a/src/Data.php +++ b/src/Data.php @@ -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,27 +249,24 @@ 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) { - 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])) { - $this->config_vars[$variable] = $value; - } else { - $this->config_vars[$variable] = array_merge((array)$this->config_vars[$variable], (array)$value); - } + + 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->_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); } } } @@ -384,25 +383,6 @@ abstract class Data return array_merge($this->parent ? $this->parent->getConfigVars() : [], $this->config_vars); } - /** - * 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 - * - * @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 * @@ -410,23 +390,22 @@ abstract class Data * @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; } - } diff --git a/src/Template.php b/src/Template.php index 0d626b2a..272eab17 100644 --- a/src/Template.php +++ b/src/Template.php @@ -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); diff --git a/src/Template/Config.php b/src/Template/Config.php index 2a6e7b7a..ecaedfb7 100644 --- a/src/Template/Config.php +++ b/src/Template/Config.php @@ -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 *