1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the feed-io package. |
4
|
|
|
* |
5
|
|
|
* (c) Alexandre Debril <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace FeedIo; |
12
|
|
|
|
13
|
|
|
use FeedIo\ParserAbstract; |
14
|
|
|
use FeedIo\Adapter\ClientInterface; |
15
|
|
|
use FeedIo\Adapter\ResponseInterface; |
16
|
|
|
use FeedIo\Reader\Document; |
17
|
|
|
use FeedIo\Reader\ReadErrorException; |
18
|
|
|
use FeedIo\Reader\Result; |
19
|
|
|
use FeedIo\Reader\NoAccurateParserException; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Consumes feeds and return corresponding Result instances |
24
|
|
|
* |
25
|
|
|
* Depends on : |
26
|
|
|
* - FeedIo\Adapter\ClientInterface |
27
|
|
|
* - Psr\Log\LoggerInterface |
28
|
|
|
* |
29
|
|
|
* A Reader instance MUST have at least one parser added with the addParser() method to read feeds |
30
|
|
|
* It will throw a NoAccurateParserException if it cannot find a suitable parser for the feed. |
31
|
|
|
*/ |
32
|
|
|
class Reader |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var \FeedIo\Adapter\ClientInterface; |
36
|
|
|
*/ |
37
|
|
|
protected $client; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Psr\Log\LoggerInterface |
41
|
|
|
*/ |
42
|
|
|
protected $logger; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $parsers = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param ClientInterface $client |
51
|
|
|
* @param LoggerInterface $logger |
52
|
|
|
*/ |
53
|
15 |
|
public function __construct(ClientInterface $client, LoggerInterface $logger) |
54
|
|
|
{ |
55
|
15 |
|
$this->client = $client; |
56
|
15 |
|
$this->logger = $logger; |
57
|
15 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Parser $parser |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
14 |
|
public function addParser(ParserAbstract $parser) |
64
|
|
|
{ |
65
|
14 |
|
$this->logger->debug("new parser added : ".get_class($parser->getStandard())); |
66
|
14 |
|
$this->parsers[] = $parser; |
67
|
|
|
|
68
|
14 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* adds a filter to every parsers |
73
|
|
|
* |
74
|
|
|
* @param \FeedIo\FilterInterface $filter |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
2 |
|
public function addFilter(FilterInterface $filter) |
78
|
|
|
{ |
79
|
2 |
|
foreach ($this->parsers as $parser) { |
80
|
2 |
|
$parser->addFilter($filter); |
81
|
2 |
|
} |
82
|
|
|
|
83
|
2 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Reset filters on every parsers |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
1 |
|
public function resetFilters() |
91
|
|
|
{ |
92
|
1 |
|
foreach ($this->parsers as $parser) { |
93
|
1 |
|
$parser->resetFilters(); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $url |
101
|
|
|
* @param FeedInterface $feed |
102
|
|
|
* @param \DateTime $modifiedSince |
103
|
|
|
* @return \FeedIo\Reader\Result |
104
|
|
|
* @throws ReadErrorException |
105
|
|
|
*/ |
106
|
4 |
|
public function read($url, FeedInterface $feed, \DateTime $modifiedSince = null) |
107
|
|
|
{ |
108
|
4 |
|
$this->logger->debug("start reading {$url}"); |
109
|
4 |
|
if (is_null($modifiedSince)) { |
110
|
2 |
|
$this->logger->notice("no 'modifiedSince' parameter given, setting it to 01/01/1970"); |
111
|
2 |
|
$modifiedSince = new \DateTime('@0'); |
112
|
2 |
|
} |
113
|
|
|
|
114
|
|
|
try { |
115
|
4 |
|
$this->logger->info("hitting {$url}"); |
116
|
4 |
|
$response = $this->client->getResponse($url, $modifiedSince); |
117
|
3 |
|
$document = $this->handleResponse($response, $feed); |
118
|
|
|
|
119
|
3 |
|
return new Result($document, $feed, $modifiedSince, $response, $url); |
120
|
1 |
|
} catch (\Exception $e) { |
121
|
1 |
|
$this->logger->warning("{$url} read error : {$e->getMessage()}"); |
122
|
1 |
|
throw new ReadErrorException($e); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param ResponseInterface $response |
128
|
|
|
* @param FeedInterface $feed |
129
|
|
|
* @return Document |
130
|
|
|
*/ |
131
|
3 |
|
public function handleResponse(ResponseInterface $response, FeedInterface $feed) |
132
|
|
|
{ |
133
|
3 |
|
$this->logger->debug("response ok, now turning it into a document"); |
134
|
3 |
|
$document = new Document($response->getBody()); |
135
|
|
|
|
136
|
3 |
|
if ($response->isModified()) { |
137
|
2 |
|
$this->logger->info("the stream is modified, parsing it"); |
138
|
2 |
|
$this->parseDocument($document, $feed); |
139
|
2 |
|
} |
140
|
|
|
|
141
|
3 |
|
return $document; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param Document $document |
146
|
|
|
* @param FeedInterface $feed |
147
|
|
|
* @return FeedInterface |
148
|
|
|
* @throws Parser\UnsupportedFormatException |
149
|
|
|
* @throws Reader\NoAccurateParserException |
150
|
|
|
*/ |
151
|
3 |
|
public function parseDocument(Document $document, FeedInterface $feed) |
152
|
|
|
{ |
153
|
3 |
|
$parser = $this->getAccurateParser($document); |
154
|
3 |
|
$this->logger->debug("accurate parser : ".get_class($parser)); |
155
|
|
|
|
156
|
3 |
|
return $parser->parse($document, $feed); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param Document $document |
161
|
|
|
* @return ParserAbstract |
162
|
|
|
* @throws Reader\NoAccurateParserException |
163
|
|
|
*/ |
164
|
5 |
|
public function getAccurateParser(Document $document) |
165
|
|
|
{ |
166
|
5 |
|
foreach ($this->parsers as $parser) { |
167
|
4 |
|
if ($parser->getStandard()->canHandle($document)) { |
168
|
4 |
|
return $parser; |
169
|
|
|
} |
170
|
2 |
|
} |
171
|
|
|
|
172
|
1 |
|
$message = 'No parser can handle this stream'; |
173
|
1 |
|
$this->logger->error($message); |
174
|
1 |
|
throw new NoAccurateParserException($message); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|