2019-09-07 04:23:15 +00:00
|
|
|
import { interfaces, Container } from "inversify";
|
|
|
|
|
|
|
|
let _container: Container;
|
|
|
|
|
2020-05-02 16:14:47 +00:00
|
|
|
const ContainerInstance = Symbol.for("@ccms/ioc:Container");
|
2019-09-07 04:23:15 +00:00
|
|
|
const INJECTION = Symbol.for("INJECTION");
|
|
|
|
|
|
|
|
function _proxyGetter(
|
|
|
|
proto: any,
|
|
|
|
key: string,
|
|
|
|
resolve: () => any,
|
|
|
|
doCache: boolean
|
|
|
|
) {
|
2019-09-27 10:39:40 +00:00
|
|
|
function getter(this: object) {
|
2019-09-07 04:23:15 +00:00
|
|
|
if (doCache && !Reflect.hasMetadata(INJECTION, this, key)) {
|
|
|
|
Reflect.defineMetadata(INJECTION, resolve(), this, key);
|
|
|
|
}
|
|
|
|
if (Reflect.hasMetadata(INJECTION, this, key)) {
|
|
|
|
return Reflect.getMetadata(INJECTION, this, key);
|
|
|
|
} else {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:39:40 +00:00
|
|
|
function setter(this: object, newVal: any) {
|
2019-09-07 04:23:15 +00:00
|
|
|
Reflect.defineMetadata(INJECTION, newVal, this, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperty(proto, key, {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: getter,
|
|
|
|
set: setter
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initContainer(container: Container) {
|
2020-01-17 03:10:30 +00:00
|
|
|
Reflect.defineMetadata(ContainerInstance, container, Reflect);
|
2019-09-07 04:23:15 +00:00
|
|
|
_container = container;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getContainer(): Container {
|
2020-01-17 03:10:30 +00:00
|
|
|
return _container || Reflect.getMetadata(ContainerInstance, Reflect)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makePropertyInjectDecorator(doCache: boolean) {
|
|
|
|
return function(serviceIdentifier: interfaces.ServiceIdentifier<any>) {
|
|
|
|
return function(proto: any, key: string): void {
|
|
|
|
let resolve = () => {
|
|
|
|
return getContainer().get(serviceIdentifier);
|
|
|
|
};
|
|
|
|
_proxyGetter(proto, key, resolve, doCache);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function makePropertyInjectNamedDecorator(doCache: boolean) {
|
|
|
|
return function(serviceIdentifier: interfaces.ServiceIdentifier<any>, named: string) {
|
|
|
|
return function(proto: any, key: string): void {
|
|
|
|
let resolve = () => {
|
|
|
|
return getContainer().getNamed(serviceIdentifier, named);
|
|
|
|
};
|
|
|
|
_proxyGetter(proto, key, resolve, doCache);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function makePropertyInjectTaggedDecorator(doCache: boolean) {
|
|
|
|
return function(serviceIdentifier: interfaces.ServiceIdentifier<any>, key: string, value: any) {
|
|
|
|
return function(proto: any, propertyName: string): void {
|
|
|
|
let resolve = () => {
|
|
|
|
return getContainer().getTagged(serviceIdentifier, key, value);
|
|
|
|
};
|
|
|
|
_proxyGetter(proto, propertyName, resolve, doCache);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function makePropertyMultiInjectDecorator(doCache: boolean) {
|
|
|
|
return function(serviceIdentifier: interfaces.ServiceIdentifier<any>) {
|
|
|
|
return function(proto: any, key: string): void {
|
|
|
|
let resolve = () => {
|
|
|
|
return getContainer().getAll(serviceIdentifier);
|
|
|
|
};
|
|
|
|
_proxyGetter(proto, key, resolve, doCache);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let doCache = true;
|
|
|
|
|
|
|
|
let lazyInject = makePropertyInjectDecorator(doCache)
|
|
|
|
let lazyInjectNamed = makePropertyInjectNamedDecorator(doCache)
|
|
|
|
let lazyInjectTagged = makePropertyInjectTaggedDecorator(doCache)
|
|
|
|
let lazyMultiInject = makePropertyMultiInjectDecorator(doCache)
|
|
|
|
|
|
|
|
export {
|
|
|
|
initContainer,
|
|
|
|
getContainer,
|
2020-01-17 03:10:30 +00:00
|
|
|
ContainerInstance,
|
2019-09-07 04:23:15 +00:00
|
|
|
lazyInject,
|
|
|
|
lazyInjectNamed,
|
|
|
|
lazyInjectTagged,
|
|
|
|
lazyMultiInject
|
|
|
|
};
|