mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-02-01 15:23:03 +00:00
add think_adapter file, separate from think.js
This commit is contained in:
parent
8c537e60b6
commit
7e178563aa
@ -21,6 +21,7 @@ import Middleware from './think_middleware.js';
|
||||
import Hook from './think_hook.js';
|
||||
import Route from './think_route.js';
|
||||
import Config from './think_config.js';
|
||||
import Adatper from './think_adapter.js';
|
||||
|
||||
import './think_cache.js';
|
||||
import './think_data.js';
|
||||
@ -181,6 +182,11 @@ think.config = Config;
|
||||
think.getModuleConfig = module => {
|
||||
return think.config(undefined, undefined, module);
|
||||
};
|
||||
/**
|
||||
* adapter
|
||||
* @type {Function}
|
||||
*/
|
||||
think.adapter = Adatper;
|
||||
|
||||
/**
|
||||
* alias co module to think.co
|
||||
@ -475,109 +481,6 @@ think.log = (msg, type, showTime) => {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* create, register, call adapter
|
||||
* @param {String} name []
|
||||
* @return {void} []
|
||||
*/
|
||||
think.adapter = (...args) => {
|
||||
let [type, name, fn] = args;
|
||||
let length = args.length, key = 'adapter_';
|
||||
if(length === 3){
|
||||
//register adapter
|
||||
//think.adapter('session', 'redis', function(){})
|
||||
if (think.isFunction(fn)) {
|
||||
key += `${type}_${name}`;
|
||||
thinkData.export[key] = fn;
|
||||
return;
|
||||
}
|
||||
//create adapter
|
||||
//module.exports = think.adapter('session', 'memory', {})
|
||||
else if(think.isObject(fn)){
|
||||
return think.Class(think.adapter(type, name), fn);
|
||||
}
|
||||
}
|
||||
//type has not _
|
||||
else if(length === 2 && think.isString(type) && type.indexOf('_') === -1){
|
||||
//create adapter
|
||||
//module.exports = think.adapter('session', {})
|
||||
if(think.isObject(name)){
|
||||
return think.Class(think.adapter(type, 'base'), name);
|
||||
}
|
||||
//get adapter
|
||||
//think.adapter('session', 'redis')
|
||||
else if (think.isString(name)) {
|
||||
let nameLower = name.toLowerCase();
|
||||
if(name !== nameLower){
|
||||
name = nameLower;
|
||||
think.log(colors => {
|
||||
return colors.yellow(`[WARNING]`) + ` adapter type \`${name}\` has uppercase chars.`;
|
||||
});
|
||||
}
|
||||
|
||||
key += type + '_' + name;
|
||||
let cls = think.require(key, true);
|
||||
if (cls) {
|
||||
return cls;
|
||||
}else{
|
||||
think.loadAdapter(type, name);
|
||||
let cls = think.require(key, true);
|
||||
if(cls){
|
||||
return cls;
|
||||
}
|
||||
}
|
||||
throw new Error(think.locale('ADAPTER_NOT_FOUND', key));
|
||||
}
|
||||
}
|
||||
|
||||
//create adapter
|
||||
//module.exports = think.adapter({})
|
||||
//module.exports = think.adapter(function(){}, {});
|
||||
let superClass;
|
||||
if (think.isFunction(type)) {
|
||||
superClass = type;
|
||||
}else if (think.isString(type)) {
|
||||
superClass = think.require(type);
|
||||
}
|
||||
//create clean Class
|
||||
if (!superClass) {
|
||||
return think.Class(type);
|
||||
}
|
||||
return think.Class(superClass, name);
|
||||
};
|
||||
/**
|
||||
* load system & comon module adapter
|
||||
* @return {} []
|
||||
*/
|
||||
think.loadAdapter = (type, name = 'base') => {
|
||||
let paths = [`${think.THINK_LIB_PATH}${think.sep}adapter`];
|
||||
|
||||
//load base adapter
|
||||
think.adapter.base = think.safeRequire(paths[0] + '/base.js');
|
||||
|
||||
//common module adapter
|
||||
let adapterPath = think.getPath(undefined, think.dirname.adapter);
|
||||
if (think.isDir(adapterPath)) {
|
||||
paths.push(adapterPath);
|
||||
}
|
||||
paths.forEach(path => {
|
||||
if(type){
|
||||
let filepath = `${path}${think.sep}${type}${think.sep}${name}.js`;
|
||||
if(think.isFile(filepath)){
|
||||
thinkData.alias[`adapter_${type}_${name}`] = filepath;
|
||||
}
|
||||
}else{
|
||||
let dirs = fs.readdirSync(path);
|
||||
dirs.forEach(dir => {
|
||||
if(!think.isDir(`${path}/${dir}`)){
|
||||
return;
|
||||
}
|
||||
think.alias(`adapter_${dir}`, `${path}${think.sep}${dir}`);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* load alias
|
||||
* @param {String} type []
|
||||
|
||||
122
src/core/think_adapter.js
Normal file
122
src/core/think_adapter.js
Normal file
@ -0,0 +1,122 @@
|
||||
'use strict';
|
||||
|
||||
import fs from 'fs';
|
||||
|
||||
/**
|
||||
* create, register, call adapter
|
||||
* @param {String} name []
|
||||
* @return {void} []
|
||||
*/
|
||||
let Adapter = (...args) => {
|
||||
let [type, name, fn] = args;
|
||||
let length = args.length, key = 'adapter_';
|
||||
if(length === 3){
|
||||
//register adapter
|
||||
//think.adapter('session', 'redis', function(){})
|
||||
if (think.isFunction(fn)) {
|
||||
key += `${type}_${name}`;
|
||||
thinkData.export[key] = fn;
|
||||
return;
|
||||
}
|
||||
//create adapter
|
||||
//module.exports = think.adapter('session', 'memory', {})
|
||||
else if(think.isObject(fn)){
|
||||
return think.Class(think.adapter(type, name), fn);
|
||||
}
|
||||
}
|
||||
//type has not _
|
||||
else if(length === 2 && think.isString(type) && type.indexOf('_') === -1){
|
||||
//create adapter
|
||||
//module.exports = think.adapter('session', {})
|
||||
if(think.isObject(name)){
|
||||
return think.Class(think.adapter(type, 'base'), name);
|
||||
}
|
||||
//get adapter
|
||||
//think.adapter('session', 'redis')
|
||||
else if (think.isString(name)) {
|
||||
return Adapter.get(type, name);
|
||||
}
|
||||
}
|
||||
|
||||
return Adapter.create(type, name);
|
||||
};
|
||||
|
||||
//get adapter
|
||||
//think.adapter('session', 'redis')
|
||||
Adapter.get = (type, name) => {
|
||||
let key = 'adapter_';
|
||||
let nameLower = name.toLowerCase();
|
||||
if(name !== nameLower){
|
||||
name = nameLower;
|
||||
think.log(colors => {
|
||||
return colors.yellow(`[WARNING]`) + ` adapter type \`${name}\` has uppercase chars.`;
|
||||
});
|
||||
}
|
||||
|
||||
key += type + '_' + name;
|
||||
let cls = think.require(key, true);
|
||||
if (cls) {
|
||||
return cls;
|
||||
}else{
|
||||
Adapter.load(type, name);
|
||||
let cls = think.require(key, true);
|
||||
if(cls){
|
||||
return cls;
|
||||
}
|
||||
}
|
||||
throw new Error(think.locale('ADAPTER_NOT_FOUND', key));
|
||||
};
|
||||
|
||||
//create adapter
|
||||
//module.exports = think.adapter({})
|
||||
//module.exports = think.adapter(function(){}, {});
|
||||
Adapter.create = (type, name) => {
|
||||
let superClass;
|
||||
if (think.isFunction(type)) {
|
||||
superClass = type;
|
||||
}else if (think.isString(type)) {
|
||||
superClass = think.require(type);
|
||||
}
|
||||
//create clean Class
|
||||
if (!superClass) {
|
||||
return think.Class(type);
|
||||
}
|
||||
return think.Class(superClass, name);
|
||||
};
|
||||
|
||||
/**
|
||||
* load system & comon module adapter
|
||||
* @return {} []
|
||||
*/
|
||||
Adapter.load = (type, name = 'base') => {
|
||||
let paths = [`${think.THINK_LIB_PATH}${think.sep}adapter`];
|
||||
|
||||
//load base adapter
|
||||
if(!think.adapter.base){
|
||||
think.adapter.base = think.safeRequire(paths[0] + '/base.js');
|
||||
}
|
||||
|
||||
//common module adapter
|
||||
let adapterPath = think.getPath(undefined, think.dirname.adapter);
|
||||
if (think.isDir(adapterPath)) {
|
||||
paths.push(adapterPath);
|
||||
}
|
||||
paths.forEach(path => {
|
||||
if(type){
|
||||
let filepath = `${path}${think.sep}${type}${think.sep}${name}.js`;
|
||||
if(think.isFile(filepath)){
|
||||
thinkData.alias[`adapter_${type}_${name}`] = filepath;
|
||||
}
|
||||
}else{
|
||||
let dirs = fs.readdirSync(path);
|
||||
dirs.forEach(dir => {
|
||||
if(!think.isDir(`${path}/${dir}`)){
|
||||
return;
|
||||
}
|
||||
think.alias(`adapter_${dir}`, `${path}${think.sep}${dir}`);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export default Adapter;
|
||||
@ -264,7 +264,7 @@ export default class {
|
||||
* @return {} []
|
||||
*/
|
||||
loadAdapter(){
|
||||
think.loadAdapter();
|
||||
think.adapter.load();
|
||||
}
|
||||
/**
|
||||
* load middleware
|
||||
|
||||
@ -1185,26 +1185,26 @@ describe('core/think.js', function(){
|
||||
assert.equal(think.isFunction(fn.prototype.get), true);
|
||||
})
|
||||
|
||||
it('think.loadAdapter base', function(done){
|
||||
it('think.adapter.load base', function(done){
|
||||
var mode = think.mode;
|
||||
var path = think.getPath(undefined, think.dirname.adapter);;
|
||||
think.mkdir(path);
|
||||
think.loadAdapter(true);
|
||||
think.adapter.load(true);
|
||||
think.rmdir(path).then(done);
|
||||
})
|
||||
it('think.loadAdapter load, store/base adapter', function(done){
|
||||
it('think.adapter.load load, store/base adapter', function(done){
|
||||
var mode = think.mode;
|
||||
var path = think.getPath(undefined, think.dirname.adapter);;
|
||||
think.mkdir(path);
|
||||
think.loadAdapter('store', 'memory');
|
||||
think.adapter.load('store', 'memory');
|
||||
think.rmdir(path).then(done);
|
||||
})
|
||||
it('think.loadAdapter extra adapter', function(done){
|
||||
it('think.adapter.load extra adapter', function(done){
|
||||
var mode = think.mode;
|
||||
var path = think.getPath(undefined, think.dirname.adapter);;
|
||||
think.mkdir(path + '/welefentest');
|
||||
require('fs').writeFileSync(path + '/welefentest/base.js', 'module.exports=think.Class({}, true)')
|
||||
think.loadAdapter();
|
||||
think.adapter.load();
|
||||
assert.equal(think.isFunction(think.adapter.welefentest), false);
|
||||
delete think.adapter.welefentest;
|
||||
think.rmdir(path).then(done);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user