1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\CacheBundle\Url; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use Http\Message\MessageFactory; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Psr\Log\LogLevel; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Kevin Bond <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Crawler implements \Countable |
15
|
|
|
{ |
16
|
|
|
private $httpClient; |
17
|
|
|
private $messageFactory; |
18
|
|
|
private $logger; |
19
|
|
|
private $urlProviders; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param HttpClient $httpClient |
23
|
|
|
* @param MessageFactory $messageFactory |
24
|
|
|
* @param LoggerInterface $logger |
25
|
|
|
* @param UrlProvider[] $urlProviders |
26
|
|
|
*/ |
27
|
6 |
|
public function __construct(HttpClient $httpClient, MessageFactory $messageFactory, LoggerInterface $logger = null, array $urlProviders = []) |
28
|
|
|
{ |
29
|
6 |
|
$this->httpClient = $httpClient; |
30
|
6 |
|
$this->messageFactory = $messageFactory; |
31
|
6 |
|
$this->logger = $logger; |
32
|
6 |
|
$this->urlProviders = $urlProviders; |
33
|
6 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param UrlProvider $provider |
37
|
|
|
*/ |
38
|
2 |
|
public function addUrlProvider(UrlProvider $provider) |
39
|
|
|
{ |
40
|
2 |
|
$this->urlProviders[] = $provider; |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
4 |
|
public function count() |
47
|
|
|
{ |
48
|
4 |
|
$count = 0; |
49
|
|
|
|
50
|
4 |
|
foreach ($this->urlProviders as $provider) { |
51
|
4 |
|
$count += count($provider); |
52
|
4 |
|
} |
53
|
|
|
|
54
|
4 |
|
return $count; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param callable $callback Response as first argument, calling URL as second |
59
|
|
|
*/ |
60
|
4 |
|
public function crawl(callable $callback = null) |
61
|
|
|
{ |
62
|
4 |
|
foreach ($this->getUrls() as $url) { |
63
|
4 |
|
$response = $this->httpClient->sendRequest($this->messageFactory->createRequest('GET', $url)); |
64
|
|
|
|
65
|
4 |
|
$this->log($response, $url); |
66
|
|
|
|
67
|
4 |
|
if ($callback) { |
68
|
4 |
|
$callback($response, $url); |
69
|
4 |
|
} |
70
|
4 |
|
} |
71
|
4 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ResponseInterface $response |
75
|
|
|
* @param string $url |
76
|
|
|
*/ |
77
|
4 |
|
private function log(ResponseInterface $response, $url) |
78
|
|
|
{ |
79
|
4 |
|
if (null === $this->logger) { |
80
|
2 |
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
$status = $response->getStatusCode(); |
84
|
|
|
|
85
|
2 |
|
$this->logger->log($status == 200 ? LogLevel::DEBUG : LogLevel::NOTICE, sprintf('[%s] %s', $status, $url)); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
4 |
|
private function getUrls() |
92
|
|
|
{ |
93
|
4 |
|
$urls = []; |
94
|
|
|
|
95
|
4 |
|
foreach ($this->urlProviders as $provider) { |
96
|
4 |
|
$urls = array_merge($urls, $provider->getUrls()); |
97
|
4 |
|
} |
98
|
|
|
|
99
|
4 |
|
return $urls; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|