Няма описание
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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\template\driver;
  12. use think\Exception;
  13. class File
  14. {
  15. protected $cacheFile;
  16. /*
  17. * fwrite 方式写入编译文件 by 许
  18. */
  19. public function fwrite($cacheFile, $content)
  20. {
  21. // 检测模板目录
  22. $dir = dirname($cacheFile);
  23. if (!is_dir($dir)) {
  24. mkdir($dir, 0755, true);
  25. }
  26. // 生成模板缓存文件
  27. $fp = fopen($cacheFile,"w");
  28. if (false === fwrite($fp, $content)) {
  29. throw new Exception('cache write error:' . $cacheFile, 11602);
  30. }
  31. fclose($fp);
  32. }
  33. /**
  34. * 写入编译缓存
  35. * @param string $cacheFile 缓存的文件名
  36. * @param string $content 缓存的内容
  37. * @return void|array
  38. */
  39. public function write($cacheFile, $content)
  40. {
  41. // 检测模板目录
  42. $dir = dirname($cacheFile);
  43. if (!is_dir($dir)) {
  44. mkdir($dir, 0755, true);
  45. }
  46. // 生成模板缓存文件
  47. if (false === file_put_contents($cacheFile, $content)) {
  48. throw new Exception('cache write error:' . $cacheFile, 11602);
  49. }
  50. }
  51. /**
  52. * 读取编译编译
  53. * @param string $cacheFile 缓存的文件名
  54. * @param array $vars 变量数组
  55. * @return void
  56. */
  57. public function read($cacheFile, $vars = [])
  58. {
  59. $this->cacheFile = $cacheFile;
  60. if (!empty($vars) && is_array($vars)) {
  61. // 模板阵列变量分解成为独立变量
  62. extract($vars, EXTR_OVERWRITE);
  63. }
  64. //载入模版缓存文件
  65. include $this->cacheFile;
  66. }
  67. /**
  68. * 检查编译缓存是否有效
  69. * @param string $cacheFile 缓存的文件名
  70. * @param int $cacheTime 缓存时间
  71. * @return boolean
  72. */
  73. public function check($cacheFile, $cacheTime)
  74. {
  75. // 缓存文件不存在, 直接返回false
  76. if (!file_exists($cacheFile)) {
  77. return false;
  78. }
  79. if (0 != $cacheTime && $_SERVER['REQUEST_TIME'] > filemtime($cacheFile) + $cacheTime) {
  80. // 缓存是否在有效期
  81. return false;
  82. }
  83. return true;
  84. }
  85. }