1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var ModuleLoaderStrategy = require('@themost/common/config').ModuleLoaderStrategy;
|
11 | var _ = require('lodash');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function ServiceConfigurationElement() {
|
19 |
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function ServicesConfiguration() {
|
27 |
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | ServicesConfiguration.config = function(app) {
|
44 | |
45 |
|
46 |
|
47 | var services = app.getConfiguration().getSourceAt('services');
|
48 | if (_.isArray(services)) {
|
49 | _.forEach(services,
|
50 | |
51 |
|
52 |
|
53 | function(x) {
|
54 | if (typeof x.serviceType === 'undefined' || x.serviceType === null) {
|
55 | throw new Error('Invalid configuration. Service type cannot be empty at this context.');
|
56 | }
|
57 | var strategyType = x.strategyType || x.serviceType;
|
58 | var StrategyCtor;
|
59 | var ServiceCtor;
|
60 | var typeModule;
|
61 | var typeCtor;
|
62 | var hashIndex = strategyType.indexOf('#');
|
63 | if (hashIndex>-1) {
|
64 | typeModule = app.getConfiguration().getStrategy(ModuleLoaderStrategy).require(strategyType.substr(0,hashIndex));
|
65 | typeCtor = strategyType.substr(hashIndex+1,strategyType.length-hashIndex);
|
66 | StrategyCtor = typeModule[typeCtor];
|
67 | }
|
68 | else {
|
69 | StrategyCtor = app.getConfiguration().getStrategy(ModuleLoaderStrategy).require(strategyType);
|
70 | }
|
71 | hashIndex = x.serviceType.indexOf('#');
|
72 | if (hashIndex>-1) {
|
73 | typeModule = app.getConfiguration().getStrategy(ModuleLoaderStrategy).require(x.serviceType.substr(0,hashIndex));
|
74 | typeCtor = x.serviceType.substr(hashIndex+1,x.serviceType.length-hashIndex);
|
75 | ServiceCtor = typeModule[typeCtor];
|
76 | }
|
77 | else {
|
78 | ServiceCtor = app.getConfiguration().getStrategy(ModuleLoaderStrategy).require(x.serviceType);
|
79 | }
|
80 | app.useStrategy(StrategyCtor, ServiceCtor);
|
81 | });
|
82 | }
|
83 | };
|
84 |
|
85 |
|
86 | if (typeof exports !== 'undefined')
|
87 | {
|
88 | module.exports.ServiceConfigurationElement = ServiceConfigurationElement;
|
89 | module.exports.ServicesConfiguration = ServicesConfiguration;
|
90 | } |
\ | No newline at end of file |