From 544ffec4918fa91f7c26dbc708264b4eabecc2c0 Mon Sep 17 00:00:00 2001 From: welefen Date: Sun, 19 Jul 2015 16:53:19 +0800 Subject: [PATCH] replace think._alias to thinkCache.ALIAS --- src/core/think.js | 28 ++++++----------- src/index.js | 21 +++++++------ test/adapter/cache/base.js | 1 - test/adapter/cache/file.js | 1 - test/adapter/cache/memcache.js | 1 - test/adapter/cache/redis.js | 1 - test/core/think.js | 57 +++++++++++++++++----------------- 7 files changed, 50 insertions(+), 60 deletions(-) diff --git a/src/core/think.js b/src/core/think.js index d4cc64c2..e9d29a61 100644 --- a/src/core/think.js +++ b/src/core/think.js @@ -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}`); }); }); }; diff --git a/src/index.js b/src/index.js index 310904bc..031ddafb 100644 --- a/src/index.js +++ b/src/index.js @@ -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(); diff --git a/test/adapter/cache/base.js b/test/adapter/cache/base.js index 051a7608..d51077d6 100644 --- a/test/adapter/cache/base.js +++ b/test/adapter/cache/base.js @@ -114,7 +114,6 @@ describe('adapter/cache/base.js', function() { }); after(function() { - // think._alias = {}; // think.cli = false; // think.mode = think.mode_mini; // think.module = []; diff --git a/test/adapter/cache/file.js b/test/adapter/cache/file.js index 9599f6a5..5858222c 100644 --- a/test/adapter/cache/file.js +++ b/test/adapter/cache/file.js @@ -142,7 +142,6 @@ describe('adapter/cache/file.js', function() { }); after(function() { - // think._alias = {}; // think.cli = false; // think.mode = think.mode_mini; // think.module = []; diff --git a/test/adapter/cache/memcache.js b/test/adapter/cache/memcache.js index 0829ac8a..f435a1af 100644 --- a/test/adapter/cache/memcache.js +++ b/test/adapter/cache/memcache.js @@ -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; diff --git a/test/adapter/cache/redis.js b/test/adapter/cache/redis.js index 3a263dc9..6672093c 100644 --- a/test/adapter/cache/redis.js +++ b/test/adapter/cache/redis.js @@ -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; diff --git a/test/core/think.js b/test/core/think.js index 470fce6a..99dd5aa9 100644 --- a/test/core/think.js +++ b/test/core/think.js @@ -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', {