forked from hng-teamfury/team-fury-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.php
102 lines (86 loc) · 2.94 KB
/
parser.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
<?php
function parse_scripts($toJson = false, $prettify = false) {
// scan directory for files matching pattern(s)
// store filename in object variable
$cli = [
'py' => 'python3',
'js' => 'node',
'php' => 'php',
'java' => 'java',
'dart' => 'dart'
];
$regex = '/Hello World, this is ([a-zA-Z\s-]+) with HNGi7 ID (HNG-\d{5}) and email ([_a-z0-9-]+[\.[_a-z0-9-]+]*@[a-z0-9-]+[\.[a-z0-9-]+]*.[a-z]{2,3}) using ([\w\s]+) for stage 2 task/i';
$totalResults = [];
$passCount = 0;
$failCount = 0;
$totalCount = 0;
foreach (glob("scripts/*.{js,py,php}", GLOB_BRACE) as $filename) {
$fileExt = pathinfo($filename, PATHINFO_EXTENSION);
// echo "$fileExt , $filename \n";
$result = new stdClass();
// for each file, execute the appropriate CLI program
$output = shell_exec("$cli[$fileExt] $filename");
// if success then
if ($output) {
// parse output with regex
$matched = preg_match($regex, $output, $matches);
// if regex match
if ($matched) {
// store fullname in property
$result->fullname = $matches[1];
// store ID in property
$result->id = $matches[2];
// store email in property
$result->email = $matches[3];
// store language in a property
$result->language = $matches[4];
// store passed in property
$result->status = "pass";
$result->output = $output;
$result->file = $filename;
$passCount += 1;
}
else {
// else
// store fail for result property
$result->status = "fail";
$result->output = $output;
$result->file = $filename;
$failCount += 1;
$result->fullname = "undefined";
$result->email = "undefined";
$result->id = "undefined";
$result->language = "undefined";
}
}
else {
// store fail for result property in object variable
$result->status = "fail";
$result->output = "Invalid script found - $filename";
$result->file = $filename;
$failCount +=1;
$result->email = "undefined";
$result->fullname = "undefined";
$result->id = "undefined";
$result->language = "undefined";
}
$totalResults[] = $result;
$totalCount += 1;
}
$summary = new stdClass();
$summary->totalResults = $totalResults;
$summary->passCount = $passCount;
$summary->failCount = $failCount;
$summary->totalCount = $totalCount;
if ($toJson) {
// return json
if($prettify){
$summary = json_encode($totalResults, JSON_PRETTY_PRINT);
}
else {
$summary = json_encode($totalResults);
}
}
return $summary;
}
?>