laravel-jstools
Version:
JS tools for building front-side of Laravel applications
54 lines (53 loc) • 2.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceContainer = void 0;
const JST_AbstractNotBindToConcreteException_1 = require("../exceptions/JST_AbstractNotBindToConcreteException");
const Service_1 = require("../services/Service");
const Entity_1 = require("../entities/Entity");
class ServiceContainer {
constructor() {
this.classMap = {};
this.aliasMap = {};
this.data = {};
}
setMaps(classMap, aliasMap) {
this.classMap = classMap;
this.aliasMap = aliasMap;
}
createInstance(className, params) {
if (className in this.classMap) {
const classInfo = this.classMap[className];
const instance = classInfo.closure(params);
if (instance instanceof Service_1.Service || instance instanceof Entity_1.Entity) {
const dependsList = instance.getServiceDependsList();
const serviceList = {};
dependsList.forEach((item, i, arr) => {
serviceList[item] = this.createInstance(item, params);
});
instance.setServiceList(serviceList);
}
return instance;
}
throw (0, JST_AbstractNotBindToConcreteException_1.JST_AbstractNotBindToConcreteException)(`Can't create instance of "${className}"! Abstract is not bind!`);
}
getInstance(className, params) {
const classInfo = this.classMap[className];
if (classInfo && classInfo.singleton) {
if (!(className in this.data)) {
this.data[className] = this.createInstance(className, params);
}
return this.data[className];
}
return this.createInstance(className, params);
}
make(name) {
if (name in this.aliasMap) {
return this.getInstance(this.aliasMap[name], {});
}
return this.getInstance(name, {});
}
makeEntity(name, entityType, params) {
return this.getInstance(name, Object.assign(params, { entityType }));
}
}
exports.ServiceContainer = ServiceContainer;