-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
349 lines (328 loc) · 13.3 KB
/
index.php
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
<?php
require './config.php';
class vizWalker {
/** @var array */
private $data = [];
/** @var string */
private $pdep = '';
/** @var string */
private $cache_file = '';
function __construct(string $pdep, string $cache_file) {
$this->pdep = $pdep;
$this->cache_file = $cache_file;
}
function loadGraphData():bool {
$data = json_decode(file_get_contents($this->cache_file), true);
if (!$data) {
echo json_last_error_msg();
return false;
}
$this->data = $data;
return true;
}
function getDot(string $node, string $mode = 'class', string $depth = '1', bool $ignore_static = false):string {
$flags = " -d $depth";
if ($ignore_static) {
$flags .= " --ignore-static";
}
$flags .= " -g -i " . $this->cache_file;
if ($mode == 'class') {
$flags .= ' -c ';
} elseif ($mode == 'file') {
$flags .= ' -f ';
}
$dot = (string)shell_exec($this->pdep . $flags . escapeshellcmd($node));
// Make tests yellow
$dot = preg_replace("#^(.*tests{0,1}/.*)\[shape=box\]#m", '$1[shape=box,style=filled,fillcolor=lightyellow]', $dot);
return $dot;
}
function getDeps(string $node, string $mode = 'class', string $depth = '1', bool $ignore_static = false):string {
$flags = " -j -d $depth";
if ($ignore_static) {
$flags .= " --ignore-static";
}
$flags .= " -i " . $this->cache_file;
if ($mode == 'class') {
$flags .= ' -c ';
} elseif ($mode == 'file') {
$flags .= ' -f ';
}
return (string)shell_exec($this->pdep . $flags . escapeshellcmd($node));
}
function search(string $str, string $mode):array {
if (empty($this->data)) {
$this->loadGraphData();
}
$fnc = function($el) use($str) { return (stripos($el, $str) !== false); };
if ($mode == 'class') {
return array_keys(array_filter($this->data['ctype'], $fnc, ARRAY_FILTER_USE_KEY));
} else {
return array_keys(array_filter($this->data['fgraph'], $fnc, ARRAY_FILTER_USE_KEY));
}
}
}
$v = new vizWalker($config['pdep_fullpath'], $config['cached_graph_fullpath']);
if(!empty($_GET['csearch'])) {
$matches = $v->search((string)$_GET['csearch'], 'class');
$res = [];
$i = 0;
foreach($matches as $val) {
$res[$i]['id'] = $val;
$res[$i++]['text'] = trim($val,'\\');
}
header("Content-Type: application/json");
echo json_encode(['results'=>$res]);
return;
} elseif(!empty($_GET['fsearch'])) {
$matches = $v->search((string)$_GET['fsearch'], 'file');
$res = [];
$i = 0;
foreach($matches as $val) {
$res[$i]['id'] = $val;
$res[$i++]['text'] = $val;
}
header("Content-Type: application/json");
echo json_encode(['results'=>$res]);
return;
} elseif(!empty($_GET['ajax']) && !empty($_GET['mode']) && !empty($_GET['node']) && !empty($_GET['d'])) {
$inh = $_GET['inh'] ?? false;
echo $v->getDot($_GET['node'], $_GET['mode'], $_GET['d'], $inh);
return;
} elseif(basename($_SERVER['SCRIPT_NAME']) == 'api.php' && !empty($_GET['mode']) && !empty($_GET['node']) && !empty($_GET['d'])) {
$inh = $_GET['inh'] ?? false;
echo $v->getDeps($_GET['node'], $_GET['mode'], $_GET['d'], $inh);
return;
} elseif(!empty($_GET['class'])) {
$mode = 'class';
$node = htmlspecialchars($_GET['class']);
} elseif(!empty($_GET['file'])) {
$mode = 'file';
$node = htmlspecialchars($_GET['file']);
}
$mode = $_GET['mode'] ?? 'class';
$depth = $_GET['d'] ?? 1;
$node = $_GET['node'] ?? '';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<base href="<?=$config['install_uri']?>">
<title>Code Graph</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/10.6.2/css/bootstrap-slider.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div class="container-fluid">
<div class="modal" tabindex="-1" role="dialog" id="infoModal">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Etsyweb Class and File Dependencies</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>
black lines on the graph indicate inheritance<br/>
<span style="color:#E66100">orange</span> lines are method calls<br/>
<span style="color:#5D3A9B">solid purple</span> lines indicate a static method call<br/>
<span style="color:#5D3A9B">dashed purple</span> lines indicate a static constant dependency<br/>
</p>
<p>
On the file graph, the numbers on the edges indicate the line number the dependency occurs on
</p>
<p>
The 1-5 slider is the dependency depth. For very connected nodes, going above 2 may not work.
</p>
<p>
Nodes on the graph are clickable and will give you the dependency graph for that node
</p>
<p>
You can also paste a class in the file input field in order to get the file-based dependencies
of that class. Same goes for putting a filename in the class field.
</p>
<p>
Note that rendering is done in the client browser. You may hit stack size issues on graphs with a lot of nodes.
</p>
</div>
</div>
</div>
</div>
<div id="controls" class="col-md-4" style="position:relative;">
<p>
<br />
<form id="form1">
<button type="button" class="btn btn-outline-dark btn-sm" data-toggle="modal" data-target="#infoModal" style="position:absolute; top: 1px; right: 1px; font-size: 0.5rem;">Eh?</button>
<input id="classes" class="form-control" placeholder="Class search" type="text" autocomplete="off"
<?php
if($node && $mode=='class') {
echo "value=\"$node\"";
}
?>
>
<input id="files" class="form-control" placeholder="Filename search" type="text" autocomplete="off"
<?php
if($node && $mode=='file') {
echo "value=\"$node\"";
}
?>
>
</p>
<p>
<input id="sl1" data-slider-id='depthSlider' type="text" data-slider-min="1" data-slider-max="5" data-slider-step="1" data-slider-value="1"/>
</p>
</form>
</div>
<div id="graph" class="col-md-12">
</div>
</div>
<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://unpkg.com/bootstrap@4.3.1/dist/js/bootstrap.min.js"></script>
<script src="https://unpkg.com/bootstrap-slider@10.6.2/dist/bootstrap-slider.min.js"></script>
<script src="https://unpkg.com/d3@5.12.0/dist/d3.min.js"></script>
<script src="https://unpkg.com/viz.js@1.8.2/viz.js" type="javascript/worker"></script>
<script src="https://unpkg.com/d3-graphviz@2.6.1/build/d3-graphviz.js"></script>
<script src="https://cdn.jsdelivr.net/gh/xcash/bootstrap-autocomplete@v2.2.2/dist/latest/bootstrap-autocomplete.min.js"></script>
<script src="js/scripts.js"></script>
<script>
var mode = '<?= $mode?>';
var node = '<?= addslashes($node)?>';
var nodes = [];
var slider = null;
var transitionSmooth = d3.transition("smooth")
.duration(750)
.ease(d3.easeLinear);
var graphviz = d3.select("#graph").graphviz()
.totalMemory(Math.pow(2,27))
.width($("#graph").width())
.height($(window).innerHeight())
.fit(true)
.logEvents(true)
.onerror(function(msg) { this.renderDot('graph {"'+msg+'" [shape=none,fontcolor=red] }'); })
.transition(function() {
return d3.transition("smooth").duration(800);
});
function graph(pushState) {
var d = $('#sl1').val();
if (typeof graph.numDrawn == 'undefined') {
graph.numDrawn = 0;
}
$(".slider-handle").addClass("spinner-grow");
console.log("Graphing " + node);
fetch(`?ajax=1&mode=`+mode+`&node=`+node+`&d=`+d)
.then(function(response) {
return response.text();
})
.then(function(text) {
if (text) {
if(pushState) {
if(graph.numDrawn > 0) {
console.log("Pushing state" + '?mode='+mode+'&node='+node+'&d='+d);
history.pushState({mode:mode,node:node,d:d},'','?mode='+mode+'&node='+node+'&d='+d);
} else {
console.log("Replacing state" + '?mode='+mode+'&node='+node+'&d='+d);
history.replaceState({mode:mode,node:node,d:d},'','?mode='+mode+'&node='+node+'&d='+d);
}
}
try {
graphviz.renderDot(text);
} catch(e) {
console.log(e);
$(".slider-handle").removeClass("spinner-grow");
}
graphviz.on('end', function() {
nodes = d3.selectAll('.node');
graph.numDrawn++;
$(".slider-handle").removeClass("spinner-grow");
nodes.on("click", function () {
node = d3.select(this).selectAll('text').text();
if(mode=='class') {
$("#classes").val(node);
} else {
$("#files").val(node);
}
graph(true);
return false;
});
});
} else {
$(".slider-handle").removeClass("spinner-grow");
}
});
return false;
}
$("#sl1").on("change", function() { graph(true); return false; });
$("#classes").on("autocomplete.select", function(evt, el) { mode = 'class'; node = el.id; graph(true); return false; });
$("#classes").on("autocomplete.freevalue", function(evt, val) { mode = 'class'; node = val; graph(true); return false; });
$('#classes').autoComplete({
resolver: 'custom',
formatResult: function (el) {
return {
value: el.id,
text: el.text
};
},
events: {
search: function (q, cb) {
$.ajax('', { data: { 'csearch': q} }).done(function (res) {
cb(res.results)
});
}
}
});
$("#files").on("autocomplete.select", function(evt, el) { mode = 'file'; node = el.id; graph(true); return false; });
$("#files").on("autocomplete.freevalue", function(evt, val) { mode = 'file'; node = val; graph(true); return false; });
$('#files').autoComplete({
resolver: 'custom',
formatResult: function (el) {
return {
value: el.id,
text: el.text
};
},
events: {
search: function (q, cb) {
$.ajax('', { data: { 'fsearch': q} }).done(function (res) {
cb(res.results)
});
}
}
});
$(document).ready(function() {
$('#sl1').slider({
tooltip: 'hide',
value: <?=$depth?>,
ticks: [1, 2, 3, 4, 5],
ticks_labels: ['1', '2', '3', '4', '5'],
});
$('#form1').on('click', '.dropdown-item', function () {
return false;
});
if (node.length) {
graph(true);
}
});
$(window).bind("popstate", function(e) {
var state = e.originalEvent.state;
if (state !== null) {
$('#sl1').slider('setValue', state.d);
mode = state.mode;
node = state.node;
if(mode=='class') {
$("#classes").val(node);
} else {
$("#files").val(node);
}
graph(false);
}
return true;
});
</script>
</body>
</html>