1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\HttpStatusCheck; |
4
|
|
|
|
5
|
|
|
use Spatie\Crawler\CrawlObserver; |
6
|
|
|
use Psr\Http\Message\UriInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use GuzzleHttp\Exception\RequestException; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
class CrawlLogger extends CrawlObserver |
12
|
|
|
{ |
13
|
|
|
const UNRESPONSIVE_HOST = 'Host did not respond'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
17
|
|
|
*/ |
18
|
|
|
protected $consoleOutput; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $crawledUrls = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string|null |
27
|
|
|
*/ |
28
|
|
|
protected $outputFile = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $consoleOutput |
32
|
|
|
*/ |
33
|
|
|
public function __construct(OutputInterface $consoleOutput) |
34
|
|
|
{ |
35
|
|
|
$this->consoleOutput = $consoleOutput; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Called when the crawl will crawl the url. |
40
|
|
|
* |
41
|
|
|
* @param \Psr\Http\Message\UriInterface $url |
42
|
|
|
*/ |
43
|
|
|
public function willCrawl(UriInterface $url) |
44
|
|
|
{ |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Called when the crawl has ended. |
49
|
|
|
*/ |
50
|
|
|
public function finishedCrawling() |
51
|
|
|
{ |
52
|
|
|
$this->consoleOutput->writeln(''); |
53
|
|
|
$this->consoleOutput->writeln('Crawling summary'); |
54
|
|
|
$this->consoleOutput->writeln('----------------'); |
55
|
|
|
|
56
|
|
|
ksort($this->crawledUrls); |
57
|
|
|
|
58
|
|
|
foreach ($this->crawledUrls as $statusCode => $urls) { |
59
|
|
|
$colorTag = $this->getColorTagForStatusCode($statusCode); |
60
|
|
|
|
61
|
|
|
$count = count($urls); |
62
|
|
|
|
63
|
|
|
if (is_numeric($statusCode)) { |
64
|
|
|
$this->consoleOutput->writeln("<{$colorTag}>Crawled {$count} url(s) with statuscode {$statusCode}</{$colorTag}>"); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($statusCode == static::UNRESPONSIVE_HOST) { |
68
|
|
|
$this->consoleOutput->writeln("<{$colorTag}>{$count} url(s) did have unresponsive host(s)</{$colorTag}>"); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->consoleOutput->writeln(''); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getColorTagForStatusCode(string $code): string |
76
|
|
|
{ |
77
|
|
|
if ($this->startsWith($code, '2')) { |
78
|
|
|
return 'info'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($this->startsWith($code, '3')) { |
82
|
|
|
return 'comment'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return 'error'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string|null $haystack |
90
|
|
|
* @param string|array $needles |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function startsWith($haystack, $needles): bool |
95
|
|
|
{ |
96
|
|
|
foreach ((array) $needles as $needle) { |
97
|
|
|
if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the filename to write the output log. |
107
|
|
|
* |
108
|
|
|
* @param string $filename |
109
|
|
|
*/ |
110
|
|
|
public function setOutputFile($filename) |
111
|
|
|
{ |
112
|
|
|
$this->outputFile = $filename; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function crawled( |
116
|
|
|
UriInterface $url, |
117
|
|
|
ResponseInterface $response, |
118
|
|
|
?UriInterface $foundOnUrl = null |
119
|
|
|
) { |
120
|
|
|
$statusCode = $response->getStatusCode(); |
121
|
|
|
|
122
|
|
|
$reason = $response->getReasonPhrase(); |
123
|
|
|
|
124
|
|
|
$colorTag = $this->getColorTagForStatusCode($statusCode); |
125
|
|
|
|
126
|
|
|
$timestamp = date('Y-m-d H:i:s'); |
127
|
|
|
|
128
|
|
|
$message = "{$statusCode} {$reason} - ".(string) $url; |
129
|
|
|
|
130
|
|
|
if ($this->outputFile && $colorTag === 'error') { |
|
|
|
|
131
|
|
|
file_put_contents($this->outputFile, $message.PHP_EOL, FILE_APPEND); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->consoleOutput->writeln("<{$colorTag}>[{$timestamp}] {$message}</{$colorTag}>"); |
135
|
|
|
|
136
|
|
|
$this->crawledUrls[$statusCode][] = $url; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function crawlFailed( |
140
|
|
|
UriInterface $url, |
141
|
|
|
RequestException $requestException, |
142
|
|
|
?UriInterface $foundOnUrl = null |
143
|
|
|
) { |
144
|
|
|
$statusCode = self::UNRESPONSIVE_HOST; |
145
|
|
|
|
146
|
|
|
$reason = $requestException->getResponse()->getReasonPhrase(); |
147
|
|
|
|
148
|
|
|
$colorTag = $this->getColorTagForStatusCode($statusCode); |
149
|
|
|
|
150
|
|
|
$timestamp = date('Y-m-d H:i:s'); |
151
|
|
|
|
152
|
|
|
$message = "{$statusCode}: {$reason} - ".(string) $url; |
153
|
|
|
|
154
|
|
|
if ($foundOnUrl) { |
155
|
|
|
$message .= " (found on {$foundOnUrl})"; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($this->outputFile) { |
|
|
|
|
159
|
|
|
file_put_contents($this->outputFile, $message.PHP_EOL, FILE_APPEND); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->consoleOutput->writeln("<{$colorTag}>[{$timestamp}] {$message}</{$colorTag}>"); |
163
|
|
|
|
164
|
|
|
$this->crawledUrls[$statusCode][] = $url; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: