-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappjs-list.php
More file actions
executable file
·359 lines (286 loc) · 9.52 KB
/
appjs-list.php
File metadata and controls
executable file
·359 lines (286 loc) · 9.52 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/php
<?PHP
error_reporting((E_ALL | E_STRICT));
error_reporting(error_reporting() ^ E_NOTICE);
require_once("global.inc.php");
require_once("config.localonly.inc.php");
$params = getParams();
include('ext4aliases.inc.php');
// application init
chdir(__DIR__ . "/$webRoot");
echo "\nCWD=" . getcwd() . "\n\n";
#####################################
echo "Read files from $searchDir\n";
$fileRegex = '/.*\.js$/';
$classes = getAppClasses($appJsRoot, $fileRegex, $appJsName, $appJsMain);
ksort($classes);
echo "Sorting dependencies\n";
$classesNew = sortDeps($classes, $appJsName, $appJsMain);
$out = "";
foreach ($classesNew as $className => $dummy) {
$out .= $appJsRoot .
str_replace('.', DIRECTORY_SEPARATOR, substr($className, strlen($appJsName))) . ".js\n";
}
if (count($classesNew) < count($classes) && $params['partial'] == "add") {
echo "\nAdd " . count($classes) - count($classesNew) . " unresolved classes.\n";
$out .= "\n";
foreach ($classes as $className => $dummy) {
if (!$classesNew[$className]) {
$out .= $appJsRoot .
str_replace('.', DIRECTORY_SEPARATOR, substr($className, strlen($appJsName))) . ".js\n";
}
}
} // add unresolved
// update index appjs include
echo "\n";
if ($params['apply']) {
echo "Write appjs file list $appJsList.\n";
if (file_put_contents($appJsList, $out) === false) {
echo "ERROR on write to $appJsList.\n";
}
}
else {
echo " - Check-only. Do not write file list.\n";
}
echo "\n";
#############################################################
/*
* Get app classes from fileregex
*/
function getAppClasses($dirName, $regex, $appName, $appMain, $startDir = null) {
// if startdir is not given, then use current dirname
if ($startDir === null) {
$startDir = $dirName;
}
$classes = array();
$subDirs = array();
$dh = opendir($dirName);
if ($dh === false) {
echo " *** Cannot open directory $dirName.\n";
exit;
}
while (($fileName = readdir($dh)) !== false) {
$searchFileName = "$dirName/$fileName";
if (is_dir($searchFileName)) {
if (substr($fileName, 0, 1) == '.') {
// dont process thisdir, parentdir and other dotdirs
}
else {
$subDirs[] = $searchFileName;
}
continue;
}
if (!preg_match($regex, $searchFileName)) {
continue;
}
// make classname out of directory name
// remove startdir (leading slash remains) and extension
$shortFileName = substr($searchFileName, strlen($startDir));
$className = $appName . str_replace(DIRECTORY_SEPARATOR, '.', substr($shortFileName, 0, -3));
// check if file contains class name
$content = file_get_contents($searchFileName);
// exclude app main js, because this has no class name
if ($shortFileName != DIRECTORY_SEPARATOR . $appMain) {
// TODO this is very dumb, should be refined
if (!preg_match('|' . preg_quote($className) . '|', $content)) {
echo "Warning: Cannot find class $className in $appDirRel$fileName.\n";
exit;
}
}
// add content to class array
$classes[$className] = $content;
}
// process subdirs
foreach ($subDirs as $searchFileName) {
$classes = array_merge($classes, getAppClasses($searchFileName, $regex, $appName, $appMain, $startDir));
}
return $classes;
} // eo get app classes
/*
* Sort dependencies
*/
function sortDeps($classes, $appName, $appJs) {
global $debug;
global $extAliases;
global $params;
$deps = array();
$xtypeDeps = array();
$aliases = array();
$storeIdDefs = array();
$storeIdDeps = array();
// may be we should differ between explicit dependencies like
// Ext.require method and the "requires" property
// and implicit dependencies (all other)?
// collect dependencies on other classes, xtypes and store ids for all classes
foreach ($classes as $className => $content) {
$deps[$className] = array(); // force each class to the deps array
// remove comments
$content = preg_replace('|/\*.*?\*/|s', '', $content);
$content = preg_replace('|^\s*//.*$|m', '', $content);
// collect class name dependencies
// search for application classes mentioned somewhere
if (preg_match_all("|($appName\..*?)['\"]|", $content, $matches)) {
foreach ($matches[1] as $depClass) {
// the own class is not a dependency
if ($depClass == $className) {
continue;
}
$deps[$className][$depClass] = $depClass;
}
}
// collect xtype alias (asume one alias per class/file ?)
if (preg_match_all("|alias\s*:\s*['\"]widget\.(\w+)['\"]|", $content, $matches)) {
foreach ($matches[1] as $xtype) {
if (array_key_exists($xtype, $aliases)) {
echo "Warning: Duplicate xtype alias $xtype in:\n";
echo " - " . $aliases[$xtype] . "\n";
echo " - $className\n";
}
$aliases[$xtype] = $className;
}
}
// collect xtype dependencies
if (preg_match_all("|xtype\s*:\s*['\"](\w+)['\"]|", $content, $matches)) {
foreach ($matches[1] as $xtype) {
$xtypeDeps[$className][$xtype] = $xtype;
}
}
// collect store ids
if (preg_match_all("|storeId\s*:\s*['\"](\w+)['\"]|", $content, $matches)) {
foreach ($matches[1] as $storeId) {
if (array_key_exists($storeId, $storeIdDefs)) {
echo "Warning: Duplicate storeId $storeId in:\n";
echo " - " . $storeIdDefs[$storeId] . "\n";
echo " - $className\n";
}
$storeIdDefs[$storeId] = $className;
}
}
// collect storeId dependencies
if (preg_match_all("|store\s*:\s*['\"](\w+)['\"]|", $content, $matches)) {
foreach ($matches[1] as $storeId) {
$storeIdDeps[$className][$storeId] = $storeId;
}
}
} // collect deps
// map xtype dependencies to classnames
foreach ($xtypeDeps as $className => $xtypeDep) {
foreach ($xtypeDep as $xtype) {
// we need not handle aliases already defined in ext
if (in_array($xtype, $extAliases)) {
continue;
}
$depClass = $aliases[$xtype];
if (!$depClass) {
echo "Warning: Cannot find class for xtype $xtype required in $className.\n";
continue;
}
elseif ($debug) {
echo "Xtype $xtype found in $depClass.\n";
}
$deps[$className][$depClass] = $depClass;
}
}
// map store dependencies by storeId to classnames
foreach ($storeIdDeps as $className => $storeDep) {
foreach ($storeDep as $storeId) {
$depClass = $storeIdDefs[$storeId];
if (!$depClass) {
echo "Warning: Cannot find class for storeId $storeId required in $className.\n";
continue;
}
elseif ($debug) {
echo "StoreId $storeId found in $depClass.\n";
}
$deps[$className][$depClass] = $depClass;
}
}
// check for missing class dependencies
$missing = array();
foreach ($deps as $className => $classDeps) {
foreach ($classDeps as $depName) {
if (!isset($deps[$depName])) {
echo "* $className depends on $depName which does not exist.\n";
$missing[$depName] = $depName;
if ($params['missingdep'] == "cont") {
unset($deps[$className][$depName]);
echo " - Remove dependency.\n";
}
}
}
} // eo missing check
if (count($missing)) {
switch ($params['missingdep']) {
case "cont":
// nothing to do here - already removed
break;
default: // abort
echo "\nAbort.\n\n";
exit;
}
} // eo missing
// order by pushing those classes to the stack that have no dependences
// or where the dependencies are already on the stack (resolved)
$classesNew = array();
while (count($deps)) {
$resolvedCount = 0;
foreach ($deps as $className => $classDeps) {
// if no open dependencies than push to stack and remove from deps
if (count($classDeps) == 0) {
$resolvedCount++;
$classesNew[$className] = $className;
unset($deps[$className]);
// remove from other classes dependencies
foreach ($deps as $otherClassName => $otherClassDeps) {
foreach ($otherClassDeps as $depName) {
if ($depName == $className) {
unset($deps[$otherClassName][$depName]);
}
}
}
}
elseif ($debug) {
echo "$className has " . count($classDeps) . " dependencies.\n";
}
} // eo one dep loop
if ($resolvedCount == 0) {
// - sort by dep count of a class or by
// count of references TO a class?
echo "Warning: Unresolved dependencies.\n";
foreach ($deps as $className => $classDeps) {
echo " - $className (" . count($classDeps) . ")\n";
foreach ($classDeps as $depName) {
echo " * $depName";
if (!$classes[$depName]) {
echo " (not found)";
}
echo "\n";
}
}
echo "\n";
// - circular check (direct backpointer at least)
foreach ($deps as $className => $classDeps) {
foreach ($classDeps as $depName) {
foreach ($deps[$depName] as $otherDeps) {
foreach ($otherDeps as $otherDep) {
if ($otherDep == $className) {
echo "* Curcular dependency $className <-> $backDep\n";
}
}
}
}
}
echo "\n";
switch ($params['partial']) {
case "ok":
case "add":
return $classesNew;
break;
default: // abort
exit;
}
}
} // eo while deps (files)
return $classesNew;
} // eo check js names
?>