This repository has been archived by the owner on Jul 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_formatInjections.js
164 lines (130 loc) · 4.21 KB
/
_formatInjections.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
'use strict';
var fs = require('fs-extra');
var finder = require('fs-finder');
var doFileExists = require('./utils/doFileExists.js');
var rmDuplicated = require('./utils/rmDuplicated.js');
var setIdentation = require('./utils/setIdentation.js');
var log = require('./utils/log.js');
var flags = require ('./utils/flags.js');
var prodExceptions = require('./exceptions/prod.js');
var devExceptions = require('./exceptions/dev.js');
/**
* Method that will return array of Bundles declarations paths for each
* dependencies
*/
var bundlesFilter = function(dependencies) {
var bundles = [];
for (var key in dependencies) {
var dependency = dependencies[key];
var dependencyPath = './vendor/' + dependency;
if (doFileExists(dependencyPath)) {
var dependencyBundles = finder.from(dependencyPath).findFiles('*Bundle.php');
bundles = bundles.concat(dependencyBundles);
}
}
return bundles;
};
var isBundle = function(file) {
return file.indexOf('use Symfony\\Component\\HttpKernel\\Bundle\\Bundle') > -1;
};
/**
* Method that will return array of string to include into appKernel.php from
* an array of bundles.
*/
var appKernelInjections = function(bundles) {
var injections = {
prod: [],
dev: [],
};
for (var key in bundles) {
var bundle = bundles[key];
var file = fs.readFileSync(bundle, 'utf8');
if (isBundle(file)) {
var injection = getClassInjection(file);
var isDev = isBundleDevException(injection);
if (isDev) {
injection = getClassInjection(file, isDev);
injections.dev = injections.dev.concat(injection);
} else {
injections.prod = injections.prod.concat(injection);
}
}
}
injections.prod = rmDuplicated(injections.prod).filter(appKernelFilter).sort();
injections.dev = rmDuplicated(injections.dev).filter(appKernelFilter).sort();
return injections;
};
/**
* Format the injection
*/
var getClassInjection = function(file, dev) {
if (typeof(dev) === 'undefined') {
dev = false;
}
var namespace = file.match(/namespace\s(.*)\;/)[1];
var bundleClass = file.match(/class\s(.*)\sextends/)[1];
var prefix = 'new ';
if (dev) {
prefix = '$bundles[] = new ';
}
var suffix = '(),';
if (dev) {
suffix = '();';
}
if (namespace === 'JMS\\DiExtraBundle' && bundleClass === 'JMSDiExtraBundle') {
suffix = '($this)';
if (dev) {
suffix = suffix + ';';
} else {
suffix = suffix + ',';
}
}
return prefix + namespace + '\\' + bundleClass + suffix;
};
/**
* Method that will return true if an injection must be insert into the dev env
* condition into the appKernel
*/
var isBundleDevException = function(injection) {
if (devExceptions.indexOf(injection) >= 0) {
return true;
}
return false;
};
/**
* Fuction designed for the js filter() method in order to sort bundles we don't
* want to inject via the module
*/
var appKernelFilter = function(injection) {
var regexFilter = /(Test)|(^new\sSymfony)|(^new\sAcme\\DemoBundle)/;
if (prodExceptions.indexOf(injection) >= 0 || injection.match(regexFilter)) {
return false;
}
return true;
};
/**
* Method that will format into a single string all the bundles injections;
*/
var stringifyBundleInjection = function(bundlesInjections) {
var injections = {
prod: '',
dev: '',
};
var indentation = setIdentation(3);
for (var type in injections) {
for (var key in bundlesInjections[type]) {
var injection = bundlesInjections[type][key];
injections[type] += indentation + injection + '\n';
}
injections[type] += flags[type].end;
}
return injections;
};
var formatInjections = function(requirements) {
log('...Sorting between bundles and non-bundle dependencies...');
var bundles = bundlesFilter(requirements);
log('...Formating the injection...');
var bundlesInjections = appKernelInjections(bundles);
return stringifyBundleInjection(bundlesInjections);
};
module.exports = formatInjections;