-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (53 loc) · 1.87 KB
/
index.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
var pioc = require('pioc'),
path = require('path'),
toString = Object.prototype.toString;
exports.register = function(server, options, next) {
var baseUrl = options.baseUrl || process.cwd(),
$module = pioc.createModule(options.baseUrl),
$provider = pioc.createProvider($module),
$injector = pioc.createInjector($module, $provider),
services = options.services;
Object.keys(services).forEach(function(serviceName) {
var service = getServiceValue(baseUrl, services[serviceName]);
if(serviceName.match(/^value!/)) {
$module.value(serviceName.substr('value!'.length), service);
} else if(serviceName.match(/^factory!/)) {
$module.factory(serviceName.substr('factory!').length, service);
} else {
$module.bind(serviceName, service);
}
});
$module
.value('module', $module)
.value('provider', $provider)
.value('injector', $injector);
server.method(options.methodName || 'lookup', createLookup($provider, $injector));
next();
};
function getServiceValue(baseUrl, serviceSource) {
if(toString.call(serviceSource) === '[object String]') {
if(serviceSource[0] !== '.') {
return require(serviceSource);
}
return require(path.normalize(path.join(baseUrl, serviceSource)))
}
return serviceSource;
}
function createLookup(provider, injector) {
var lookup = function(serviceName) {
return provider.get(serviceName);
};
lookup.all = function(servicePrefix) {
return provider.getAll(servicePrefix);
};
lookup.has = function(serviceName) {
return provider.has(serviceName);
};
lookup.inject = function(callback) {
return injector.resolve(callback);
};
return lookup;
}
exports.register.attributes = {
pkg: require('./package.json')
};