all files / src/ cache.js

60.71% Statements 17/28
25% Branches 3/12
50% Functions 3/6
60.71% Lines 17/28
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77              1× 1×   1× 1× 1× 1×       1×           1×                               1×                 1×             1× 2×     2×               1× 2× 2×           1×  
/*!
 * Copyright (C) 2017 Glayzzle (BSD3 License)
 * @authors https://github.com/glayzzle/php-transpiler/graphs/contributors
 * @url http://glayzzle.com
 */
'use strict';
 
var md5 = require('crypto').createHash('md5');
var version = require('../package.json').version;
 
var Cache = function(path) {
  this.evals = {};
  this.scripts = {};
  this.stat = {
    resolves: {},
    items: {}
  };
  this.path = path;
};
 
/**
 * Resolves a cache filename
 */
Cache.prototype.resolve = function(key) {
  var fPos = key.lastIndexOf('/');
  if (fPos === -1) {
    fPos = key.lastIndexOf('\\');
  }
  var prefix = version + '-' + md5.update(key).digest('hex');
  if (fPos > 0) {
    return prefix + '-' + key.substring(fPos + 1) + '.js';
  } else {
    return prefix + '.js';
  }
};
 
/**
 * Gets the evaluation callback
 */
Cache.prototype.getEval = function(code) {
  if (code in this.evals) {
    return this.evals[code];
  }
};
 
/**
 * Stores an evaluation callback
 */
Cache.prototype.setEval = function(code, fn) {
  this.evals[code] = fn;
};
 
/**
 * Resolves a script
 */
Cache.prototype.getScript = function(filename) {
  Iif (filename in this.scripts) {
    return this.scripts[filename];
  }
  Iif (this.path) {
    // retrieve the file from cache (if exists)
  }
};
 
/**
 * Stores a script
 */
Cache.prototype.setScript = function(filename, fn) {
  this.scripts[filename] = fn;
  Iif (this.path) {
    // stores the file to cache
  }
};
 
// exports the cache module
module.exports = Cache;