3 * Smarty Internal Plugin Config
11 * Smarty Internal Plugin Config
12 * Main class for config variables
18 class Smarty_Internal_Config
25 public $smarty = null;
27 * Object of config var storage
37 public $config_resource = null;
39 * Compiled config file
43 public $compiled_config = null;
45 * filepath of compiled config file
49 public $compiled_filepath = null;
51 * Filemtime of compiled config Filemtime
55 public $compiled_timestamp = null;
57 * flag if compiled config file is invalid and must be (re)compiled
61 public $mustCompile = null;
63 * Config file compiler object
65 * @var Smarty_Internal_Config_File_Compiler object
67 public $compiler_object = null;
70 * Constructor of config file object
72 * @param string $config_resource config file resource name
73 * @param Smarty $smarty Smarty instance
74 * @param object $data object for config vars storage
76 public function __construct($config_resource, $smarty, $data = null)
79 $this->smarty = $smarty;
80 $this->config_resource = $config_resource;
84 * Returns the compiled filepath
86 * @return string the compiled filepath
88 public function getCompiledFilepath()
90 return $this->compiled_filepath === null ?
91 ($this->compiled_filepath = $this->buildCompiledFilepath()) :
92 $this->compiled_filepath;
100 public function buildCompiledFilepath()
102 $_compile_id = isset($this->smarty->compile_id) ? preg_replace('![^\w\|]+!', '_', $this->smarty->compile_id) : null;
103 $_flag = (int) $this->smarty->config_read_hidden + (int) $this->smarty->config_booleanize * 2
104 + (int) $this->smarty->config_overwrite * 4;
105 $_filepath = sha1(realpath($this->source->filepath) . $_flag);
106 // if use_sub_dirs, break file into directories
107 if ($this->smarty->use_sub_dirs) {
108 $_filepath = substr($_filepath, 0, 2) . DS
109 . substr($_filepath, 2, 2) . DS
110 . substr($_filepath, 4, 2) . DS
113 $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
114 if (isset($_compile_id)) {
115 $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
117 $_compile_dir = $this->smarty->getCompileDir();
119 return $_compile_dir . $_filepath . '.' . basename($this->source->name) . '.config' . '.php';
123 * Returns the timestamp of the compiled file
125 * @return integer the file timestamp
127 public function getCompiledTimestamp()
129 return $this->compiled_timestamp === null
130 ? ($this->compiled_timestamp = (file_exists($this->getCompiledFilepath())) ? filemtime($this->getCompiledFilepath()) : false)
131 : $this->compiled_timestamp;
135 * Returns if the current config file must be compiled
136 * It does compare the timestamps of config source and the compiled config and checks the force compile configuration
138 * @return boolean true if the file must be compiled
140 public function mustCompile()
142 return $this->mustCompile === null ?
143 $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp() === false || $this->smarty->compile_check && $this->getCompiledTimestamp() < $this->source->timestamp) :
148 * Returns the compiled config file
149 * It checks if the config file must be compiled or just read the compiled version
151 * @return string the compiled config file
153 public function getCompiledConfig()
155 if ($this->compiled_config === null) {
156 // see if template needs compiling.
157 if ($this->mustCompile()) {
158 $this->compileConfigSource();
160 $this->compiled_config = file_get_contents($this->getCompiledFilepath());
164 return $this->compiled_config;
168 * Compiles the config files
172 public function compileConfigSource()
175 if (!is_object($this->compiler_object)) {
177 $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
180 if ($this->smarty->compile_locking) {
181 if ($saved_timestamp = $this->getCompiledTimestamp()) {
182 touch($this->getCompiledFilepath());
187 $this->compiler_object->compileSource($this);
189 catch (Exception $e) {
190 // restore old timestamp in case of error
191 if ($this->smarty->compile_locking && $saved_timestamp) {
192 touch($this->getCompiledFilepath(), $saved_timestamp);
196 // compiling succeeded
197 // write compiled template
198 Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty);
202 * load config variables
204 * @param mixed $sections array of section names, single section or null
205 * @param string $scope global,parent or local
209 public function loadConfigVars($sections = null, $scope = 'local')
211 if ($this->data instanceof Smarty_Internal_Template) {
212 $this->data->properties['file_dependency'][sha1($this->source->filepath)] = array($this->source->filepath, $this->source->timestamp, 'file');
214 if ($this->mustCompile()) {
215 $this->compileConfigSource();
218 if ($scope == 'local') {
219 $scope_ptr = $this->data;
220 } elseif ($scope == 'parent') {
221 if (isset($this->data->parent)) {
222 $scope_ptr = $this->data->parent;
224 $scope_ptr = $this->data;
226 } elseif ($scope == 'root' || $scope == 'global') {
227 $scope_ptr = $this->data;
228 while (isset($scope_ptr->parent)) {
229 $scope_ptr = $scope_ptr->parent;
232 $_config_vars = array();
233 include($this->getCompiledFilepath());
234 // copy global config vars
235 foreach ($_config_vars['vars'] as $variable => $value) {
236 if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
237 $scope_ptr->config_vars[$variable] = $value;
239 $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
243 if (!empty($sections)) {
244 foreach ((array) $sections as $this_section) {
245 if (isset($_config_vars['sections'][$this_section])) {
246 foreach ($_config_vars['sections'][$this_section]['vars'] as $variable => $value) {
247 if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
248 $scope_ptr->config_vars[$variable] = $value;
250 $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
259 * set Smarty property in template context
261 * @param string $property_name property name
262 * @param mixed $value value
264 * @throws SmartyException if $property_name is not valid
266 public function __set($property_name, $value)
268 switch ($property_name) {
271 $this->$property_name = $value;
276 throw new SmartyException("invalid config property '$property_name'.");
280 * get Smarty property in template context
282 * @param string $property_name property name
284 * @return \Smarty_Config_Source|\Smarty_Template_Compiled
285 * @throws SmartyException if $property_name is not valid
287 public function __get($property_name)
289 switch ($property_name) {
291 if (empty($this->config_resource)) {
292 throw new SmartyException("Unable to parse resource name \"{$this->config_resource}\"");
294 $this->source = Smarty_Resource::config($this);
296 return $this->source;
299 $this->compiled = $this->source->getCompiled($this);
301 return $this->compiled;
304 throw new SmartyException("config attribute '$property_name' does not exist.");