|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class MyVisitor extends \PhpParser\NodeVisitorAbstract |
|
4
|
|
|
{ |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @var |
|
8
|
|
|
*/ |
|
9
|
|
|
private $callback; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @param $callback |
|
13
|
|
|
*/ |
|
14
|
|
|
public function __construct($callback) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->callback = $callback; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritdoc |
|
21
|
|
|
*/ |
|
22
|
|
|
public function leaveNode(\PhpParser\Node $node) |
|
23
|
|
|
{ |
|
24
|
|
|
call_user_func($this->callback, $node); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param $node |
|
30
|
|
|
* @param $callback |
|
31
|
|
|
*/ |
|
32
|
|
|
function iterate_over_node($node, $callback) |
|
33
|
|
|
{ |
|
34
|
|
|
/* |
|
35
|
|
|
// way 2 |
|
36
|
|
|
foreach ($node->getSubNodeNames() as $name) { |
|
37
|
|
|
$subNode = $node->$name; |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
if ($subNode instanceof \PhpParser\Node) { |
|
42
|
|
|
|
|
43
|
|
|
return iterate_over_node($node, $callback); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (is_array($subNode)) { |
|
47
|
|
|
foreach ($subNode as $sub) { |
|
48
|
|
|
return iterate_over_node($sub, $callback); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $callback($node); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $callback($node); |
|
56
|
|
|
*/ |
|
57
|
|
|
|
|
58
|
|
|
/* |
|
59
|
|
|
// way 1 |
|
60
|
|
|
static $traverser; |
|
61
|
|
|
if (!isset($traverser)) { |
|
62
|
|
|
$myVisitor = new MyVisitor($callback); |
|
63
|
|
|
$traverser = new \PhpParser\NodeTraverser(); |
|
64
|
|
|
$traverser->addVisitor($myVisitor); |
|
65
|
|
|
} |
|
66
|
|
|
$traverser->traverse(array($node)); |
|
67
|
|
|
return; |
|
68
|
|
|
*/ |
|
69
|
|
|
|
|
70
|
|
|
// way 1 |
|
71
|
|
|
$myVisitor = new MyVisitor($callback); |
|
72
|
|
|
$traverser = new \PhpParser\NodeTraverser(); |
|
73
|
|
|
$traverser->addVisitor($myVisitor); |
|
74
|
|
|
$traverser->traverse([$node]); |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $node |
|
80
|
|
|
* @return string|null |
|
81
|
|
|
*/ |
|
82
|
|
|
function getNameOfNode($node) |
|
83
|
|
|
{ |
|
84
|
|
|
if (is_string($node)) { |
|
85
|
|
|
return $node; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($node instanceof \PhpParser\Node\Name\FullyQualified) { |
|
89
|
|
|
return (string)$node; |
|
90
|
|
|
} |
|
91
|
|
|
if ($node instanceof \PhpParser\Node\Expr\New_) { |
|
92
|
|
|
return getNameOfNode($node->class); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (isset($node->class)) { |
|
96
|
|
|
return getNameOfNode($node->class); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ($node instanceof \PhpParser\Node\Name) { |
|
100
|
|
|
return (string)implode($node->parts); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\Variable) { |
|
104
|
|
|
return getNameOfNode($node->name); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\MethodCall) { |
|
108
|
|
|
return getNameOfNode($node->name); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if ($node instanceof \PhpParser\Node\Expr\ArrayDimFetch) { |
|
112
|
|
|
return getNameOfNode($node->var); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\BinaryOp) { |
|
116
|
|
|
return get_class($node->name); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if ($node instanceof \PhpParser\Node\Expr\PropertyFetch) { |
|
120
|
|
|
return getNameOfNode($node->var); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if (isset($node->name) && !is_string($node->name)) { |
|
124
|
|
|
return getNameOfNode($node->name); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if (isset($node->name) && null === $node->name) { |
|
128
|
|
|
return 'anonymous@' . spl_object_hash($node); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (isset($node->name)) { |
|
132
|
|
|
return (string)$node->name; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return null; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param $src |
|
140
|
|
|
* @param $dst |
|
141
|
|
|
*/ |
|
142
|
|
|
function recurse_copy($src, $dst) |
|
143
|
|
|
{ |
|
144
|
|
|
$dir = opendir($src); |
|
145
|
|
|
if (!file_exists($dst)) { |
|
146
|
|
|
mkdir($dst); |
|
147
|
|
|
} |
|
148
|
|
|
while (false !== ($file = readdir($dir))) { |
|
149
|
|
|
if (($file != '.') && ($file != '..')) { |
|
150
|
|
|
if (is_dir($src . '/' . $file)) { |
|
151
|
|
|
recurse_copy($src . '/' . $file, $dst . '/' . $file); |
|
152
|
|
|
} else { |
|
153
|
|
|
copy($src . '/' . $file, $dst . '/' . $file); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
closedir($dir); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
function getVersion() |
|
164
|
|
|
{ |
|
165
|
|
|
return 'v2.7.4'; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param array $array |
|
170
|
|
|
* @param string $attribute |
|
171
|
|
|
* @param mixed $currentValue |
|
172
|
|
|
* @return false|float |
|
173
|
|
|
*/ |
|
174
|
|
|
function gradientAlphaFor($array, $attribute, $currentValue) |
|
175
|
|
|
{ |
|
176
|
|
|
// memory cache |
|
177
|
|
|
static $caches; |
|
178
|
|
|
if(null === $caches) { |
|
179
|
|
|
$caches = []; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if(!isset($caches[$attribute])) { |
|
183
|
|
|
// avoid to iterate over array too many times |
|
184
|
|
|
$max = 0; |
|
185
|
|
|
$min = 1; |
|
186
|
|
|
foreach($array as $item) { |
|
187
|
|
|
if(!isset($item[$attribute])) { |
|
188
|
|
|
continue; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$max = max($max, $item[$attribute]); |
|
192
|
|
|
$min = min($min, $item[$attribute]); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$caches[$attribute]['max'] = $max; |
|
196
|
|
|
$caches[$attribute]['min'] = $min; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$max = $caches[$attribute]['max']; |
|
200
|
|
|
$min = $caches[$attribute]['min']; |
|
201
|
|
|
|
|
202
|
|
|
$percent = (($currentValue - $min) * 100) / (max(1, $max - $min)); |
|
203
|
|
|
return round($percent / 100, 2); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Style an element according its position in range |
|
208
|
|
|
* |
|
209
|
|
|
* @param array $array |
|
210
|
|
|
* @param string $attribute |
|
211
|
|
|
* @param mixed $currentValue |
|
212
|
|
|
* @return string |
|
213
|
|
|
*/ |
|
214
|
|
|
function gradientStyleFor($array, $attribute, $currentValue) { |
|
215
|
|
|
return sprintf(' style="background-color: hsla(203, 82%%, 76%%, %s);"', gradientAlphaFor($array, $attribute, $currentValue)); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Calculate percentalies |
|
220
|
|
|
* |
|
221
|
|
|
* @param float[]|int[] $arr |
|
222
|
|
|
* @param float $percentile |
|
223
|
|
|
* @return mixed |
|
224
|
|
|
*/ |
|
225
|
|
|
function percentile($arr, $percentile = 0.95) |
|
226
|
|
|
{ |
|
227
|
|
|
sort($arr); |
|
228
|
|
|
return $arr[max(round($percentile * count($arr) - 1.0 - $percentile), 0)]; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Download the given URI and decode it as JSON. |
|
233
|
|
|
* @param string $uri |
|
234
|
|
|
* |
|
235
|
|
|
* @return mixed |
|
236
|
|
|
*/ |
|
237
|
|
|
function getURIContentAsJson($uri){ |
|
238
|
|
|
// get the json file |
|
239
|
|
|
$httpsProxy = getenv('https_proxy'); |
|
240
|
|
|
if (!empty($httpsProxy)) { |
|
241
|
|
|
return json_decode(@file_get_contents($uri, false, [ |
|
242
|
|
|
'http' => [ |
|
243
|
|
|
'proxy' => str_replace( |
|
244
|
|
|
'https://', |
|
245
|
|
|
'tcp://', |
|
246
|
|
|
str_replace( |
|
247
|
|
|
'http://', |
|
248
|
|
|
'tcp://', |
|
249
|
|
|
$httpsProxy |
|
250
|
|
|
) |
|
251
|
|
|
), |
|
252
|
|
|
'request_fulluri' => true, |
|
253
|
|
|
], |
|
254
|
|
|
])); |
|
255
|
|
|
} else { |
|
256
|
|
|
return json_decode(@file_get_contents($uri)); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|