Code coverage report for thinkjs/lib/Lib/Behavior/WriteHtmlCacheBehavior.js

Statements: 100% (18 / 18)      Branches: 100% (4 / 4)      Functions: 100% (4 / 4)      Lines: 100% (18 / 18)      Ignored: none     

All files » thinkjs/lib/Lib/Behavior/ » WriteHtmlCacheBehavior.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51        2 2   2         2   2           6 5   1 1 1   1 1             1 1 1                 2   4 4  
/**
 * 模版文件列表
 * @type {Object}
 */
var path = require('path');
var fs = require('fs');
 
var tplFiles = {};
/**
 * 写入html缓存
 * @return {[type]} [description]
 */
module.exports = Behavior(function(){
  'use strict';
  return {
    options: {
      'html_cache_on': false, //是否开启缓存
      'html_cache_path': ''
    },
    run: function(content){
      if (!this.options.html_cache_on || !this.http.html_filename) {
        return content;
      }
      this.recordViewFile();
      var file = this.options.html_cache_path + '/' + this.http.html_filename;
      mkdir(path.dirname(file));
      //异步方式写入缓存
      fs.writeFile(file, content);
      return content;
    },
    /**
     * 记录模版文件名
     * @return {[type]} [description]
     */
    recordViewFile: function(){
      var tplFile = this.http.tpl_file;
      var key = this.http.group + ':' + this.http.controller + ':' + this.http.action;
      tplFiles[key] = tplFile;
    }
  };
});
/**
 * 获取模版文件
 * @param  {[type]} http [description]
 * @return {[type]}      [description]
 */
module.exports.getViewFile = function(http){
  'use strict';
  var key = http.group + ':' + http.controller + ':' + http.action;
  return tplFiles[key];
};