-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.php
120 lines (95 loc) · 2.69 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
require_once __DIR__.'/src/Runner.php';
require_once __DIR__.'/src/Report.php';
class Parser
{
const supported_commands = [
'exit' => 'exit from program',
'parse' => 'parse site,'.
' site is first parameter ,'.PHP_EOL.
' example : parse www.netpeak.ua',
'report' => 'generating report for domain'.PHP_EOL.
'example report www.netpeak.ua',
'help' => 'show list of available commands',
];
public $argv;
public function __construct($argv)
{
$this->argv = $argv;
}
public function executeCommand()
{
$this->printMessage("Hello");
$res = $this->argv[1] ?? 'empty command';
switch ($res) {
case 'parse':
$this->executeParse();
break;
case 'report':
$this->executeReport();
break;
case 'help':
$this->printListOfCommands();
break;
default:
$this->executeNotSupported($res);
break;
}
$this->printMessage("Good bye!");
}
/**
* @param string $message
*/
private function printMessage(string $message): void
{
echo "\n {$message} \n";
}
private function executeParse(): void
{
$site = $this->argv[2] ?? '';
if ($site === '') {
$message = "Bad url: $site Sorry :(";
$this->printMessage($message);
return;
}
$exploded = explode('.', $site);
if (count($exploded) < 2) {
$message = "Bad url. Sorry :(";
$this->printMessage($message);
} else {
$runner = new Runner($site);
$runner->run();
}
}
private function executeReport()
{
$domain = $this->argv[2] ?? '';
if (!$domain) {
echo "\n domain is empty \n";
return;
}
$report = new Report($domain);
$report->printReport();
}
private function printListOfCommands()
{
$this->printMessage("=== Supported commands: ===");
foreach (self::supported_commands as $command => $description) {
$message = "$command: \n $description ";
$this->printMessage($message);
$this->printMessage("-----------");
}
}
/**
* @param $res
*/
private function executeNotSupported($res): void
{
$message = "$res not supported";
$this->printMessage($message);
$this->printListOfCommands();
exit;
}
}
$parser = new Parser($argv);
$parser->executeCommand();