PageRenderTime 32ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/engine/_lib/smarty/sysplugins/smarty_internal_utility.php

https://github.com/raphaelbastide/berta
PHP | 276 lines | 193 code | 21 blank | 62 comment | 50 complexity | ee0ee25f3ced3ea8a1ed3e7011fd65bd MD5 | raw file
  1. <?php
  2. /**
  3. * Project: Smarty: the PHP compiling template engine
  4. * File: smarty_internal_utility.php
  5. * SVN: $Id: $
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * For questions, help, comments, discussion, etc., please join the
  22. * Smarty mailing list. Send a blank e-mail to
  23. * smarty-discussion-subscribe@googlegroups.com
  24. *
  25. * @link http://www.smarty.net/
  26. * @copyright 2008 New Digital Group, Inc.
  27. * @author Monte Ohrt <monte at ohrt dot com>
  28. * @author Uwe Tews
  29. * @package Smarty
  30. * @subpackage PluginsInternal
  31. * @version 3-SVN$Rev: 3286 $
  32. */
  33. class Smarty_Internal_Utility {
  34. protected $smarty;
  35. function __construct($smarty)
  36. {
  37. $this->smarty = $smarty;
  38. }
  39. /**
  40. * Compile all template files
  41. *
  42. * @param string $extension file extension
  43. * @param bool $force_compile force all to recompile
  44. * @param int $time_limit
  45. * @param int $max_errors
  46. * @return integer number of template files recompiled
  47. */
  48. function compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
  49. {
  50. // switch off time limit
  51. if (function_exists('set_time_limit')) {
  52. @set_time_limit($time_limit);
  53. }
  54. $this->smarty->force_compile = $force_compile;
  55. $_count = 0;
  56. $_error_count = 0;
  57. // loop over array of template directories
  58. foreach((array)$this->smarty->template_dir as $_dir) {
  59. $_compileDirs = new RecursiveDirectoryIterator($_dir);
  60. $_compile = new RecursiveIteratorIterator($_compileDirs);
  61. foreach ($_compile as $_fileinfo) {
  62. if (strpos($_fileinfo, '.svn') !== false) continue;
  63. $_file = $_fileinfo->getFilename();
  64. if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
  65. if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
  66. $_template_file = $_file;
  67. } else {
  68. $_template_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
  69. }
  70. echo '<br>', $_dir, '---', $_template_file;
  71. flush();
  72. $_start_time = microtime(true);
  73. try {
  74. $_tpl = $this->smarty->createTemplate($_template_file);
  75. if ($_tpl->mustCompile()) {
  76. $_tpl->compileTemplateSource();
  77. echo ' compiled in ', microtime(true) - $_start_time, ' seconds';
  78. flush();
  79. } else {
  80. echo ' is up to date';
  81. flush();
  82. }
  83. }
  84. catch (Exception $e) {
  85. echo 'Error: ', $e->getMessage(), "<br><br>";
  86. $_error_count++;
  87. }
  88. if ($max_errors !== null && $_error_count == $max_errors) {
  89. echo '<br><br>too many errors';
  90. exit();
  91. }
  92. }
  93. }
  94. return $_count;
  95. }
  96. /**
  97. * Compile all config files
  98. *
  99. * @param string $extension file extension
  100. * @param bool $force_compile force all to recompile
  101. * @param int $time_limit
  102. * @param int $max_errors
  103. * @return integer number of template files recompiled
  104. */
  105. function compileAllConfig($extention = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
  106. {
  107. // switch off time limit
  108. if (function_exists('set_time_limit')) {
  109. @set_time_limit($time_limit);
  110. }
  111. $this->smarty->force_compile = $force_compile;
  112. $_count = 0;
  113. $_error_count = 0;
  114. // loop over array of template directories
  115. foreach((array)$this->smarty->config_dir as $_dir) {
  116. $_compileDirs = new RecursiveDirectoryIterator($_dir);
  117. $_compile = new RecursiveIteratorIterator($_compileDirs);
  118. foreach ($_compile as $_fileinfo) {
  119. if (strpos($_fileinfo, '.svn') !== false) continue;
  120. $_file = $_fileinfo->getFilename();
  121. if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
  122. if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
  123. $_config_file = $_file;
  124. } else {
  125. $_config_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
  126. }
  127. echo '<br>', $_dir, '---', $_config_file;
  128. flush();
  129. $_start_time = microtime(true);
  130. try {
  131. $_config = new Smarty_Internal_Config($_config_file, $this->smarty);
  132. if ($_config->mustCompile()) {
  133. $_config->compileConfigSource();
  134. echo ' compiled in ', microtime(true) - $_start_time, ' seconds';
  135. flush();
  136. } else {
  137. echo ' is up to date';
  138. flush();
  139. }
  140. }
  141. catch (Exception $e) {
  142. echo 'Error: ', $e->getMessage(), "<br><br>";
  143. $_error_count++;
  144. }
  145. if ($max_errors !== null && $_error_count == $max_errors) {
  146. echo '<br><br>too many errors';
  147. exit();
  148. }
  149. }
  150. }
  151. return $_count;
  152. }
  153. /**
  154. * Delete compiled template file
  155. *
  156. * @param string $resource_name template name
  157. * @param string $compile_id compile id
  158. * @param integer $exp_time expiration time
  159. * @return integer number of template files deleted
  160. */
  161. function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
  162. {
  163. $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
  164. $_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
  165. if (isset($resource_name)) {
  166. $_resource_part_1 = $resource_name . '.php';
  167. $_resource_part_2 = $resource_name . '.cache' . '.php';
  168. } else {
  169. $_resource_part = '';
  170. }
  171. $_dir = $this->smarty->compile_dir;
  172. if ($this->smarty->use_sub_dirs && isset($_compile_id)) {
  173. $_dir .= $_compile_id . $_dir_sep;
  174. }
  175. if (isset($_compile_id)) {
  176. $_compile_id_part = $this->smarty->compile_dir . $_compile_id . $_dir_sep;
  177. }
  178. $_count = 0;
  179. $_compileDirs = new RecursiveDirectoryIterator($_dir);
  180. $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
  181. foreach ($_compile as $_file) {
  182. if (strpos($_file, '.svn') !== false) continue;
  183. if ($_file->isDir()) {
  184. if (!$_compile->isDot()) {
  185. // delete folder if empty
  186. @rmdir($_file->getPathname());
  187. }
  188. } else {
  189. if ((!isset($_compile_id) || (strlen((string)$_file) > strlen($_compile_id_part) && substr_compare((string)$_file, $_compile_id_part, 0, strlen($_compile_id_part)) == 0)) &&
  190. (!isset($resource_name) || (strlen((string)$_file) > strlen($_resource_part_1) && substr_compare((string)$_file, $_resource_part_1, - strlen($_resource_part_1), strlen($_resource_part_1)) == 0) ||
  191. (strlen((string)$_file) > strlen($_resource_part_2) && substr_compare((string)$_file, $_resource_part_2, - strlen($_resource_part_2), strlen($_resource_part_2)) == 0))) {
  192. if (isset($exp_time)) {
  193. if (time() - @filemtime($_file) >= $exp_time) {
  194. $_count += @unlink((string) $_file) ? 1 : 0;
  195. }
  196. } else {
  197. $_count += @unlink((string) $_file) ? 1 : 0;
  198. }
  199. }
  200. }
  201. }
  202. return $_count;
  203. }
  204. function testInstall()
  205. {
  206. echo "<PRE>\n";
  207. echo "Smarty Installation test...\n";
  208. echo "Testing template directory...\n";
  209. foreach((array)$this->smarty->template_dir as $template_dir) {
  210. if (!is_dir($template_dir))
  211. echo "FAILED: $template_dir is not a directory.\n";
  212. elseif (!is_readable($template_dir))
  213. echo "FAILED: $template_dir is not readable.\n";
  214. else
  215. echo "$template_dir is OK.\n";
  216. }
  217. echo "Testing compile directory...\n";
  218. if (!is_dir($this->smarty->compile_dir))
  219. echo "FAILED: {$this->smarty->compile_dir} is not a directory.\n";
  220. elseif (!is_readable($this->smarty->compile_dir))
  221. echo "FAILED: {$this->smarty->compile_dir} is not readable.\n";
  222. elseif (!is_writable($this->smarty->compile_dir))
  223. echo "FAILED: {$this->smarty->compile_dir} is not writable.\n";
  224. else
  225. echo "{$this->smarty->compile_dir} is OK.\n";
  226. echo "Testing plugins directory...\n";
  227. foreach((array)$this->smarty->plugins_dir as $plugin_dir) {
  228. if (!is_dir($plugin_dir))
  229. echo "FAILED: $plugin_dir is not a directory.\n";
  230. elseif (!is_readable($plugin_dir))
  231. echo "FAILED: $plugin_dir is not readable.\n";
  232. else
  233. echo "$plugin_dir is OK.\n";
  234. }
  235. echo "Testing cache directory...\n";
  236. if (!is_dir($this->smarty->cache_dir))
  237. echo "FAILED: {$this->smarty->cache_dir} is not a directory.\n";
  238. elseif (!is_readable($this->smarty->cache_dir))
  239. echo "FAILED: {$this->smarty->cache_dir} is not readable.\n";
  240. elseif (!is_writable($this->smarty->cache_dir))
  241. echo "FAILED: {$this->smarty->cache_dir} is not writable.\n";
  242. else
  243. echo "{$this->smarty->cache_dir} is OK.\n";
  244. echo "Testing configs directory...\n";
  245. if (!is_dir($this->smarty->config_dir))
  246. echo "FAILED: {$this->smarty->config_dir} is not a directory.\n";
  247. elseif (!is_readable($this->smarty->config_dir))
  248. echo "FAILED: {$this->smarty->config_dir} is not readable.\n";
  249. else
  250. echo "{$this->smarty->config_dir} is OK.\n";
  251. echo "Tests complete.\n";
  252. echo "</PRE>\n";
  253. return true;
  254. }
  255. }