Files
smarty/libs/sysplugins/smarty_internal_method_configload.php

186 lines
7.0 KiB
PHP
Raw Normal View History

<?php
/**
* Smarty Method ConfigLoad
*
* Smarty::configLoad() method
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Method_ConfigLoad
{
/**
* Valid for all objects
*
* @var int
*/
public $objMap = 7;
/**
* 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_Internal_Data|\Smarty_Internal_Template|\Smarty $data
* @param string $config_file filename
* @param mixed $sections array of section names, single
* section or null
2015-11-01 02:58:27 +01:00
*
* @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
*/
2015-11-01 02:58:27 +01:00
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 */
2015-11-01 02:58:27 +01:00
$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);
2015-08-23 01:25:57 +02:00
if ($data->_objType == 2) {
2016-02-09 01:27:15 +01:00
$data->compiled->file_dependency[ $confObj->source->uid ] =
2015-11-01 02:58:27 +01:00
array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type);
}
}
/**
* load config variables into template object
*
2015-11-01 02:58:27 +01:00
* @param \Smarty_Internal_Template $tpl
* @param array $_config_vars
2015-11-01 02:58:27 +01:00
*
*/
2015-11-01 02:58:27 +01:00
public function _loadConfigVars(Smarty_Internal_Template $tpl, $_config_vars)
{
2015-11-01 02:58:27 +01:00
$this->_assignConfigVars($tpl->parent, $tpl, $_config_vars);
$scope = $tpl->source->scope;
2016-01-02 02:47:32 +01:00
$scopes = array();
if ($scope) {
$scopes[] = $scope;
}
if ($tpl->scope) {
$scopes[] = $tpl->scope;
}
if (empty($scopes)) {
2015-11-01 02:58:27 +01:00
return;
}
2016-01-02 02:47:32 +01:00
foreach ($scopes as $s) {
2015-11-01 02:58:27 +01:00
$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_ROOT) {
while (isset($ptr->parent)) {
$ptr = $ptr->parent;
2016-01-02 02:47:32 +01:00
$this->_assignConfigVars($ptr, $tpl, $_config_vars);
2015-11-01 02:58:27 +01:00
}
}
}
}
2015-11-01 02:58:27 +01:00
}
/**
* 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
2016-02-09 01:27:15 +01:00
foreach ($_config_vars[ 'vars' ] as $variable => $value) {
if ($tpl->smarty->config_overwrite || !isset($scope_ptr->config_vars[ $variable ])) {
$scope_ptr->config_vars[ $variable ] = $value;
} else {
2016-02-09 01:27:15 +01:00
$scope_ptr->config_vars[ $variable ] =
array_merge((array) $scope_ptr->config_vars[ $variable ], (array) $value);
}
}
// scan sections
2015-11-01 02:58:27 +01:00
$sections = $tpl->source->config_sections;
if (!empty($sections)) {
2015-11-01 02:58:27 +01:00
foreach ((array) $sections as $tpl_section) {
2016-02-09 01:27:15 +01:00
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 {
2016-02-09 01:27:15 +01:00
$scope_ptr->config_vars[ $variable ] =
array_merge((array) $scope_ptr->config_vars[ $variable ], (array) $value);
}
}
}
}
}
}
2015-11-01 02:58:27 +01:00
/**
* 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
*/
2015-12-19 20:10:45 +01:00
public function _getConfigVariable(Smarty_Internal_Template $tpl, $varName, $errorEnable = true)
2015-11-01 02:58:27 +01:00
{
$_ptr = $tpl;
while ($_ptr !== null) {
2016-02-09 01:27:15 +01:00
if (isset($_ptr->config_vars[ $varName ])) {
2015-11-01 02:58:27 +01:00
// found it, return it
2016-02-09 01:27:15 +01:00
return $_ptr->config_vars[ $varName ];
2015-11-01 02:58:27 +01:00
}
// not found, try at parent
$_ptr = $_ptr->parent;
}
if ($tpl->smarty->error_unassigned && $errorEnable) {
// force a notice
$x = $$varName;
}
return null;
}
}