3 * Smarty Internal Plugin Config
11 * Smarty Internal Plugin Config
13 * Main class for config variables
18 * @property Smarty_Config_Source $source
19 * @property Smarty_Config_Compiled $compiled
22 class Smarty_Internal_Config {
29 public $smarty = null;
31 * Object of config var storage
40 public $config_resource = null;
42 * Compiled config file
46 public $compiled_config = null;
48 * filepath of compiled config file
52 public $compiled_filepath = null;
54 * Filemtime of compiled config Filemtime
58 public $compiled_timestamp = null;
60 * flag if compiled config file is invalid and must be (re)compiled
63 public $mustCompile = null;
65 * Config file compiler object
67 * @var Smarty_Internal_Config_File_Compiler object
69 public $compiler_object = null;
72 * Constructor of config file object
74 * @param string $config_resource config file resource name
75 * @param Smarty $smarty Smarty instance
76 * @param object $data object for config vars storage
78 public function __construct($config_resource, $smarty, $data = null)
81 $this->smarty = $smarty;
82 $this->config_resource = $config_resource;
86 * Returns the compiled filepath
88 * @return string the compiled filepath
90 public function getCompiledFilepath()
92 return $this->compiled_filepath === null ?
93 ($this->compiled_filepath = $this->buildCompiledFilepath()) :
94 $this->compiled_filepath;
102 public function buildCompiledFilepath()
104 $_compile_id = isset($this->smarty->compile_id) ? preg_replace('![^\w\|]+!', '_', $this->smarty->compile_id) : null;
105 $_flag = (int) $this->smarty->config_read_hidden + (int) $this->smarty->config_booleanize * 2
106 + (int) $this->smarty->config_overwrite * 4;
107 $_filepath = sha1($this->source->name . $_flag);
108 // if use_sub_dirs, break file into directories
109 if ($this->smarty->use_sub_dirs) {
110 $_filepath = substr($_filepath, 0, 2) . DS
111 . substr($_filepath, 2, 2) . DS
112 . substr($_filepath, 4, 2) . DS
115 $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
116 if (isset($_compile_id)) {
117 $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
119 $_compile_dir = $this->smarty->getCompileDir();
120 return $_compile_dir . $_filepath . '.' . basename($this->source->name) . '.config' . '.php';
124 * Returns the timpestamp of the compiled file
126 * @return integer the file timestamp
128 public function getCompiledTimestamp()
130 return $this->compiled_timestamp === null
131 ? ($this->compiled_timestamp = (file_exists($this->getCompiledFilepath())) ? filemtime($this->getCompiledFilepath()) : false)
132 : $this->compiled_timestamp;
136 * Returns if the current config file must be compiled
138 * It does compare the timestamps of config source and the compiled config and checks the force compile configuration
140 * @return boolean true if the file must be compiled
142 public function mustCompile()
144 return $this->mustCompile === null ?
145 $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp () === false || $this->smarty->compile_check && $this->getCompiledTimestamp () < $this->source->timestamp):
150 * Returns the compiled config file
152 * It checks if the config file must be compiled or just read the compiled version
154 * @return string the compiled config file
156 public function getCompiledConfig()
158 if ($this->compiled_config === null) {
159 // see if template needs compiling.
160 if ($this->mustCompile()) {
161 $this->compileConfigSource();
163 $this->compiled_config = file_get_contents($this->getCompiledFilepath());
166 return $this->compiled_config;
170 * Compiles the config files
174 public function compileConfigSource()
177 if (!is_object($this->compiler_object)) {
179 $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
182 if ($this->smarty->compile_locking) {
183 if ($saved_timestamp = $this->getCompiledTimestamp()) {
184 touch($this->getCompiledFilepath());
189 $this->compiler_object->compileSource($this);
190 } catch (Exception $e) {
191 // restore old timestamp in case of error
192 if ($this->smarty->compile_locking && $saved_timestamp) {
193 touch($this->getCompiledFilepath(), $saved_timestamp);
197 // compiling succeded
198 // write compiled template
199 Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty);
203 * load config variables
205 * @param mixed $sections array of section names, single section or null
206 * @param object $scope global,parent or local
208 public function loadConfigVars($sections = null, $scope = 'local')
210 if ($this->data instanceof Smarty_Internal_Template) {
211 $this->data->properties['file_dependency'][sha1($this->source->filepath)] = array($this->source->filepath, $this->source->timestamp, 'file');
213 if ($this->mustCompile()) {
214 $this->compileConfigSource();
217 if ($scope == 'local') {
218 $scope_ptr = $this->data;
219 } elseif ($scope == 'parent') {
220 if (isset($this->data->parent)) {
221 $scope_ptr = $this->data->parent;
223 $scope_ptr = $this->data;
225 } elseif ($scope == 'root' || $scope == 'global') {
226 $scope_ptr = $this->data;
227 while (isset($scope_ptr->parent)) {
228 $scope_ptr = $scope_ptr->parent;
231 $_config_vars = array();
232 include($this->getCompiledFilepath());
233 // copy global config vars
234 foreach ($_config_vars['vars'] as $variable => $value) {
235 if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
236 $scope_ptr->config_vars[$variable] = $value;
238 $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
242 if (!empty($sections)) {
243 $sections = array_flip((array) $sections);
244 foreach ($_config_vars['sections'] as $this_section => $dummy) {
245 if (isset($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
263 * @throws SmartyException if $property_name is not valid
265 public function __set($property_name, $value)
267 switch ($property_name) {
270 $this->$property_name = $value;
274 throw new SmartyException("invalid config property '$property_name'.");
278 * get Smarty property in template context
280 * @param string $property_name property name
281 * @throws SmartyException if $property_name is not valid
283 public function __get($property_name)
285 switch ($property_name) {
287 if (empty($this->config_resource)) {
288 throw new SmartyException("Unable to parse resource name \"{$this->config_resource}\"");
290 $this->source = Smarty_Resource::config($this);
291 return $this->source;
294 $this->compiled = $this->source->getCompiled($this);
295 return $this->compiled;
298 throw new SmartyException("config attribute '$property_name' does not exist.");