1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xparse\Parser; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use GuzzleHttp\RequestOptions; |
9
|
|
|
use GuzzleHttp\TransferStats; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Xparse\ElementFinder\ElementFinder; |
13
|
|
|
use Xparse\ParserInterface\ParserInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @package Xparse\Parser |
18
|
|
|
*/ |
19
|
|
|
class Parser implements ParserInterface { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
* @var null|\Xparse\ElementFinder\ElementFinder |
24
|
|
|
*/ |
25
|
|
|
protected $lastPage = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ClientInterface |
29
|
|
|
*/ |
30
|
|
|
protected $client = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ElementFinderFactoryInterface |
34
|
|
|
*/ |
35
|
|
|
protected $elementFinderFactory = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var null|ResponseInterface |
39
|
|
|
*/ |
40
|
|
|
protected $lastResponse = null; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param ClientInterface|null $client |
45
|
|
|
* @param ElementFinderFactoryInterface|null $elementFinderFactory |
46
|
|
|
*/ |
47
|
9 |
|
public function __construct(ClientInterface $client = null, ElementFinderFactoryInterface $elementFinderFactory = null) { |
48
|
9 |
|
if ($client === null) { |
49
|
3 |
|
$client = new Client([ |
50
|
3 |
|
RequestOptions::ALLOW_REDIRECTS => true, |
51
|
3 |
|
RequestOptions::COOKIES => new \GuzzleHttp\Cookie\CookieJar(), |
52
|
3 |
|
]); |
53
|
3 |
|
} |
54
|
|
|
|
55
|
9 |
|
if ($elementFinderFactory === null) { |
56
|
9 |
|
$elementFinderFactory = new ElementFinderFactory(); |
57
|
9 |
|
} |
58
|
|
|
|
59
|
9 |
|
$this->elementFinderFactory = $elementFinderFactory; |
60
|
|
|
|
61
|
9 |
|
$this->client = $client; |
62
|
9 |
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $url |
67
|
|
|
* @param array $options |
68
|
|
|
* @return ElementFinder |
69
|
|
|
* @throws \Exception |
70
|
|
|
* @throws \InvalidArgumentException |
71
|
|
|
*/ |
72
|
4 |
|
public function get($url, array $options = []) { |
73
|
4 |
|
if (empty($url) or !is_string($url)) { |
74
|
1 |
|
throw new \InvalidArgumentException('Url must be not empty and string.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
$request = new Request('GET', $url); |
78
|
3 |
|
return $this->send($request, $options); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $url |
84
|
|
|
* @param string|resource|\Psr\Http\Message\StreamInterface $body Message body. |
85
|
|
|
* @param array $options |
86
|
|
|
* @return ElementFinder |
87
|
|
|
* @throws \Exception |
88
|
|
|
* @throws \InvalidArgumentException |
89
|
|
|
*/ |
90
|
2 |
|
public function post($url, $body = null, array $options = []) { |
91
|
|
|
|
92
|
2 |
|
if (empty($url) or !is_string($url)) { |
93
|
1 |
|
throw new \InvalidArgumentException('Url must be not empty and string.'); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$request = new Request('POST', $url, [], $body); |
97
|
|
|
|
98
|
1 |
|
return $this->send($request, $options); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return \Xparse\ElementFinder\ElementFinder |
104
|
|
|
*/ |
105
|
2 |
|
public function getLastPage() { |
106
|
2 |
|
return $this->lastPage; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param \Xparse\ElementFinder\ElementFinder $lastPage |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
4 |
|
public function setLastPage($lastPage) { |
115
|
4 |
|
$this->lastPage = $lastPage; |
116
|
4 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return null|ResponseInterface |
122
|
|
|
*/ |
123
|
1 |
|
public function getLastResponse() { |
124
|
1 |
|
return $this->lastResponse; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return ClientInterface |
130
|
|
|
*/ |
131
|
4 |
|
public function getClient() { |
132
|
4 |
|
return $this->client; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $request |
138
|
|
|
* @param array $options |
139
|
|
|
* @return \Xparse\ElementFinder\ElementFinder |
140
|
|
|
* @throws \Exception |
141
|
|
|
*/ |
142
|
4 |
|
public function send(RequestInterface $request, array $options = []) { |
143
|
|
|
|
144
|
4 |
|
$prevCallback = !empty($options['on_stats']) ? $options['on_stats'] : null; |
145
|
|
|
|
146
|
4 |
|
$effectiveUri = null; |
147
|
4 |
|
$options['on_stats'] = function (TransferStats $stats) use (&$effectiveUri, $prevCallback) { |
148
|
4 |
|
$effectiveUri = $stats->getEffectiveUri(); |
149
|
4 |
|
if ($prevCallback !== null) { |
150
|
|
|
/** @var callable $prevCallback */ |
151
|
|
|
$prevCallback($stats); |
152
|
|
|
} |
153
|
4 |
|
}; |
154
|
|
|
|
155
|
4 |
|
$response = $this->client->send($request, $options); |
156
|
|
|
|
157
|
4 |
|
$guzzleEffectiveUrl = $response->getHeaderLine('X-GUZZLE-EFFECTIVE-URL'); |
158
|
4 |
|
if (!empty($guzzleEffectiveUrl)) { |
159
|
|
|
$effectiveUri = $guzzleEffectiveUrl; |
160
|
|
|
} |
161
|
|
|
|
162
|
4 |
|
$page = $this->elementFinderFactory->create($response, $effectiveUri); |
163
|
|
|
|
164
|
4 |
|
$this->setLastPage($page); |
165
|
4 |
|
$this->lastResponse = $response; |
166
|
4 |
|
return $page; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return ElementFinderFactoryInterface |
172
|
|
|
*/ |
173
|
1 |
|
public function getElementFinderFactory() { |
174
|
1 |
|
return $this->elementFinderFactory; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|