Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove egg-core from midway-core #159

Merged
merged 8 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/midway-core/.autod.conf.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@czy88840616 czy88840616 Mar 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他这个是可以自己增加特定目录的加载器,貌似我们暂时用不到了。

"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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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';
Loading