Skip to content

Commit

Permalink
- update config file processing
Browse files Browse the repository at this point in the history
  • Loading branch information
uwetews committed Nov 1, 2015
1 parent 8a78038 commit cc3f69a
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 112 deletions.
3 changes: 3 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
 ===== 3.1.28-dev===== (xx.xx.2015)
01.11.2015
- update config file processing

31.10.2015
- bugfix add missing $trusted_dir property to SmartyBC class (forum topic 25751)

Expand Down
8 changes: 4 additions & 4 deletions lexer/smarty_internal_templateparser.y
Original file line number Diff line number Diff line change
Expand Up @@ -903,19 +903,19 @@ variable(res) ::= object(o). {

// config variable
variable(res) ::= HATCH ID(i) HATCH. {
res = '$_smarty_tpl->smarty->ext->_config->_getConfigVariable($_smarty_tpl, \''. i .'\')';
res = $this->compiler->compileConfigVariable("'" . i . "'");
}

variable(res) ::= HATCH ID(i) HATCH arrayindex(a). {
res = '(is_array($tmp = $_smarty_tpl->smarty->ext->_config->_getConfigVariable($_smarty_tpl, \''. i .'\')) ? $tmp'.a.' :null)';
res = '(is_array($tmp = ' . $this->compiler->compileConfigVariable("'" . i . "'") . ') ? $tmp'.a.' :null)';
}

variable(res) ::= HATCH variable(v) HATCH. {
res = '$_smarty_tpl->smarty->ext->_config->_getConfigVariable($_smarty_tpl, '. v .')';
res = $this->compiler->compileConfigVariable(v);
}

variable(res) ::= HATCH variable(v) HATCH arrayindex(a). {
res = '(is_array($tmp = $_smarty_tpl->smarty->ext->_config->_getConfigVariable($_smarty_tpl, '. v .')) ? $tmp'.a.' : null)';
res = '(is_array($tmp = ' . $this->compiler->compileConfigVariable(v) . ') ? $tmp'.a.' : null)';
}

varindexed(res) ::= DOLLARID(i) arrayindex(a). {
Expand Down
2 changes: 1 addition & 1 deletion libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.28-dev/75';
const SMARTY_VERSION = '3.1.28-dev/76';

/**
* define variable scopes
Expand Down
38 changes: 29 additions & 9 deletions libs/sysplugins/smarty_internal_compile_config_load.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ class Smarty_Internal_Compile_Config_Load extends Smarty_Internal_CompileBase
* @var array
* @see Smarty_Internal_CompileBase
*/
public $optional_attributes = array('section', 'scope');
public $optional_attributes = array('section', 'scope', 'bubble_up');

/**
* Valid scope names
*
* @var array
*/
public $valid_scopes = array('local' => true, 'parent' => true, 'root' => true, 'global' => true,
'smarty' => true, 'tpl_root' => true);

/**
* Compiles code for the {config_load} tag
Expand All @@ -51,7 +59,6 @@ class Smarty_Internal_Compile_Config_Load extends Smarty_Internal_CompileBase
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
static $_is_legal_scope = array('local' => true, 'parent' => true, 'root' => true, 'global' => true);
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);

Expand All @@ -66,18 +73,31 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
} else {
$section = 'null';
}
$scope = 'local';
// scope setup
$_scope = Smarty::SCOPE_LOCAL;
if (isset($_attr['scope'])) {
$_attr['scope'] = trim($_attr['scope'], "'\"");
if (isset($_is_legal_scope[$_attr['scope']])) {
$scope = $_attr['scope'];
} else {
$compiler->trigger_template_error('illegal value for "scope" attribute', null, true);
if (!isset($this->valid_scopes[$_attr['scope']])) {
$compiler->trigger_template_error("illegal value '{$_attr['scope']}' for \"scope\" attribute", null, true);
}
if ($_attr['scope'] != 'local') {
if ($_attr['scope'] == 'parent') {
$_scope = Smarty::SCOPE_PARENT;
} elseif ($_attr['scope'] == 'root') {
$_scope = Smarty::SCOPE_ROOT;
} elseif ($_attr['scope'] == 'global') {
$_scope = Smarty::SCOPE_GLOBAL;
} elseif ($_attr['scope'] == 'smarty') {
$_scope = Smarty::SCOPE_SMARTY;
} elseif ($_attr['scope'] == 'tpl_root') {
$_scope = Smarty::SCOPE_TPL_ROOT;
}
$_scope += (isset($_attr['bubble_up']) && $_attr['bubble_up'] == 'false') ? 0 : Smarty::SCOPE_BUBBLE_UP;
}
}

// create config object
$_output = "<?php \$_smarty_tpl->configLoad($conf_file, $section, '$scope');?>";
$_output =
"<?php\n\$_smarty_tpl->smarty->ext->configLoad->_loadConfigFile(\$_smarty_tpl, {$conf_file}, {$section}, {$_scope});\n?>\n";

return $_output;
}
Expand Down
4 changes: 3 additions & 1 deletion libs/sysplugins/smarty_internal_config_file_compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ public function compileTemplate(Smarty_Internal_Template $template)
$this->smarty->_debug->start_compile($this->template);
}
// init the lexer/parser to compile the config file
/* @var Smarty_Internal_ConfigFileLexer $lex */
$lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->getContent()) .
"\n", $this);
/* @var Smarty_Internal_ConfigFileParser $parser */
$parser = new $this->parser_class($lex, $this);

if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
Expand Down Expand Up @@ -143,7 +145,7 @@ public function compileTemplate(Smarty_Internal_Template $template)
strftime("%Y-%m-%d %H:%M:%S") . "\n";
$template_header .= " compiled from \"" . $this->template->source->filepath . "\" */ ?>\n";

$code = '<?php Smarty_Internal_Method_ConfigLoad::_loadConfigVars($_smarty_tpl, ' .
$code = '<?php $_smarty_tpl->smarty->ext->configLoad->_loadConfigVars($_smarty_tpl, ' .
var_export($this->config_data, true) . '); ?>';
return $template_header . $this->template->smarty->ext->_codeFrame->create($this->template, $code);
}
Expand Down
135 changes: 108 additions & 27 deletions libs/sysplugins/smarty_internal_method_configload.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,74 +28,155 @@ class Smarty_Internal_Method_ConfigLoad
* @param string $config_file filename
* @param mixed $sections array of section names, single
* section or null
* @param string $scope scope into which config variables
*
* @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template
* @throws \SmartyException
*/
public function configLoad(Smarty_Internal_Data $data, $config_file, $sections = null)
{
$this->_loadConfigFile($data, $config_file, $sections, 0);
return $data;
}

/**
* load a config file, optionally load just selected sections
*
* @api Smarty::configLoad()
* @link http://www.smarty.net/docs/en/api.config.load.tpl
*
* @param \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template $data
* @param string $config_file filename
* @param mixed $sections array of section names, single
* section or null
* @param int $scope scope into which config variables
* shall be loaded
*
* @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template
* @throws \SmartyException
*/
public function configLoad(Smarty_Internal_Data $data, $config_file, $sections = null, $scope = 'local')
public function _loadConfigFile(Smarty_Internal_Data $data, $config_file, $sections = null, $scope = 0)
{
/* @var \Smarty $smarty */
$smarty = isset($data->smarty) ? $data->smarty : $data;
/* @var \Smarty_Internal_Template $confObj */
$confObj = new $smarty->template_class($config_file, $smarty, $data);
$confObj = new Smarty_Internal_Template($config_file, $smarty, $data);
$confObj->caching = Smarty::CACHING_OFF;
$confObj->source = Smarty_Template_Config::load($confObj);
$confObj->source->config_sections = $sections;
$confObj->source->scope = $scope;
$confObj->compiled = Smarty_Template_Compiled::load($confObj);
$confObj->compiled->render($confObj);
if ($data->_objType == 2) {
$data->compiled->file_dependency[$confObj->source->uid] = array($confObj->source->filepath,
$confObj->source->getTimeStamp(),
$confObj->source->type);
$data->compiled->file_dependency[$confObj->source->uid] =
array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type);
}
return $data;
}

/**
* load config variables into template object
*
* @param \Smarty_Internal_Template $_template
* @param \Smarty_Internal_Template $tpl
* @param array $_config_vars
*
*/
static function _loadConfigVars(Smarty_Internal_Template $_template, $_config_vars)
public function _loadConfigVars(Smarty_Internal_Template $tpl, $_config_vars)
{
$scope = $_template->source->scope;
// pointer to scope (local scope is parent of template object
$scope_ptr = $_template->parent;
if ($scope == 'parent') {
if (isset($_template->parent->parent)) {
$scope_ptr = $_template->parent->parent;
}
} elseif ($scope == 'root' || $scope == 'global') {
while (isset($scope_ptr->parent)) {
$scope_ptr = $scope_ptr->parent;
$this->_assignConfigVars($tpl->parent, $tpl, $_config_vars);
$scope = $tpl->source->scope;
if (!$scope && !$tpl->scope) {
return;
}
foreach (array($scope, $tpl->scope) as $s) {
$s = ($bubble_up = $s >= Smarty::SCOPE_BUBBLE_UP) ? $s - Smarty::SCOPE_BUBBLE_UP : $s;
if ($bubble_up && $s) {
$ptr = $tpl->parent->parent;
if (isset($ptr)) {
$this->_assignConfigVars($ptr, $tpl, $_config_vars);
$ptr = $ptr->parent;
}
if ($s == Smarty::SCOPE_PARENT) {
continue;
}
while (isset($ptr) && $ptr->_objType == 2) {
$this->_assignConfigVars($ptr, $tpl, $_config_vars);
$ptr = $ptr->parent;
}
if ($s == Smarty::SCOPE_TPL_ROOT) {
continue;
} elseif ($s == Smarty::SCOPE_SMARTY) {
$this->_assignConfigVars($tpl->smarty, $tpl, $_config_vars);
} elseif ($s == Smarty::SCOPE_GLOBAL) {
$this->_assignConfigVars($tpl->smarty, $tpl, $_config_vars);
} elseif ($s == Smarty::SCOPE_ROOT) {
while (isset($ptr->parent)) {
$ptr = $ptr->parent;
}
$this->_assignConfigVars($ptr, $tpl, $_config_vars);
}
}
}
}

/**
* Assign all config variables in given scope
*
* @param \Smarty_Internal_Data $scope_ptr
* @param \Smarty_Internal_Template $tpl
* @param array $_config_vars
*/
public function _assignConfigVars(Smarty_Internal_Data $scope_ptr, Smarty_Internal_Template $tpl, $_config_vars)
{
// copy global config vars
foreach ($_config_vars['vars'] as $variable => $value) {
if ($_template->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
if ($tpl->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
$scope_ptr->config_vars[$variable] = $value;
} else {
$scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
$scope_ptr->config_vars[$variable] =
array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
}
}
// scan sections
$sections = $_template->source->config_sections;
$sections = $tpl->source->config_sections;
if (!empty($sections)) {
foreach ((array) $sections as $_template_section) {
if (isset($_config_vars['sections'][$_template_section])) {
foreach ($_config_vars['sections'][$_template_section]['vars'] as $variable => $value) {
if ($_template->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
foreach ((array) $sections as $tpl_section) {
if (isset($_config_vars['sections'][$tpl_section])) {
foreach ($_config_vars['sections'][$tpl_section]['vars'] as $variable => $value) {
if ($tpl->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
$scope_ptr->config_vars[$variable] = $value;
} else {
$scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
$scope_ptr->config_vars[$variable] =
array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
}
}
}
}
}
}

/**
* gets a config variable value
*
* @param \Smarty_Internal_Template $tpl template object
* @param string $varName the name of the config variable
* @param bool $errorEnable
*
* @return mixed the value of the config variable
*/
public function _getConfigVariable(\Smarty_Internal_Template $tpl, $varName, $errorEnable = true)
{
$_ptr = $tpl;
while ($_ptr !== null) {
if (isset($_ptr->config_vars[$varName])) {
// found it, return it
return $_ptr->config_vars[$varName];
}
// not found, try at parent
$_ptr = $_ptr->parent;
}
if ($tpl->smarty->error_unassigned && $errorEnable) {
// force a notice
$x = $$varName;
}
return null;
}
}
39 changes: 0 additions & 39 deletions libs/sysplugins/smarty_internal_runtime_config.php

This file was deleted.

Loading

0 comments on commit cc3f69a

Please sign in to comment.