-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (52 loc) · 1.65 KB
/
index.js
File metadata and controls
62 lines (52 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
const Bottle = require('bottlejs');
const _ = require('lodash');
module.exports = function () {
const di = new Bottle();
function registerModule(module, serviceName){
if (module.service) {
serviceName = module.service;
}
if (module.implementation) {
serviceName = `${serviceName}-${module.implementation}`;
}
const argList = [serviceName, module].concat(module.deps || [])
if (module.type === 'factory') {
module.serviceName = serviceName;
di.factory.apply(di, argList);
} else {
di.service.apply(di, argList);
}
}
function get(dependencyName) {
try {
return di.container[dependencyName];
} catch (error) {
throw new Error(`Can not get the dependency "${dependencyName}": ${error}`)
}
}
function getImplementation(service, implementation) {
try {
const depenedncy = di.container[`${service}-${implementation}`];
if (!depenedncy) {
throw new Error(`Can not found the implementation "${implementation}" for the service "${service}"`)
}
return depenedncy;
} catch (error) {
throw new Error(`Can not get the implementation "${implementation}" for the service "${service}": ${error}`)
}
}
function extend(source) {
source.container.$list().forEach(function (serviceName) {
if (!_.includes(di.container.$list(), serviceName)) {
di.service.apply(di, [serviceName, function(){ return source.get(serviceName)}]);
}
});
}
di.container.getImplementation = getImplementation;
di.container.get = get;
di.registerModule = registerModule;
di.get = get;
di.extend = extend;
return di;
}