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.7KB

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