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