PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/bors/templates/smarty/resources/file.php

https://bitbucket.org/Balancer/bors-core
PHP | 148 lines | 110 code | 22 blank | 16 comment | 19 complexity | 42444ab7f7fb200eb46cf45e443e7e4c MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. class bors_templates_smarty_resources_file extends Smarty_Resource_Custom
  3. {
  4. private $smarty = NULL;
  5. function __construct($smarty)
  6. {
  7. $this->smarty = $smarty;
  8. }
  9. /**
  10. * Fetch a template and its modification time from database
  11. *
  12. * @param string $name template name
  13. * @param string $source template source
  14. * @param integer $mtime template modification timestamp (epoch)
  15. * @return void
  16. */
  17. public function fetch($tpl_name, &$tpl_source, &$mtime)
  18. {
  19. if(file_exists($tpl_name))
  20. {
  21. $tpl_source = ec(file_get_contents($tpl_name));
  22. $mtime = filemtime($tpl_name);
  23. return;
  24. }
  25. if(($dirs = $this->smarty->getTemplateVars('template_dirnames')))
  26. {
  27. foreach($dirs as $dir)
  28. {
  29. if(file_exists($fn = str_replace('xfile:', '', $dir)."/".$tpl_name))
  30. {
  31. $tpl_source = ec(file_get_contents($fn));
  32. $mtime = filemtime($fn);
  33. return;
  34. }
  35. }
  36. }
  37. foreach($this->smarty->getTemplateDir() as $d)
  38. {
  39. if(file_exists($fn = "$d/$tpl_name"))
  40. {
  41. $tpl_source = ec(file_get_contents($fn));
  42. $mtime = filemtime($fn);
  43. return;
  44. }
  45. }
  46. foreach(bors_dirs(true) as $dir)
  47. {
  48. if(file_exists($fn = $dir.'/templates/'.$tpl_name))
  49. {
  50. $tpl_source = ec(file_get_contents($fn));
  51. $mtime = filemtime($fn);
  52. return;
  53. }
  54. if(file_exists($fn = $dir.'/'.$tpl_name))
  55. {
  56. $tpl_source = ec(file_get_contents($fn));
  57. $mtime = filemtime($fn);
  58. return;
  59. }
  60. }
  61. $tpl_source = NULL;
  62. $mtime = NULL;
  63. return;
  64. }
  65. /**
  66. * Fetch a template's modification time from database
  67. *
  68. * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the comple template source.
  69. * @param string $name template name
  70. * @return integer timestamp (epoch) the template was modified
  71. */
  72. protected function fetchTimestamp($tpl_name)
  73. {
  74. // echo "Fetch TS for $tpl_name<br/>\n";
  75. static $cache;
  76. if(!empty($cache[$tpl_name]))
  77. return $cache[$tpl_name];
  78. $tpl_timestamp = NULL;
  79. if(file_exists($tpl_name))
  80. $tpl_timestamp = filemtime($tpl_name);
  81. if(($dirs = $this->smarty->getTemplateVars('template_dirnames')))
  82. {
  83. foreach($dirs as $dir)
  84. {
  85. if(!$tpl_timestamp && file_exists($fn = str_replace('xfile:', '', $dir)."/".$tpl_name))
  86. {
  87. $tpl_timestamp = filemtime($fn);
  88. break;
  89. }
  90. }
  91. }
  92. foreach($this->smarty->getTemplateDir() as $d)
  93. if(!$tpl_timestamp && file_exists($fn = "$d/$tpl_name"))
  94. $tpl_timestamp = filemtime($fn);
  95. $find_tpl = '/templates/'.$tpl_name;
  96. $find_classes_tpl = '/'.$tpl_name;
  97. $default_template_dir = '/templates/'.dirname(config('default_template')).'/'.$tpl_name;
  98. if(!$tpl_timestamp)
  99. {
  100. foreach(bors_dirs(true) as $dir)
  101. {
  102. if(file_exists($fn = $dir.$find_tpl))
  103. {
  104. $tpl_timestamp = filemtime($fn);
  105. break;
  106. }
  107. if(file_exists($fn = $dir.$find_classes_tpl))
  108. {
  109. $tpl_timestamp = filemtime($fn);
  110. break;
  111. }
  112. if(file_exists($fn = $dir.$default_template_dir))
  113. {
  114. $tpl_timestamp = filemtime($fn);
  115. break;
  116. }
  117. }
  118. }
  119. if(!$tpl_timestamp)
  120. return $tpl_timestamp;
  121. if(config('templates_cache_disabled'))
  122. $tpl_timestamp = time();
  123. $cache[$tpl_name] = $tpl_timestamp;
  124. return $tpl_timestamp;
  125. }
  126. }