PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/vendors/smarty/sysplugins/smarty_internal_config.php

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