Skip to content

Commit

Permalink
refactor: remove egg-core from midway-core
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Mar 5, 2019
1 parent fef300b commit 834eb81
Show file tree
Hide file tree
Showing 52 changed files with 548 additions and 762 deletions.
1 change: 0 additions & 1 deletion packages/midway-core/.autod.conf.js
Expand Up @@ -11,7 +11,6 @@ module.exports = {
'run',
],
dep: [
"inflection"
],
devdep: [
],
Expand Down
9 changes: 3 additions & 6 deletions packages/midway-core/package.json
Expand Up @@ -27,15 +27,12 @@
"midway-bin": "^1.4.3"
},
"dependencies": {
"@midwayjs/decorator": "^1.4.3",
"camelcase": "^5.0.0",
"debug": "^4.1.1",
"egg-core": "^4.14.1",
"extend2": "^1.0.0",
"globby": "^9.0.0",
"inflection": "^1.12.0",
"injection": "^1.3.2",
"is-type-of": "^1.2.1",
"reflect-metadata": "^0.1.13"
"injection": "^1.4.1",
"is-type-of": "^1.2.1"
},
"author": "Harry Chen <czy88840616@gmail.com>",
"repository": {
Expand Down
Expand Up @@ -3,3 +3,5 @@ export const MidwayHandlerKey = {
PLUGIN: 'plugin',
LOGGER: 'logger',
};

export const FUNCTION_INJECT_KEY = 'midway:function_inject_key';
69 changes: 26 additions & 43 deletions packages/midway-core/src/container.ts
@@ -1,7 +1,10 @@
import 'reflect-metadata';
import { CLASS_KEY_CONSTRUCTOR, CONFIG_KEY, LOGGER_KEY, PLUGIN_KEY } from '@midwayjs/decorator';
import {
Autowire,
Container,
getClassMetadata,
getObjectDefinition,
getProviderId,
IApplicationContext,
IContainer,
IManagedInstance,
Expand All @@ -10,26 +13,13 @@ import {
IObjectDefinition,
IObjectDefinitionParser,
IParserContext,
OBJ_DEF_CLS,
ObjectDefinitionOptions,
ObjectIdentifier,
Scope,
ScopeEnum,
TagClsMetadata,
TAGGED_CLS,
XmlObjectDefinition
} from 'injection';
import {
CLASS_KEY_CONSTRUCTOR,
CONFIG_KEY_CLZ,
CONFIG_KEY_PROP,
FUNCTION_INJECT_KEY,
LOGGER_KEY_CLZ,
LOGGER_KEY_PROP,
PLUGIN_KEY_CLZ,
PLUGIN_KEY_PROP
} from './decorators';
import { MidwayHandlerKey } from './constants';
import { FUNCTION_INJECT_KEY, MidwayHandlerKey } from './constant';

const globby = require('globby');
const path = require('path');
Expand All @@ -41,6 +31,11 @@ const MIDDLEWARES = 'middlewares';
const TYPE_LOGGER = 'logger';
const TYPE_PLUGIN = 'plugin';

interface FrameworkDecoratorMetadata {
key: string;
propertyName: string;
}

class BaseParser {
container: MidwayContainer;

Expand Down Expand Up @@ -186,6 +181,7 @@ export class MidwayContainer extends Container implements IContainer {
// ctx is in requestContainer
this.registerObject('ctx', this.ctx);
}

/**
* update current context in applicationContext
* for mock and other case
Expand Down Expand Up @@ -240,9 +236,9 @@ export class MidwayContainer extends Container implements IContainer {

protected bindClass(module) {
if (is.class(module)) {
const metaData = Reflect.getMetadata(TAGGED_CLS, module) as TagClsMetadata;
if (metaData) {
this.bind(metaData.id, module);
const providerId = getProviderId(module);
if (providerId) {
this.bind(providerId, module);
} else {
// inject by name in js
this.bind(camelcase(module.name), module);
Expand Down Expand Up @@ -277,7 +273,7 @@ export class MidwayContainer extends Container implements IContainer {
this.beforeEachCreated((target, constructorArgs, context) => {
let constructorMetaData;
try {
constructorMetaData = Reflect.getOwnMetadata(CLASS_KEY_CONSTRUCTOR, target);
constructorMetaData = getClassMetadata(CLASS_KEY_CONSTRUCTOR, target);
} catch (e) {
debug(`beforeEachCreated error ${e.stack}`);
}
Expand Down Expand Up @@ -308,14 +304,14 @@ export class MidwayContainer extends Container implements IContainer {
this.afterEachCreated((instance, context, definition) => {

// 处理配置装饰器
const configSetterProps = this.getClzSetterProps(CONFIG_KEY_CLZ, instance);
this.defineGetterPropertyValue(configSetterProps, CONFIG_KEY_PROP, instance, this.handlerMap.get(MidwayHandlerKey.CONFIG));
const configSetterProps: FrameworkDecoratorMetadata[] = getClassMetadata(CONFIG_KEY, instance);
this.defineGetterPropertyValue(configSetterProps, instance, this.handlerMap.get(MidwayHandlerKey.CONFIG));
// 处理插件装饰器
const pluginSetterProps = this.getClzSetterProps(PLUGIN_KEY_CLZ, instance);
this.defineGetterPropertyValue(pluginSetterProps, PLUGIN_KEY_PROP, instance, this.handlerMap.get(MidwayHandlerKey.PLUGIN));
const pluginSetterProps: FrameworkDecoratorMetadata[] = getClassMetadata(PLUGIN_KEY, instance);
this.defineGetterPropertyValue(pluginSetterProps, instance, this.handlerMap.get(MidwayHandlerKey.PLUGIN));
// 处理日志装饰器
const loggerSetterProps = this.getClzSetterProps(LOGGER_KEY_CLZ, instance);
this.defineGetterPropertyValue(loggerSetterProps, LOGGER_KEY_PROP, instance, this.handlerMap.get(MidwayHandlerKey.LOGGER));
const loggerSetterProps: FrameworkDecoratorMetadata[] = getClassMetadata(LOGGER_KEY, instance);
this.defineGetterPropertyValue(loggerSetterProps, instance, this.handlerMap.get(MidwayHandlerKey.LOGGER));

// 表示非ts annotation模式
if (!pluginSetterProps && !loggerSetterProps && definition.isAutowire()) {
Expand All @@ -338,32 +334,19 @@ export class MidwayContainer extends Container implements IContainer {
});
}

/**
* get method name for decorator
*
* @param setterClzKey
* @param target
* @returns {Array<string>}
*/
private getClzSetterProps(setterClzKey, target): string[] {
return Reflect.getMetadata(setterClzKey, target);
}

/**
* binding getter method for decorator
*
* @param setterProps
* @param metadataKey
* @param instance
* @param getterHandler
*/
private defineGetterPropertyValue(setterProps, metadataKey, instance, getterHandler) {
private defineGetterPropertyValue(setterProps: FrameworkDecoratorMetadata[], instance, getterHandler) {
if (setterProps && getterHandler) {
for (const prop of setterProps) {
const propertyKey = Reflect.getMetadata(metadataKey, instance, prop);
if (propertyKey) {
Object.defineProperty(instance, prop, {
get: () => getterHandler(propertyKey),
if (prop.propertyName) {
Object.defineProperty(instance, prop.propertyName, {
get: () => getterHandler(prop.key),
configurable: false,
enumerable: true
});
Expand All @@ -380,7 +363,7 @@ export class MidwayContainer extends Container implements IContainer {
super.registerCustomBinding(objectDefinition, target);

// Override the default scope to request
const objDefOptions: ObjectDefinitionOptions = Reflect.getMetadata(OBJ_DEF_CLS, target);
const objDefOptions: ObjectDefinitionOptions = getObjectDefinition(target);
if (objDefOptions && !objDefOptions.scope) {
debug(`register @scope to default value(request), id=${objectDefinition.id}`);
objectDefinition.scope = ScopeEnum.Request;
Expand Down
52 changes: 0 additions & 52 deletions packages/midway-core/src/decorators/application.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/midway-core/src/decorators/index.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/midway-core/src/decorators/metaKeys.ts

This file was deleted.

7 changes: 3 additions & 4 deletions packages/midway-core/src/index.ts
@@ -1,6 +1,5 @@
export { MidwayLoader } from './loader';
export { ContainerLoader } from './loader';
export { MidwayContainer } from './container';
export * from './decorators';
export * from './interface';
export * from './constants';
export { MidwayRequestContainer } from './requestContainer';
export * from './providerWrapper';
export * from './constant';

0 comments on commit 834eb81

Please sign in to comment.