You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_method_compilealltemplates.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Smarty Method CompileAllTemplates
  4. *
  5. * Smarty::compileAllTemplates() method
  6. *
  7. * @package Smarty
  8. * @subpackage PluginsInternal
  9. * @author Uwe Tews
  10. */
  11. class Smarty_Internal_Method_CompileAllTemplates
  12. {
  13. /**
  14. * Valid for Smarty object
  15. *
  16. * @var int
  17. */
  18. public $objMap = 1;
  19. /**
  20. * Compile all template files
  21. *
  22. * @api Smarty::compileAllTemplates()
  23. *
  24. * @param \Smarty $smarty
  25. * @param string $extension file extension
  26. * @param bool $force_compile force all to recompile
  27. * @param int $time_limit
  28. * @param int $max_errors
  29. *
  30. * @return integer number of template files recompiled
  31. */
  32. public function compileAllTemplates(Smarty $smarty, $extension = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
  33. {
  34. return $this->compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors);
  35. }
  36. /**
  37. * Compile all template or config files
  38. *
  39. * @param \Smarty $smarty
  40. * @param string $extension template file name extension
  41. * @param bool $force_compile force all to recompile
  42. * @param int $time_limit set maximum execution time
  43. * @param int $max_errors set maximum allowed errors
  44. * @param bool $isConfig flag true if called for config files
  45. *
  46. * @return int number of template files compiled
  47. */
  48. protected function compileAll(Smarty $smarty, $extension, $force_compile, $time_limit, $max_errors, $isConfig = false)
  49. {
  50. // switch off time limit
  51. if (function_exists('set_time_limit')) {
  52. @set_time_limit($time_limit);
  53. }
  54. $_count = 0;
  55. $_error_count = 0;
  56. $sourceDir = $isConfig ? $smarty->getConfigDir() : $smarty->getTemplateDir();
  57. // loop over array of source directories
  58. foreach ($sourceDir as $_dir) {
  59. $_dir_1 = new RecursiveDirectoryIterator($_dir);
  60. $_dir_2 = new RecursiveIteratorIterator($_dir_1);
  61. foreach ($_dir_2 as $_fileinfo) {
  62. $_file = $_fileinfo->getFilename();
  63. if (substr(basename($_fileinfo->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) {
  64. continue;
  65. }
  66. if (!substr_compare($_file, $extension, - strlen($extension)) == 0) {
  67. continue;
  68. }
  69. if ($_fileinfo->getPath() == !substr($_dir, 0, - 1)) {
  70. $_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
  71. }
  72. echo "\n<br>", $_dir, '---', $_file;
  73. flush();
  74. $_start_time = microtime(true);
  75. $_smarty = clone $smarty;
  76. $_smarty->force_compile = $force_compile;
  77. try {
  78. /* @var Smarty_Internal_Template $_tpl */
  79. $_tpl = new $smarty->template_class($_file, $_smarty);
  80. $_tpl->caching = Smarty::CACHING_OFF;
  81. $_tpl->source = $isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl);
  82. if ($_tpl->mustCompile()) {
  83. $_tpl->compileTemplateSource();
  84. $_count ++;
  85. echo ' compiled in ', microtime(true) - $_start_time, ' seconds';
  86. flush();
  87. } else {
  88. echo ' is up to date';
  89. flush();
  90. }
  91. }
  92. catch (Exception $e) {
  93. echo "\n<br> ------>Error: ", $e->getMessage(), "<br><br>\n";
  94. $_error_count ++;
  95. }
  96. // free memory
  97. unset($_tpl);
  98. $_smarty->_cache['template_objects'] = array();
  99. if ($max_errors !== null && $_error_count == $max_errors) {
  100. echo "\n<br><br>too many errors\n";
  101. exit();
  102. }
  103. }
  104. }
  105. echo "\n<br>";
  106. return $_count;
  107. }
  108. }