1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information please see |
17
|
|
|
* <http://phing.info>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
use GuzzleHttp\Client; |
21
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
22
|
|
|
use GuzzleHttp\HandlerStack; |
23
|
|
|
use Phing\Exception\BuildException; |
24
|
|
|
use Phing\Type\Parameter; |
25
|
|
|
use Psr\Http\Message\ResponseInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Base class for GuzzleHttp-backed tasks |
29
|
|
|
* |
30
|
|
|
* Handles nested <config /> and <header /> tags, contains a method for |
31
|
|
|
* GuzzleHttp instance creation |
32
|
|
|
* |
33
|
|
|
* @package phing.tasks.ext |
34
|
|
|
* @author Alexey Borzov <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
abstract class HttpTask extends Task |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Holds the request URL |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $url = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Holds additional header data |
47
|
|
|
* |
48
|
|
|
* @var Parameter[] |
49
|
|
|
*/ |
50
|
|
|
protected $headers = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Holds additional config data for GuzzleHttp |
54
|
|
|
* |
55
|
|
|
* @var Parameter[] |
56
|
|
|
*/ |
57
|
|
|
protected $configData = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Holds the request method |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $method = 'GET'; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Holds the authentication user name |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $authUser = ''; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Holds the authentication password |
75
|
|
|
* |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
protected $authPassword = ''; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Holds the authentication scheme |
82
|
|
|
* |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
protected $authScheme = 'basic'; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var HandlerStack |
89
|
|
|
*/ |
90
|
|
|
protected static $handlerStack = null; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return HandlerStack |
94
|
|
|
*/ |
95
|
14 |
|
public static function getHandlerStack(): HandlerStack |
96
|
|
|
{ |
97
|
14 |
|
if (self::$handlerStack === null) { |
98
|
1 |
|
self::$handlerStack = HandlerStack::create(); |
99
|
|
|
} |
100
|
14 |
|
return self::$handlerStack; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Load the necessary environment for running this task. |
105
|
|
|
* |
106
|
|
|
* @throws BuildException |
107
|
|
|
*/ |
108
|
23 |
|
public function init() |
109
|
|
|
{ |
110
|
23 |
|
if (!class_exists('\GuzzleHttp\Client')) { |
111
|
|
|
throw new BuildException( |
112
|
|
|
get_class($this) . ' depends on Guzzle being installed ' |
113
|
|
|
. 'and on include_path.', |
114
|
|
|
$this->getLocation() |
115
|
|
|
); |
116
|
|
|
} |
117
|
23 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Sets the request URL |
121
|
|
|
* |
122
|
|
|
* @param string $url |
123
|
|
|
*/ |
124
|
15 |
|
public function setUrl($url) |
125
|
|
|
{ |
126
|
15 |
|
$this->url = $url; |
127
|
15 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Creates, configures, and sends a request |
131
|
|
|
* |
132
|
|
|
* @param array $options |
133
|
|
|
* @return ResponseInterface |
134
|
|
|
*/ |
135
|
14 |
|
protected function request($options = []) |
136
|
|
|
{ |
137
|
|
|
// set the authentication data |
138
|
14 |
|
if (!empty($this->authUser)) { |
139
|
|
|
$options['auth'] = [ |
140
|
2 |
|
$this->authUser, |
141
|
2 |
|
$this->authPassword, |
142
|
2 |
|
$this->authScheme |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
14 |
|
if (!empty($this->headers)) { |
147
|
2 |
|
$options['headers'] = []; |
148
|
|
|
|
149
|
2 |
|
foreach ($this->headers as $header) { |
150
|
2 |
|
$options['headers'][$header->getName()] = $header->getValue(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
14 |
|
foreach (array_keys($this->getProject()->getProperties()) as $propName) { |
155
|
14 |
|
if (0 === strpos($propName, 'phing.http.')) { |
156
|
3 |
|
$options[substr($propName, 11)] = (string) $this->getProject()->getProperty($propName); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
14 |
|
foreach ($this->configData as $parameter) { |
161
|
2 |
|
$options[$parameter->getName()] = $parameter->getValue(); |
162
|
|
|
} |
163
|
|
|
|
164
|
14 |
|
$client = new Client(['handler' => self::$handlerStack]); |
165
|
|
|
|
166
|
14 |
|
return $client->request($this->method, $this->url, $options); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Processes the server's response |
171
|
|
|
* |
172
|
|
|
* @param ResponseInterface $response |
173
|
|
|
* @return void |
174
|
|
|
* @throws BuildException |
175
|
|
|
*/ |
176
|
|
|
abstract protected function processResponse(ResponseInterface $response); |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Makes a HTTP request and processes its response |
180
|
|
|
* |
181
|
|
|
* @throws BuildException |
182
|
|
|
* @throws Exception |
183
|
|
|
*/ |
184
|
17 |
|
public function main() |
185
|
|
|
{ |
186
|
17 |
|
if (!isset($this->url)) { |
187
|
2 |
|
throw new BuildException("Required attribute 'url' is missing"); |
188
|
|
|
} |
189
|
|
|
|
190
|
15 |
|
$this->processResponse($this->request()); |
191
|
4 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Creates an additional header for this task |
195
|
|
|
* |
196
|
|
|
* @return Parameter The created header |
197
|
|
|
*/ |
198
|
2 |
|
public function createHeader() |
199
|
|
|
{ |
200
|
2 |
|
$num = array_push($this->headers, new Parameter()); |
201
|
|
|
|
202
|
2 |
|
return $this->headers[$num - 1]; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Creates a config parameter for this task |
207
|
|
|
* |
208
|
|
|
* @return Parameter The created config parameter |
209
|
|
|
*/ |
210
|
2 |
|
public function createConfig() |
211
|
|
|
{ |
212
|
2 |
|
$num = array_push($this->configData, new Parameter()); |
213
|
|
|
|
214
|
2 |
|
return $this->configData[$num - 1]; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Sets the authentication user name |
219
|
|
|
* |
220
|
|
|
* @param string $user |
221
|
|
|
*/ |
222
|
2 |
|
public function setAuthUser($user) |
223
|
|
|
{ |
224
|
2 |
|
$this->authUser = $user; |
225
|
2 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Sets the authentication password |
229
|
|
|
* |
230
|
|
|
* @param string $password |
231
|
|
|
*/ |
232
|
2 |
|
public function setAuthPassword($password) |
233
|
|
|
{ |
234
|
2 |
|
$this->authPassword = $password; |
235
|
2 |
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Sets the authentication scheme |
239
|
|
|
* |
240
|
|
|
* @param string $scheme |
241
|
|
|
*/ |
242
|
2 |
|
public function setAuthScheme($scheme) |
243
|
|
|
{ |
244
|
2 |
|
$this->authScheme = $scheme; |
245
|
2 |
|
} |
246
|
|
|
} |
247
|
|
|
|