-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.php
More file actions
112 lines (91 loc) · 3.99 KB
/
Copy pathcli.php
File metadata and controls
112 lines (91 loc) · 3.99 KB
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
#!/usr/bin/env php
<?php
declare(strict_types=1);
require_once __DIR__ . '/src/Autoloader.php';
\SEOAnalyzer\Autoloader::register();
use SEOAnalyzer\SEOAnalyzer;
use SEOAnalyzer\Exporters\JsonExporter;
use SEOAnalyzer\Exporters\HtmlReportExporter;
// Parse CLI options
$options = getopt('', ['url:', 'format::', 'export::', 'help']);
if (isset($options['help']) || empty($options['url'])) {
echo "\n\033[1;36mSEOAnalyzer - Native PHP SEO Audit Tool (CLI)\033[0m\n";
echo "---------------------------------------------------------\n";
echo "Usage:\n";
echo " php cli.php --url=https://example.com [options]\n\n";
echo "Options:\n";
echo " --url=<url> Target website URL to audit (Required)\n";
echo " --format=<type> Output format: cli (default), json, html\n";
echo " --export=<file> File path to save the exported report\n";
echo " --help Display help screen\n\n";
exit(0);
}
$url = filter_var($options['url'], FILTER_VALIDATE_URL);
if (!$url) {
echo "\033[1;31m[ERROR] Invalid URL provided: {$options['url']}\033[0m\n";
exit(1);
}
$format = strtolower($options['format'] ?? 'cli');
$exportPath = $options['export'] ?? null;
echo "\033[1;33m[+] Analyzing target URL:\033[0m {$url}\n";
try {
$analyzer = new SEOAnalyzer();
$result = $analyzer->analyze($url);
if ($format === 'json') {
$output = JsonExporter::export($result, true);
if ($exportPath) {
file_put_contents($exportPath, $output);
echo "\033[1;32m[SUCCESS] JSON report saved to: {$exportPath}\033[0m\n";
} else {
echo $output . "\n";
}
exit(0);
}
if ($format === 'html') {
$output = HtmlReportExporter::export($result);
if ($exportPath) {
file_put_contents($exportPath, $output);
echo "\033[1;32m[SUCCESS] HTML report saved to: {$exportPath}\033[0m\n";
} else {
echo $output . "\n";
}
exit(0);
}
// Default CLI Display
$data = $result->toArray();
$score = $data['score'];
$scoreColor = $score >= 80 ? "\033[1;32m" : ($score >= 50 ? "\033[1;33m" : "\033[1;31m");
echo "\n=======================================================\n";
echo " \033[1;37mSEO AUDIT RESULT:\033[0m {$scoreColor}{$score} / 100\033[0m\n";
echo " Execution Time: {$data['execution_time']} seconds\n";
echo "=======================================================\n\n";
echo "\033[1;34mSummary Counts:\033[0m\n";
echo " - Critical Issues: \033[1;31m{$data['counts']['critical']}\033[0m\n";
echo " - Warnings: \033[1;33m{$data['counts']['warning']}\033[0m\n";
echo " - Passed Checks: \033[1;32m{$data['counts']['good']}\033[0m\n\n";
echo "\033[1;34mCategory Scores:\033[0m\n";
foreach ($data['category_scores'] as $cat => $catScore) {
$cColor = $catScore >= 80 ? "\033[32m" : ($catScore >= 50 ? "\033[33m" : "\033[31m");
printf(" %-25s %s%3d / 100\033[0m\n", ucfirst($cat) . ':', $cColor, $catScore);
}
echo "\n\033[1;34mDetailed Issues & Recommendations:\033[0m\n";
echo "---------------------------------------------------------\n";
foreach ($result->getIssues() as $issue) {
$sevTag = match ($issue->severity->value) {
'critical' => "\033[1;41;37m CRITICAL \033[0m",
'warning' => "\033[1;43;30m WARNING \033[0m",
'good' => "\033[1;42;30m PASSED \033[0m",
};
echo "{$sevTag} \033[1m{$issue->title}\033[0m [{$issue->category->getTitle()}]\n";
echo " Detail: {$issue->description}\n";
echo " Fix: \033[0;36m{$issue->recommendation}\033[0m\n\n";
}
if ($exportPath) {
$htmlOutput = HtmlReportExporter::export($result);
file_put_contents($exportPath, $htmlOutput);
echo "\033[1;32m[SUCCESS] Report exported to: {$exportPath}\033[0m\n";
}
} catch (\Throwable $e) {
echo "\033[1;31m[ERROR] Analysis failed: {$e->getMessage()}\033[0m\n";
exit(1);
}