3 * Smarty Internal Plugin Config
5 * Main class for config variables
12 class Smarty_Internal_Config {
13 static $config_objects = array();
15 public function __construct($config_resource, $smarty, $data = null)
18 $this->smarty = $smarty;
19 $this->config_resource = $config_resource;
20 $this->config_resource_type = null;
21 $this->config_resource_name = null;
22 $this->config_filepath = null;
23 $this->config_timestamp = null;
24 $this->config_source = null;
25 $this->compiled_config = null;
26 $this->compiled_filepath = null;
27 $this->compiled_timestamp = null;
28 $this->mustCompile = null;
29 $this->compiler_object = null;
30 // parse config resource name
31 if (!$this->parseConfigResourceName ($config_resource)) {
32 throw new SmartyException ("Unable to parse config resource '{$config_resource}'");
36 public function getConfigFilepath ()
38 return $this->config_filepath === null ?
39 $this->config_filepath = $this->buildConfigFilepath() :
40 $this->config_filepath;
43 public function getTimestamp ()
45 return $this->config_timestamp === null ?
46 $this->config_timestamp = filemtime($this->getConfigFilepath()) :
47 $this->config_timestamp;
50 private function parseConfigResourceName($config_resource)
52 if (empty($config_resource))
54 if (strpos($config_resource, ':') === false) {
55 // no resource given, use default
56 $this->config_resource_type = $this->smarty->default_config_type;
57 $this->config_resource_name = $config_resource;
59 // get type and name from path
60 list($this->config_resource_type, $this->config_resource_name) = explode(':', $config_resource, 2);
61 if (strlen($this->config_resource_type) == 1) {
62 // 1 char is not resource type, but part of filepath
63 $this->config_resource_type = $this->smarty->default_config_type;
64 $this->config_resource_name = $config_resource;
66 $this->config_resource_type = strtolower($this->config_resource_type);
73 * get system filepath to config
75 public function buildConfigFilepath ()
77 foreach((array)$this->smarty->config_dir as $_config_dir) {
78 if (strpos('/\\', substr($_config_dir, -1)) === false) {
82 $_filepath = $_config_dir . $this->config_resource_name;
83 if (file_exists($_filepath))
86 // check for absolute path
87 if (file_exists($this->config_resource_name))
88 return $this->config_resource_name;
90 throw new SmartyException("Unable to load config file \"{$this->config_resource_name}\"");
94 * Read config file source
96 * @return string content of source file
99 * Returns the template source code
101 * The template source is being read by the actual resource handler
103 * @return string the template source
105 public function getConfigSource ()
107 if ($this->config_source === null) {
108 if ($this->readConfigSource($this) === false) {
109 throw new SmartyException("Unable to load config file \"{$this->config_resource_name}\"");
112 return $this->config_source;
114 public function readConfigSource()
117 if (file_exists($this->getConfigFilepath())) {
118 $this->config_source = file_get_contents($this->getConfigFilepath());
126 * Returns the compiled filepath
128 * @return string the compiled filepath
130 public function getCompiledFilepath ()
132 return $this->compiled_filepath === null ?
133 ($this->compiled_filepath = $this->buildCompiledFilepath()) :
134 $this->compiled_filepath;
136 public function buildCompiledFilepath()
138 $_compile_id = isset($this->smarty->compile_id) ? preg_replace('![^\w\|]+!', '_', $this->smarty->compile_id) : null;
139 $_flag = (int)$this->smarty->config_read_hidden + (int)$this->smarty->config_booleanize * 2 +
140 (int)$this->smarty->config_overwrite * 4;
141 $_filepath = sha1($this->config_resource_name . $_flag);
142 // if use_sub_dirs, break file into directories
143 if ($this->smarty->use_sub_dirs) {
144 $_filepath = substr($_filepath, 0, 2) . DS
145 . substr($_filepath, 2, 2) . DS
146 . substr($_filepath, 4, 2) . DS
149 $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
150 if (isset($_compile_id)) {
151 $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
153 $_compile_dir = $this->smarty->compile_dir;
154 if (substr($_compile_dir, -1) != DS) {
157 return $_compile_dir . $_filepath . '.' . basename($this->config_resource_name) . '.config' . '.php';
160 * Returns the timpestamp of the compiled file
162 * @return integer the file timestamp
164 public function getCompiledTimestamp ()
166 return $this->compiled_timestamp === null ?
167 ($this->compiled_timestamp = (file_exists($this->getCompiledFilepath())) ? filemtime($this->getCompiledFilepath()) : false) :
168 $this->compiled_timestamp;
171 * Returns if the current config file must be compiled
173 * It does compare the timestamps of config source and the compiled config and checks the force compile configuration
175 * @return boolean true if the file must be compiled
177 public function mustCompile ()
179 return $this->mustCompile === null ?
180 $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp () === false || $this->smarty->compile_check && $this->getCompiledTimestamp () < $this->getTimestamp ()):
184 * Returns the compiled config file
186 * It checks if the config file must be compiled or just read the compiled version
188 * @return string the compiled config file
190 public function getCompiledConfig ()
192 if ($this->compiled_config === null) {
193 // see if template needs compiling.
194 if ($this->mustCompile()) {
195 $this->compileConfigSource();
197 $this->compiled_config = file_get_contents($this->getCompiledFilepath());
200 return $this->compiled_config;
204 * Compiles the config files
206 public function compileConfigSource ()
209 if (!is_object($this->compiler_object)) {
211 $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
214 if ($this->smarty->compile_locking) {
215 if ($saved_timestamp = $this->getCompiledTimestamp()) {
216 touch($this->getCompiledFilepath());
221 $this->compiler_object->compileSource($this);
223 catch (Exception $e) {
224 // restore old timestamp in case of error
225 if ($this->smarty->compile_locking && $saved_timestamp) {
226 touch($this->getCompiledFilepath(), $saved_timestamp);
230 // compiling succeded
231 // write compiled template
232 Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty);
236 * load config variables
238 * @param mixed $sections array of section names, single section or null
239 * @param object $scope global,parent or local
241 public function loadConfigVars ($sections = null, $scope = 'local')
243 if ($this->data instanceof Smarty_Internal_Template) {
244 $this->data->properties['file_dependency'][sha1($this->getConfigFilepath())] = array($this->getConfigFilepath(), $this->getTimestamp(),'file');
246 if ($this->mustCompile()) {
247 $this->compileConfigSource();
250 if ($scope == 'local') {
251 $scope_ptr = $this->data;
252 } elseif ($scope == 'parent') {
253 if (isset($this->data->parent)) {
254 $scope_ptr = $this->data->parent;
256 $scope_ptr = $this->data;
258 } elseif ($scope == 'root' || $scope == 'global') {
259 $scope_ptr = $this->data;
260 while (isset($scope_ptr->parent)) {
261 $scope_ptr = $scope_ptr->parent;
264 $_config_vars = array();
265 include($this->getCompiledFilepath ());
266 // copy global config vars
267 foreach ($_config_vars['vars'] as $variable => $value) {
268 if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
269 $scope_ptr->config_vars[$variable] = $value;
271 $scope_ptr->config_vars[$variable] = array_merge((array)$scope_ptr->config_vars[$variable], (array)$value);
275 foreach ($_config_vars['sections'] as $this_section => $dummy) {
276 if ($sections == null || in_array($this_section, (array)$sections)) {
277 foreach ($_config_vars['sections'][$this_section]['vars'] as $variable => $value) {
278 if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
279 $scope_ptr->config_vars[$variable] = $value;
281 $scope_ptr->config_vars[$variable] = array_merge((array)$scope_ptr->config_vars[$variable], (array)$value);