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