-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapClasses.php
257 lines (210 loc) · 6.01 KB
/
MapClasses.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
<?php
/*
* @package MapClasses
* @version 1.0.0
* @author Ricardo Sismeiro <ricardo@sismeiro.com>
* @copyright 2012 Ricardo Sismeiro
* @license http://creativecommons.org/licenses/by-nc-sa/3.0/
* @filesource
*/
class MapClasses
{
public function __construct()
{
ignore_user_abort(true);
set_time_limit(0);
$this->_sendHeaders();
}
private function _sendHeaders()
{
header("HTTP/1.1 200 OK", true, 200);
}
public function run()
{
$directory = isset($_GET['dir']) ? $_GET['dir'] : '';
$directoryHtml = htmlentities($directory);
if (empty($directory)) {
exit;
}
$this->_outputHeader();
$this->_outputFlush();
$this->_ouputBody($directory);
$this->_outputFlush();
$this->_outputFooter();
$this->_outputFlush();
}
private function _outputFooter()
{
$result = '</body>' . PHP_EOL . '</html>';
echo $result;
}
private function _outputFlush()
{
ob_flush();
flush();
}
private function _ouputBody($directory)
{
$result = '<body>' . PHP_EOL;
echo $result;
$data = MapClassesSearch::classesCalls($directory);
$this->_outputScript($data);
}
private function _formatGraphData($a)
{
$r = '';
$_classes = array();
foreach ($a as $k => $v) {
foreach ($v as $i => $s) {
if (!isset($_classes[$i])) {
$_classes[$i] = 0;
}
$_classes[$i]++;
}
}
foreach ($a as $k => $v) {
$t = substr(md5($k), -6) . ' : ' . basename($k);
$_c = count($v);
foreach ($v as $i => $s) {
$r .= '{source: "' . $i . '", target: "' . $t . '", type: "suit" , group : "' . $_c . '" , ccall : "' . $_classes[$i] . '"},';
}
}
if (!empty($r)) {
$r = substr($r, 0, -1);
}
$r = '[' . $r . ']';
return $r;
}
private function _outputScript($data)
{
$result = '
<script type="text/javascript">
var links = ' . $this->_formatGraphData($data) . ';' . PHP_EOL;
$result .=<<<OUTPUT
var nodes = {};
// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, tipo : 0, ccall : link.ccall});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, tipo : link.group , ccall : 0});
});
var w = 2600,
h = 2600,
fill = d3.scale.category20();
var classSize = function (i){
var f = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22];
if (i>20) {i = 20;}
return f[i];
};
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([w, h])
.linkDistance(100)
.charge(-500)
.on("tick", tick)
.start();
var svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h);
// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
.data(["suit", "licensing", "resolved"])
.enter().append("marker")
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("d", "M0,-5L10,0L0,5");
var path = svg.append("g").selectAll("path")
.data(force.links())
.enter().append("path")
.attr("class", function(d) { return "link " + d.type; })
.attr("marker-end", function(d) { return "url(#" + d.type + ")"; });
var circle = svg.append("g").selectAll("circle")
.data(force.nodes())
.enter().append("circle")
.attr("r", function(d){ if (d.ccall > 0) return classSize(d.ccall); else return 6;})
.attr("fill", function(d) { if (d.tipo > 0) return fill(d.tipo); else return '#ccdd11'; })
.call(force.drag);
var text = svg.append("g").selectAll("g")
.data(force.nodes())
.enter().append("g");
// A copy of the text with a thick white stroke for legibility.
text.append("text")
.attr("x", 8)
.attr("y", ".31em")
.attr("class", "shadow")
.text(function(d) { return d.name; });
text.append("text")
.attr("x", 8)
.attr("y", ".31em")
.text(function(d) { return d.name; });
// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
path.attr("d", function(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy);
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
});
circle.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
text.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
</script>
OUTPUT;
echo $result;
}
private function _outputHeader()
{
$result = <<<OUTPUT
<!DOCTYPE html>
<html>
<head>
<title>MapClasses</title>
<meta charset="UTF-8" />
<!-- for more info http://mbostock.github.com/d3/ -->
<script type="text/javascript" src="http://sismeiro.com/d3/d3.js"></script>
<script type="text/javascript" src="http://sismeiro.com/d3/d3.geom.js"></script>
<script type="text/javascript" src="http://sismeiro.com/d3/d3.layout.js"></script>
<style type="text/css">
path.link {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
marker#licensing {
fill: green;
}
path.link.licensing {
stroke: green;
}
path.link.resolved {
stroke-dasharray: 0,2 1;
}
circle {
stroke: #333;
stroke-width: 1.5px;
}
text {
font: 9px sans-serif;
pointer-events: none;
}
text.shadow {
stroke: #fff;
stroke-width: 3px;
stroke-opacity: .8;
}
</style>
</head>
OUTPUT;
echo $result;
}
}