PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/sysplugins/smarty_internal_config.php

https://bitbucket.org/icefish/smarty-3
PHP | 302 lines | 156 code | 17 blank | 129 comment | 35 complexity | 762143ac566d46a23006e79b5f2a6fff MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Config
  4. *
  5. * @package Smarty
  6. * @subpackage Config
  7. * @author Uwe Tews
  8. */
  9. /**
  10. * Smarty Internal Plugin Config
  11. *
  12. * Main class for config variables
  13. *
  14. * @package Smarty
  15. * @subpackage Config
  16. *
  17. * @property Smarty_Config_Source $source
  18. * @property Smarty_Config_Compiled $compiled
  19. * @ignore
  20. */
  21. class Smarty_Internal_Config {
  22. /**
  23. * Samrty instance
  24. *
  25. * @var Smarty object
  26. */
  27. public $smarty = null;
  28. /**
  29. * Object of config var storage
  30. *
  31. * @var object
  32. */
  33. public $data = null;
  34. /**
  35. * Config resource
  36. * @var string
  37. */
  38. public $config_resource = null;
  39. /**
  40. * Compiled config file
  41. *
  42. * @var string
  43. */
  44. public $compiled_config = null;
  45. /**
  46. * filepath of compiled config file
  47. *
  48. * @var string
  49. */
  50. public $compiled_filepath = null;
  51. /**
  52. * Filemtime of compiled config Filemtime
  53. *
  54. * @var int
  55. */
  56. public $compiled_timestamp = null;
  57. /**
  58. * flag if compiled config file is invalid and must be (re)compiled
  59. * @var bool
  60. */
  61. public $mustCompile = null;
  62. /**
  63. * Config file compiler object
  64. *
  65. * @var Smarty_Internal_Config_File_Compiler object
  66. */
  67. public $compiler_object = null;
  68. /**
  69. * Constructor of config file object
  70. *
  71. * @param string $config_resource config file resource name
  72. * @param Smarty $smarty Smarty instance
  73. * @param object $data object for config vars storage
  74. */
  75. public function __construct($config_resource, $smarty, $data = null)
  76. {
  77. $this->data = $data;
  78. $this->smarty = $smarty;
  79. $this->config_resource = $config_resource;
  80. }
  81. /**
  82. * Returns the compiled filepath
  83. *
  84. * @return string the compiled filepath
  85. */
  86. public function getCompiledFilepath()
  87. {
  88. return $this->compiled_filepath === null ?
  89. ($this->compiled_filepath = $this->buildCompiledFilepath()) :
  90. $this->compiled_filepath;
  91. }
  92. /**
  93. * Get file path.
  94. *
  95. * @return string
  96. */
  97. public function buildCompiledFilepath()
  98. {
  99. $_compile_id = isset($this->smarty->compile_id) ? preg_replace('![^\w\|]+!', '_', $this->smarty->compile_id) : null;
  100. $_flag = (int) $this->smarty->config_read_hidden + (int) $this->smarty->config_booleanize * 2
  101. + (int) $this->smarty->config_overwrite * 4;
  102. $_filepath = sha1($this->source->name . $_flag);
  103. // if use_sub_dirs, break file into directories
  104. if ($this->smarty->use_sub_dirs) {
  105. $_filepath = substr($_filepath, 0, 2) . DS
  106. . substr($_filepath, 2, 2) . DS
  107. . substr($_filepath, 4, 2) . DS
  108. . $_filepath;
  109. }
  110. $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
  111. if (isset($_compile_id)) {
  112. $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
  113. }
  114. $_compile_dir = $this->smarty->getCompileDir();
  115. return $_compile_dir . $_filepath . '.' . basename($this->source->name) . '.config' . '.php';
  116. }
  117. /**
  118. * Returns the timpestamp of the compiled file
  119. *
  120. * @return integer the file timestamp
  121. */
  122. public function getCompiledTimestamp()
  123. {
  124. return $this->compiled_timestamp === null
  125. ? ($this->compiled_timestamp = (file_exists($this->getCompiledFilepath())) ? filemtime($this->getCompiledFilepath()) : false)
  126. : $this->compiled_timestamp;
  127. }
  128. /**
  129. * Returns if the current config file must be compiled
  130. *
  131. * It does compare the timestamps of config source and the compiled config and checks the force compile configuration
  132. *
  133. * @return boolean true if the file must be compiled
  134. */
  135. public function mustCompile()
  136. {
  137. return $this->mustCompile === null ?
  138. $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp () === false || $this->smarty->compile_check && $this->getCompiledTimestamp () < $this->source->timestamp):
  139. $this->mustCompile;
  140. }
  141. /**
  142. * Returns the compiled config file
  143. *
  144. * It checks if the config file must be compiled or just read the compiled version
  145. *
  146. * @return string the compiled config file
  147. */
  148. public function getCompiledConfig()
  149. {
  150. if ($this->compiled_config === null) {
  151. // see if template needs compiling.
  152. if ($this->mustCompile()) {
  153. $this->compileConfigSource();
  154. } else {
  155. $this->compiled_config = file_get_contents($this->getCompiledFilepath());
  156. }
  157. }
  158. return $this->compiled_config;
  159. }
  160. /**
  161. * Compiles the config files
  162. *
  163. * @throws Exception
  164. */
  165. public function compileConfigSource()
  166. {
  167. // compile template
  168. if (!is_object($this->compiler_object)) {
  169. // load compiler
  170. $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
  171. }
  172. // compile locking
  173. if ($this->smarty->compile_locking) {
  174. if ($saved_timestamp = $this->getCompiledTimestamp()) {
  175. touch($this->getCompiledFilepath());
  176. }
  177. }
  178. // call compiler
  179. try {
  180. $this->compiler_object->compileSource($this);
  181. } catch (Exception $e) {
  182. // restore old timestamp in case of error
  183. if ($this->smarty->compile_locking && $saved_timestamp) {
  184. touch($this->getCompiledFilepath(), $saved_timestamp);
  185. }
  186. throw $e;
  187. }
  188. // compiling succeded
  189. // write compiled template
  190. Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty);
  191. }
  192. /**
  193. * load config variables
  194. *
  195. * @param mixed $sections array of section names, single section or null
  196. * @param object $scope global,parent or local
  197. */
  198. public function loadConfigVars($sections = null, $scope = 'local')
  199. {
  200. if ($this->data instanceof Smarty_Internal_Template) {
  201. $this->data->properties['file_dependency'][sha1($this->source->filepath)] = array($this->source->filepath, $this->source->timestamp, 'file');
  202. }
  203. if ($this->mustCompile()) {
  204. $this->compileConfigSource();
  205. }
  206. // pointer to scope
  207. if ($scope == 'local') {
  208. $scope_ptr = $this->data;
  209. } elseif ($scope == 'parent') {
  210. if (isset($this->data->parent)) {
  211. $scope_ptr = $this->data->parent;
  212. } else {
  213. $scope_ptr = $this->data;
  214. }
  215. } elseif ($scope == 'root' || $scope == 'global') {
  216. $scope_ptr = $this->data;
  217. while (isset($scope_ptr->parent)) {
  218. $scope_ptr = $scope_ptr->parent;
  219. }
  220. }
  221. $_config_vars = array();
  222. include($this->getCompiledFilepath());
  223. // copy global config vars
  224. foreach ($_config_vars['vars'] as $variable => $value) {
  225. if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
  226. $scope_ptr->config_vars[$variable] = $value;
  227. } else {
  228. $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
  229. }
  230. }
  231. // scan sections
  232. if (!empty($sections)) {
  233. foreach ((array) $sections as $this_section) {
  234. if (isset($_config_vars['sections'][$this_section])) {
  235. foreach ($_config_vars['sections'][$this_section]['vars'] as $variable => $value) {
  236. if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
  237. $scope_ptr->config_vars[$variable] = $value;
  238. } else {
  239. $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
  240. }
  241. }
  242. }
  243. }
  244. }
  245. }
  246. /**
  247. * set Smarty property in template context
  248. *
  249. * @param string $property_name property name
  250. * @param mixed $value value
  251. * @throws SmartyException if $property_name is not valid
  252. */
  253. public function __set($property_name, $value)
  254. {
  255. switch ($property_name) {
  256. case 'source':
  257. case 'compiled':
  258. $this->$property_name = $value;
  259. return;
  260. }
  261. throw new SmartyException("invalid config property '$property_name'.");
  262. }
  263. /**
  264. * get Smarty property in template context
  265. *
  266. * @param string $property_name property name
  267. * @throws SmartyException if $property_name is not valid
  268. */
  269. public function __get($property_name)
  270. {
  271. switch ($property_name) {
  272. case 'source':
  273. if (empty($this->config_resource)) {
  274. throw new SmartyException("Unable to parse resource name \"{$this->config_resource}\"");
  275. }
  276. $this->source = Smarty_Resource::config($this);
  277. return $this->source;
  278. case 'compiled':
  279. $this->compiled = $this->source->getCompiled($this);
  280. return $this->compiled;
  281. }
  282. throw new SmartyException("config attribute '$property_name' does not exist.");
  283. }
  284. }
  285. ?>