Commit | Line | Data |
---|---|---|
ccd27f54 S |
1 | <?php |
2 | ||
3 | /** | |
4 | * @package Smarty | |
5 | * @subpackage PluginsInternal | |
6 | */ | |
7 | class Smarty_Internal_Extension_Config | |
8 | { | |
9 | /** | |
10 | * @param $obj | |
11 | * @param $config_file | |
12 | * @param null $sections | |
13 | * @param string $scope | |
14 | */ | |
15 | static function configLoad($obj, $config_file, $sections = null, $scope = 'local') | |
16 | { | |
17 | $smarty = isset($obj->smarty) ? $obj->smarty : $obj; | |
18 | $confObj = new $smarty->template_class($config_file, $smarty, $obj); | |
19 | $confObj->caching = Smarty::CACHING_OFF; | |
20 | $confObj->source = Smarty_Template_Config::load($confObj); | |
21 | $confObj->source->config_sections = $sections; | |
22 | $confObj->source->scope = $scope; | |
23 | $confObj->compiled = Smarty_Template_Compiled::load($confObj); | |
24 | if ($confObj->smarty->debugging) { | |
25 | Smarty_Internal_Debug::start_render($confObj); | |
26 | } | |
27 | $confObj->compiled->render($confObj); | |
28 | if ($confObj->smarty->debugging) { | |
29 | Smarty_Internal_Debug::end_render($confObj); | |
30 | } | |
31 | if ($obj instanceof Smarty_Internal_Template) { | |
32 | $obj->properties['file_dependency'][$confObj->source->uid] = array($confObj->source->filepath, $confObj->source->timestamp, $confObj->source->type); | |
33 | } | |
34 | } | |
35 | ||
36 | /** | |
37 | * load config variables | |
38 | * | |
39 | * @param mixed $sections array of section names, single section or null | |
40 | * @param string $scope global,parent or local | |
41 | * | |
42 | * @throws Exception | |
43 | */ | |
44 | static function loadConfigVars($_template, $_config_vars) | |
45 | { | |
46 | $scope = $_template->source->scope; | |
47 | // pointer to scope (local scope is parent of template object | |
48 | $scope_ptr = $_template->parent; | |
49 | if ($scope == 'parent') { | |
50 | if (isset($_template->parent->parent)) { | |
51 | $scope_ptr = $_template->parent->parent; | |
52 | } | |
53 | } elseif ($scope == 'root' || $scope == 'global') { | |
54 | while (isset($scope_ptr->parent)) { | |
55 | $scope_ptr = $scope_ptr->parent; | |
56 | } | |
57 | } | |
58 | // copy global config vars | |
59 | foreach ($_config_vars['vars'] as $variable => $value) { | |
60 | if ($_template->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) { | |
61 | $scope_ptr->config_vars[$variable] = $value; | |
62 | } else { | |
63 | $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value); | |
64 | } | |
65 | } | |
66 | // scan sections | |
67 | $sections = $_template->source->config_sections; | |
68 | if (!empty($sections)) { | |
69 | foreach ((array) $sections as $_template_section) { | |
70 | if (isset($_config_vars['sections'][$_template_section])) { | |
71 | foreach ($_config_vars['sections'][$_template_section]['vars'] as $variable => $value) { | |
72 | if ($_template->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) { | |
73 | $scope_ptr->config_vars[$variable] = $value; | |
74 | } else { | |
75 | $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value); | |
76 | } | |
77 | } | |
78 | } | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
83 | /** | |
84 | * Returns a single or all config variables | |
85 | * | |
86 | * @param string $varname variable name or null | |
87 | * @param bool $search_parents | |
88 | * | |
89 | * @return string variable value or or array of variables | |
90 | */ | |
91 | static function getConfigVars($obj, $varname = null, $search_parents = true) | |
92 | { | |
93 | $_ptr = $obj; | |
94 | $var_array = array(); | |
95 | while ($_ptr !== null) { | |
96 | if (isset($varname)) { | |
97 | if (isset($_ptr->config_vars[$varname])) { | |
98 | return $_ptr->config_vars[$varname]; | |
99 | } | |
100 | } else { | |
101 | $var_array = array_merge($_ptr->config_vars, $var_array); | |
102 | } | |
103 | // not found, try at parent | |
104 | if ($search_parents) { | |
105 | $_ptr = $_ptr->parent; | |
106 | } else { | |
107 | $_ptr = null; | |
108 | } | |
109 | } | |
110 | if (isset($varname)) { | |
111 | return ''; | |
112 | } else { | |
113 | return $var_array; | |
114 | } | |
115 | } | |
116 | ||
117 | /** | |
118 | * gets a config variable | |
119 | * | |
120 | * @param string $variable the name of the config variable | |
121 | * @param bool $error_enable | |
122 | * | |
123 | * @return mixed the value of the config variable | |
124 | */ | |
125 | static function getConfigVariable($obj, $variable, $error_enable = true) | |
126 | { | |
127 | $_ptr = $obj; | |
128 | while ($_ptr !== null) { | |
129 | if (isset($_ptr->config_vars[$variable])) { | |
130 | // found it, return it | |
131 | return $_ptr->config_vars[$variable]; | |
132 | } | |
133 | // not found, try at parent | |
134 | $_ptr = $_ptr->parent; | |
135 | } | |
cd8826ea | 136 | if ($obj->error_unassigned && $error_enable) { |
ccd27f54 S |
137 | // force a notice |
138 | $x = $$variable; | |
139 | } | |
140 | ||
141 | return null; | |
142 | } | |
143 | ||
144 | /** | |
145 | * remove a single or all config variables | |
146 | * | |
147 | * @param string $name variable name or null | |
148 | * | |
149 | * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining | |
150 | */ | |
151 | static function clearConfig($obj, $name = null) | |
152 | { | |
153 | if (isset($name)) { | |
154 | unset($obj->config_vars[$name]); | |
155 | } else { | |
156 | $obj->config_vars = array(); | |
157 | } | |
158 | return $obj; | |
159 | } | |
160 | } |