replace think._alias to thinkCache.ALIAS

This commit is contained in:
welefen 2015-07-19 16:53:19 +08:00
parent 06d7029a68
commit 544ffec491
7 changed files with 50 additions and 60 deletions

View File

@ -264,8 +264,9 @@ think.require = (name, flag) => {
return name;
}
// adapter or middle by register
if (think._aliasExport[name]) {
return think._aliasExport[name];
let Cls = thinkCache(thinkCache.ALIAS_EXPORT, name);
if (Cls) {
return Cls;
}
let load = (name, filepath) => {
@ -274,19 +275,20 @@ think.require = (name, flag) => {
obj.prototype.__filename = filepath;
}
if(obj){
think._aliasExport[name] = obj;
thinkCache(thinkCache.ALIAS_EXPORT, name, obj);
}
return obj;
};
if (think._alias[name]) {
return load(name, think._alias[name]);
let filepath = thinkCache(thinkCache.ALIAS, name);
if (filepath) {
return load(name, filepath);
}
// only check in alias
if (flag) {
return null;
}
let filepath = require.resolve(name);
filepath = require.resolve(name);
return load(filepath, filepath);
};
/**
@ -633,7 +635,7 @@ think.adapter = (...args) => {
//think.adapter('session', 'redis', function(){})
if (think.isFunction(fn)) {
key += `${type}_${name}`;
think._aliasExport[key] = fn;
thinkCache(thinkCache.ALIAS_EXPORT, key, fn);
return;
}
//create adapter
@ -705,16 +707,6 @@ think.loadAdapter = force => {
});
};
/**
* module alias
* @type {Object}
*/
think._alias = {};
/**
* module alias export
* @type {Object}
*/
think._aliasExport = {};
/**
* load alias
* @param {String} type []
@ -734,7 +726,7 @@ think.alias = (type, paths, slash) => {
}
let name = file.slice(0, -3);
name = type + (slash ? '/' : '_') + name;
think._alias[name] = `${path}/${file}`;
thinkCache(thinkCache.ALIAS, name, `${path}/${file}`);
});
});
};

View File

@ -110,18 +110,19 @@ export default class {
*/
loadAlias(){
let aliasPath = `${think.THINK_LIB_PATH}/config/alias.js`;
think._alias = require(aliasPath);
thinkCache(thinkCache.ALIAS, require(aliasPath));
}
/**
* load alias module export
* @return {} []
*/
loadAliasExport(){
for(let key in think._alias){
if (key in think._aliasExport) {
var alias = thinkCache(thinkCache.ALIAS);
for(let key in alias){
if (thinkCache(thinkCache.ALIAS_EXPORT, key)) {
continue;
}
think._aliasExport[key] = think.require(key);
thinkCache(thinkCache.ALIAS_EXPORT, key, think.require(key));
}
}
/**
@ -235,17 +236,18 @@ export default class {
name = `${think.config('default_module')}/${think.dirname.controller}/${call[length - 2]}`;
action = call[length - 1];
}
if (!(name in think._alias)) {
let filepath = thinkCache(thinkCache.ALIAS, name);
if (!filepath) {
return;
}
let cls = think.require(name);
let method = cls.prototype[action + think.config('action_suffix')];
let callMethod = cls.prototype.__call;
if (think.isFunction(method)) {
think._alias.call_controller = think._alias[name];
thinkCache(thinkCache.ALIAS, 'call_controller', filepath);
think.config('call_action', action);
}else if (think.isFunction(callMethod)) {
think._alias.call_controller = think._alias[name];
thinkCache(thinkCache.ALIAS, 'call_controller', filepath);
think.config('call_action', '__call');
}
}
@ -301,8 +303,9 @@ export default class {
* @return {} []
*/
load(){
think._alias = {};
think._aliasExport = {};
thinkCache(thinkCache.ALIAS, null);
thinkCache(thinkCache.ALIAS_EXPORT, null);
this.loadConfig();
this.loadBootstrap();
this.loadRoute();

View File

@ -114,7 +114,6 @@ describe('adapter/cache/base.js', function() {
});
after(function() {
// think._alias = {};
// think.cli = false;
// think.mode = think.mode_mini;
// think.module = [];

View File

@ -142,7 +142,6 @@ describe('adapter/cache/file.js', function() {
});
after(function() {
// think._alias = {};
// think.cli = false;
// think.mode = think.mode_mini;
// think.module = [];

View File

@ -126,7 +126,6 @@ describe('adapter/cache/memcache.js', function() {
});
after(function() {
// think._alias = {};
think.APP_PATH = think.RESOURCE_PATH = testAppPath;
think.rmdir(testAppPath);
// think.cli = false;

View File

@ -126,7 +126,6 @@ describe('adapter/cache/redis.js', function() {
});
after(function() {
// think._alias = {};
think.APP_PATH = think.RESOURCE_PATH = testAppPath;
think.rmdir(testAppPath);
// think.cli = false;

View File

@ -42,7 +42,6 @@ describe('core/think.js', function(){
think.cli = false;
think.mode = think.mode_mini;
think.module = [];
//think._alias = {};
})
it('methods from thinkit', function(){
@ -243,12 +242,12 @@ describe('core/think.js', function(){
}
})
it('think.lookClass("module/is/exist") is function', function(){
think._aliasExport['module/is/exist'] = function(){
thinkCache(thinkCache.ALIAS_EXPORT, 'module/is/exist', function(){
return 'module/is/exist';
}
})
var fn = think.lookClass('module/is/exist');
assert.equal(fn(), 'module/is/exist');
think._aliasExport = {};
thinkCache(thinkCache.ALIAS_EXPORT, 'module/is/exist', null);
})
it('think.lookClass("home/group", "controller") not found', function(){
try{
@ -258,35 +257,35 @@ describe('core/think.js', function(){
}
})
it('think.lookClass("home/group", "service") is function', function(){
think._aliasExport['home/service/group'] = function(){
thinkCache(thinkCache.ALIAS_EXPORT, 'home/service/group', function(){
return 'home/service/group';
}
})
var fn = think.lookClass("home/group", "service");
assert.equal(fn(), 'home/service/group');
think._aliasExport = {};
thinkCache(thinkCache.ALIAS_EXPORT, 'home/service/group', null);
})
it('think.lookClass("detail", "controller", "homwwwe") not found', function(){
var cls = think.lookClass('detail', 'controller', 'homwwwe', 'homwwww');
assert.equal(cls, null);
})
it('think.lookClass("group", "controller", "home") is function', function(){
think._aliasExport['home/controller/group'] = function(){
thinkCache(thinkCache.ALIAS_EXPORT, 'home/controller/group', function(){
return 'home/controller/group';
}
})
var fn = think.lookClass('group', 'controller', 'home');
assert.equal(fn(), 'home/controller/group');
delete think._aliasExport['home/controller/group'];
thinkCache(thinkCache.ALIAS_EXPORT, 'home/controller/group', null);
})
it('think.lookClass("group", "controller", "home1") is function', function(){
var mode = think.mode;
think.mode = think.mode_module;
think._aliasExport['common/controller/group'] = function(){
thinkCache(thinkCache.ALIAS_EXPORT, 'common/controller/group', function(){
return 'common/controller/group';
}
})
var fn = think.lookClass('group', 'controller', 'home1');
assert.equal(fn(), 'common/controller/group');
think.mode = mode;
delete think._aliasExport['common/controller/group'];
thinkCache(thinkCache.ALIAS_EXPORT, 'common/controller/group', null);
})
})
@ -411,33 +410,33 @@ describe('core/think.js', function(){
var data = think.require({});
assert.deepEqual(data, {})
})
it('think.require is in _aliasExport', function(){
var data = think._aliasExport;
it('think.require is in aliasExport', function(){
var data = thinkCache(thinkCache.ALIAS_EXPORT);
var fn = function(){};
think._aliasExport = {
thinkCache(thinkCache.ALIAS_EXPORT, {
'_test_': fn
}
})
var result = think.require('_test_')
assert.equal(result, fn);
think._aliasExport = data;
thinkCache(thinkCache.ALIAS_EXPORT, data);
})
it('think.require is in _alias', function(){
var data = think._alias;
think._alias = {
it('think.require is in alias', function(){
var data = thinkCache(thinkCache.ALIAS);
thinkCache(thinkCache.ALIAS, {
'_test_': __filename + '/a.js'
}
})
var result = think.require('_test_');
assert.equal(result, null);
think._alias = data;
thinkCache(thinkCache.ALIAS, data);
})
it('think.require is in _alias', function(){
var data = think._alias;
think._alias = {
var data = thinkCache(thinkCache.ALIAS);
thinkCache(thinkCache.ALIAS, {
'_test_': path.normalize(__dirname + '/../../lib/index.js')
}
})
var result = think.require('_test_');
assert.equal(think.isFunction(result), true)
think._alias = data;
thinkCache(thinkCache.ALIAS, data);
})
it('think.require is not in _alias, try it', function(){
@ -1025,9 +1024,9 @@ describe('core/think.js', function(){
var fn = function(){}
var key = 'adapter_welefen_suredy';
think.adapter('welefen', 'suredy', fn);
var fn1 = think._aliasExport[key];
var fn1 = thinkCache(thinkCache.ALIAS_EXPORT, key);
assert.equal(fn, fn1);
delete think._aliasExport[key];
thinkCache(thinkCache.ALIAS_EXPORT, key, null);
})
it('create adapter', function(){
var fn = think.adapter('session', 'base', {