1 var Class = require("../mootools/mootools-node.js").Class;
  2 var fs = require('fs');
  3 
  4 /**
  5  * @class Main resource cache controller
  6  * @requires Class
  7  * @requires fs
  8  *
  9  * @param {Object} configuration
 10  */
 11 var Cache = function(){
 12 
 13     /**
 14      * @property {Object} _configuration
 15      * @private
 16      */
 17     this._configuration = null;
 18     
 19 
 20     /** @ignore */
 21     this.initialize = function(configuration){
 22         this._configuration = configuration;
 23     };
 24 
 25     /**
 26      * cache everything mensioned in configuraion using
 27      * special classes for given resource types
 28      */
 29     this.cache = function(){
 30 
 31         for(var i=0, max=this._configuration.length;i<max;i++){
 32             var configItem = this._configuration[i];
 33 
 34             var resourceLib = require('./resource/' + configItem.className);
 35             var ResourceClass = resourceLib[configItem.className];
 36             var resourceInstance = new ResourceClass(configItem);
 37             
 38             this._cacheRecursive(resourceInstance.getResourcePath(), resourceInstance);
 39         }
 40     };
 41 
 42     /**
 43      * cache recursive used by main Cache() method
 44      *
 45      * @private
 46      * @param {String} templatePath
 47      * @param {IResourceCache} resourceInstance
 48      */
 49     this._cacheRecursive = function(templatePath, resourceInstance){
 50         var templateFiles = fs.readdirSync(templatePath);
 51         var fileContents = '';
 52         var statInfo = null;
 53 
 54         for(var i = 0, l = templateFiles.length; i < l; i++){
 55            if(templateFiles[i]!==resourceInstance.getExcludePattern()){
 56                path = templatePath + templateFiles[i];
 57                statInfo = fs.statSync(path);
 58                if(statInfo.isDirectory()){
 59                    this._cacheRecursive(path+'/', resourceInstance);
 60                } else {
 61                    fileContents = fs.readFileSync(templatePath + templateFiles[i], 'utf8');
 62                    resourceInstance.cache(templatePath+templateFiles[i], fileContents);
 63                }
 64            }
 65         }
 66     };
 67 
 68 
 69 };
 70 
 71 Cache = new Class(new Cache());
 72 exports.Cache = Cache;
 73