smarty_internal_method_compilealltemplates.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 passed smarty object
  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,
  33. $max_errors = null)
  34. {
  35. return $this->compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors);
  36. }
  37. /**
  38. * Compile all template or config files
  39. *
  40. * @param \Smarty $smarty
  41. * @param string $extension template file name extension
  42. * @param bool $force_compile force all to recompile
  43. * @param int $time_limit set maximum execution time
  44. * @param int $max_errors set maximum allowed errors
  45. * @param bool $isConfig flag true if called for config files
  46. *
  47. * @return int number of template files compiled
  48. */
  49. protected function compileAll(Smarty $smarty, $extension, $force_compile, $time_limit, $max_errors,
  50. $isConfig = false)
  51. {
  52. // switch off time limit
  53. if (function_exists('set_time_limit')) {
  54. @set_time_limit($time_limit);
  55. }
  56. $_count = 0;
  57. $_error_count = 0;
  58. $sourceDir = $isConfig ? $smarty->getConfigDir() : $smarty->getTemplateDir();
  59. // loop over array of source directories
  60. foreach ($sourceDir as $_dir) {
  61. $_dir_1 = new RecursiveDirectoryIterator($_dir, defined('FilesystemIterator::FOLLOW_SYMLINKS') ?
  62. FilesystemIterator::FOLLOW_SYMLINKS : 0);
  63. $_dir_2 = new RecursiveIteratorIterator($_dir_1);
  64. foreach ($_dir_2 as $_fileinfo) {
  65. $_file = $_fileinfo->getFilename();
  66. if (substr(basename($_fileinfo->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) {
  67. continue;
  68. }
  69. if (!substr_compare($_file, $extension, - strlen($extension)) == 0) {
  70. continue;
  71. }
  72. if ($_fileinfo->getPath() !== substr($_dir, 0, - 1)) {
  73. $_file = substr($_fileinfo->getPath(), strlen($_dir)) . $smarty->ds . $_file;
  74. }
  75. echo "\n<br>", $_dir, '---', $_file;
  76. flush();
  77. $_start_time = microtime(true);
  78. $_smarty = clone $smarty;
  79. //
  80. $_smarty->_cache = array();
  81. $_smarty->ext = new Smarty_Internal_Extension_Handler();
  82. $_smarty->ext->objType = $_smarty->_objType;
  83. $_smarty->force_compile = $force_compile;
  84. try {
  85. /* @var Smarty_Internal_Template $_tpl */
  86. $_tpl = new $smarty->template_class($_file, $_smarty);
  87. $_tpl->caching = Smarty::CACHING_OFF;
  88. $_tpl->source =
  89. $isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl);
  90. if ($_tpl->mustCompile()) {
  91. $_tpl->compileTemplateSource();
  92. $_count ++;
  93. echo ' compiled in ', microtime(true) - $_start_time, ' seconds';
  94. flush();
  95. } else {
  96. echo ' is up to date';
  97. flush();
  98. }
  99. }
  100. catch (Exception $e) {
  101. echo "\n<br> ------>Error: ", $e->getMessage(), "<br><br>\n";
  102. $_error_count ++;
  103. }
  104. // free memory
  105. unset($_tpl);
  106. $_smarty->_clearTemplateCache();
  107. if ($max_errors !== null && $_error_count == $max_errors) {
  108. echo "\n<br><br>too many errors\n";
  109. exit();
  110. }
  111. }
  112. }
  113. echo "\n<br>";
  114. return $_count;
  115. }
  116. }