forked from biggerboat/injector.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
injector-js.js
226 lines (184 loc) · 6.45 KB
/
injector-js.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
var injector = {};;injector.InjectionMapping = function(type, name, id) {
this._type = type;
this._name = name;
this._id = id;
this._value = null;
this._toType = null;
this._isValid = function() {
return this._value!=null || this._toType!=null;
};
this._validateBeforeCreation = function() {
if(this._isValid()) {
throw new Error("Could not create mapping for "+this._id+" because it already has been defined");
return false;
}
return true;
};
};
injector.InjectionMapping.prototype = {
toValue: function(value) {
if(!this._validateBeforeCreation()) {
return;
}
this._value = value;
},
toType: function(type) {
if(!this._validateBeforeCreation()) {
return;
}
this._toType = type;
},
toSingleton: function(type) {
this.toValue(new type());
},
getValue: function() {
if(!this._isValid()) {
throw new Error("Could not get value for "+this._id+" because the mapping is invalid");
return;
}
if(this._value!=null) {
return this._value;
} else if(this._toType!=null) {
return new this._toType();
}
}
};;injector.Injector = function(parentInjector) {
this._mappings = {};
this._parentInjector = null;
this._createMapping = function(type, name, id) {
if(this._hasOwnMapping(type, name)) {
throw new Error("Already has mapping for " + type);
return;
}
var mapping = new injector.InjectionMapping(type, name, id);
this._mappings[id] = mapping;
return mapping;
};
this._getMappingID = function (type, name) {
name = name == undefined ? '' : name;
return type + '|' + name;
};
this._hasOwnMapping = function(type, name) {
var mappingID = this._getMappingID(type, name);
return (this._mappings[mappingID] !== undefined);
};
this._postConstruct = function(object) {
var postConstructs = object.postConstructs!== undefined ? object.postConstructs instanceof Array ? object.postConstructs : [] : [],
index,
methodName,
method;
for(index in postConstructs) {
methodName = postConstructs[index];
method = object[methodName]=== undefined ? null : object[methodName];
if(typeof method === 'function') {
method.apply(object);
}
}
};
this.map('injector').toValue(this);
this._parentInjector = parentInjector || null;
};
injector.Injector.prototype = {
map: function(type, name) {
var mappingID = this._getMappingID(type, name);
return this._mappings[mappingID] || this._createMapping(type, name, mappingID);
},
unmap: function(type, name) {
if(this.hasMapping(type, name)) {
var mappingID = this._getMappingID(type, name);
delete this._mappings[mappingID];
} else {
var nameError = name == undefined ? "" : " by name "+ name;
throw new Error("Cannot unmap \"" + type + nameError + "\" because no mapping has been found");
}
},
hasMapping: function(type, name) {
return this._hasOwnMapping(type, name) || (this._parentInjector !== null && this._parentInjector.hasMapping(type, name));
},
hasDirectMapping: function(type, name) {
return this._hasOwnMapping(type, name);
},
getInstance: function(type, name) {
if(this.hasMapping(type, name)) {
return this.getMapping(type, name).getValue();
} else {
var nameError = name == undefined ? "" : " by name "+ name;
throw new Error("Cannot return instance \"" + type + nameError + "\" because no mapping has been found");
}
},
getMapping: function(type, name) {
if(this.hasMapping(type, name)) {
var mappingID = this._getMappingID(type, name);
if(this._mappings[mappingID] !== undefined) {
return this._mappings[mappingID];
} else {
return this.getParentInjector().getMapping(type, name);
}
} else {
var nameError = name == undefined ? "" : " by name "+ name;
throw new Error("Mapping \"" + type + nameError + "\" was not found");
}
},
injectInto: function(object) {
var member, injectionObject;
for (member in object) {
injectionObject = injector.utils.stringToObject(member, object[member]);
if(injectionObject!=null) {
if(this.hasMapping(injectionObject.type, injectionObject.name)) {
object[member] = this.getInstance(injectionObject.type, injectionObject.name);
} else {
throw new Error("Cannot inject "+injectionObject.type+" into "+object+" due to a missing rule");
}
}
}
this._postConstruct(object);
},
teardown: function() {
this._mappings = {};
this.map('injector').toValue(this);
},
getParentInjector: function() {
return this._parentInjector;
},
setParentInjector: function(parentInjector) {
if(parentInjector != null && !(parentInjector instanceof injector.Injector)) {
throw new Error('Cannot set the parentInjector because it is not an injector');
}
this._parentInjector = parentInjector;
},
createChildInjector: function() {
return new injector.Injector(this);
}
};;injector.utils = {};
/**
* Parse the value of a object to find out how we should inject
* @param type The default type in the mapping. Could be overridden by injectionString
* @param injectionString the value of the variable you want to inject into. Should be a string, else we return null
* String could be formatted like the following examples
*
* - inject - Will in the end inject by type argument
* - inject:someValue - Will override the type argument by the type after the colon
* - inject(name="one"):someValue - Will use named injection with a custom type
* - inject(name="one") - Will inject by name
*/
injector.utils.stringToObject = function(type, injectionString) {
if(typeof injectionString !== "string")
return null;
var injectionObject = { name:'', type:type },
startsWithInjectRegExp = new RegExp('^inject'),
//injectionNameRegExp = new RegExp(/(?<=name=")[^]+?(?=")/), // contains a lookbehind, which is not supported by JS
injectionNameRegExp = new RegExp(/[\w:\-]?name[\s]*?=[\s]*?("[^"]+"|'[^']+'|\w+)/),
toTypeRegExp = new RegExp(':[^:]+$'), //This will return everything from the last colon (including the colon)
name, toType;
if(injectionString=='inject') {
//Simple injection
return injectionObject;
} else if (injectionString.match(startsWithInjectRegExp)) {
name = injectionNameRegExp.exec(injectionString);
toType = toTypeRegExp.exec(injectionString);
injectionObject.name = name != null && name.length==2 ? name[1].replace(/"/gm,"") : '';
injectionObject.type = toType != null && toType.length==1 ? toType[0].replace(':','') : type; //If we did match a type specification, then strip of the colon, else use the input type
return injectionObject;
}
return null;
};